V4L/DVB (5044): Pvrusb2: Allow overriding vbi and radio device minor numbers
[safe/jmp/linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-v4l2.c
1 /*
2  *
3  *  $Id$
4  *
5  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
6  *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include <linux/kernel.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
35 #define PVR2_NR_STREAMS 3
36
37 struct pvr2_v4l2_dev;
38 struct pvr2_v4l2_fh;
39 struct pvr2_v4l2;
40
41 struct pvr2_v4l2_dev {
42         struct video_device devbase; /* MUST be first! */
43         struct pvr2_v4l2 *v4lp;
44         struct pvr2_context_stream *stream;
45         enum pvr2_config config;
46 };
47
48 struct pvr2_v4l2_fh {
49         struct pvr2_channel channel;
50         struct pvr2_v4l2_dev *dev_info;
51         enum v4l2_priority prio;
52         struct pvr2_ioread *rhp;
53         struct file *file;
54         struct pvr2_v4l2 *vhead;
55         struct pvr2_v4l2_fh *vnext;
56         struct pvr2_v4l2_fh *vprev;
57         wait_queue_head_t wait_data;
58         int fw_mode_flag;
59 };
60
61 struct pvr2_v4l2 {
62         struct pvr2_channel channel;
63         struct pvr2_v4l2_fh *vfirst;
64         struct pvr2_v4l2_fh *vlast;
65
66         struct v4l2_prio_state prio;
67
68         /* streams */
69         struct pvr2_v4l2_dev *vdev;
70 };
71
72 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
73 module_param_array(video_nr, int, NULL, 0444);
74 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
75 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
76 module_param_array(radio_nr, int, NULL, 0444);
77 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
78 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
79 module_param_array(vbi_nr, int, NULL, 0444);
80 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
81
82 static struct v4l2_capability pvr_capability ={
83         .driver         = "pvrusb2",
84         .card           = "Hauppauge WinTV pvr-usb2",
85         .bus_info       = "usb",
86         .version        = KERNEL_VERSION(0,8,0),
87         .capabilities   = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
88                            V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
89                            V4L2_CAP_READWRITE),
90         .reserved       = {0,0,0,0}
91 };
92
93 static struct v4l2_tuner pvr_v4l2_tuners[]= {
94         {
95                 .index      = 0,
96                 .name       = "TV Tuner",
97                 .type           = V4L2_TUNER_ANALOG_TV,
98                 .capability     = (V4L2_TUNER_CAP_NORM |
99                                    V4L2_TUNER_CAP_STEREO |
100                                    V4L2_TUNER_CAP_LANG1 |
101                                    V4L2_TUNER_CAP_LANG2),
102                 .rangelow   = 0,
103                 .rangehigh  = 0,
104                 .rxsubchans     = V4L2_TUNER_SUB_STEREO,
105                 .audmode        = V4L2_TUNER_MODE_STEREO,
106                 .signal         = 0,
107                 .afc            = 0,
108                 .reserved       = {0,0,0,0}
109         }
110 };
111
112 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
113         {
114                 .index          = 0,
115                 .type           = V4L2_BUF_TYPE_VIDEO_CAPTURE,
116                 .flags          = V4L2_FMT_FLAG_COMPRESSED,
117                 .description    = "MPEG1/2",
118                 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
119                 // breaks when I do that.
120                 .pixelformat    = 0, // V4L2_PIX_FMT_MPEG,
121                 .reserved       = { 0, 0, 0, 0 }
122         }
123 };
124
125 #define PVR_FORMAT_PIX  0
126 #define PVR_FORMAT_VBI  1
127
128 static struct v4l2_format pvr_format [] = {
129         [PVR_FORMAT_PIX] = {
130                 .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
131                 .fmt    = {
132                         .pix        = {
133                                 .width          = 720,
134                                 .height             = 576,
135                                 // This should really be V4L2_PIX_FMT_MPEG,
136                                 // but xawtv breaks when I do that.
137                                 .pixelformat    = 0, // V4L2_PIX_FMT_MPEG,
138                                 .field          = V4L2_FIELD_INTERLACED,
139                                 .bytesperline   = 0,  // doesn't make sense
140                                                       // here
141                                 //FIXME : Don't know what to put here...
142                                 .sizeimage          = (32*1024),
143                                 .colorspace     = 0, // doesn't make sense here
144                                 .priv           = 0
145                         }
146                 }
147         },
148         [PVR_FORMAT_VBI] = {
149                 .type   = V4L2_BUF_TYPE_VBI_CAPTURE,
150                 .fmt    = {
151                         .vbi        = {
152                                 .sampling_rate = 27000000,
153                                 .offset = 248,
154                                 .samples_per_line = 1443,
155                                 .sample_format = V4L2_PIX_FMT_GREY,
156                                 .start = { 0, 0 },
157                                 .count = { 0, 0 },
158                                 .flags = 0,
159                                 .reserved = { 0, 0 }
160                         }
161                 }
162         }
163 };
164
165 /*
166  * pvr_ioctl()
167  *
168  * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
169  *
170  */
171 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
172                               unsigned int cmd, void *arg)
173 {
174         struct pvr2_v4l2_fh *fh = file->private_data;
175         struct pvr2_v4l2 *vp = fh->vhead;
176         struct pvr2_v4l2_dev *dev_info = fh->dev_info;
177         struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
178         int ret = -EINVAL;
179
180         if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
181                 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
182         }
183
184         if (!pvr2_hdw_dev_ok(hdw)) {
185                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
186                            "ioctl failed - bad or no context");
187                 return -EFAULT;
188         }
189
190         /* check priority */
191         switch (cmd) {
192         case VIDIOC_S_CTRL:
193         case VIDIOC_S_STD:
194         case VIDIOC_S_INPUT:
195         case VIDIOC_S_TUNER:
196         case VIDIOC_S_FREQUENCY:
197                 ret = v4l2_prio_check(&vp->prio, &fh->prio);
198                 if (ret)
199                         return ret;
200         }
201
202         switch (cmd) {
203         case VIDIOC_QUERYCAP:
204         {
205                 struct v4l2_capability *cap = arg;
206
207                 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
208
209                 ret = 0;
210                 break;
211         }
212
213         case VIDIOC_G_PRIORITY:
214         {
215                 enum v4l2_priority *p = arg;
216
217                 *p = v4l2_prio_max(&vp->prio);
218                 ret = 0;
219                 break;
220         }
221
222         case VIDIOC_S_PRIORITY:
223         {
224                 enum v4l2_priority *prio = arg;
225
226                 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
227                 break;
228         }
229
230         case VIDIOC_ENUMSTD:
231         {
232                 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
233                 int idx = vs->index;
234                 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
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
262                 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
263
264                 memset(&tmp,0,sizeof(tmp));
265                 tmp.index = vi->index;
266                 ret = 0;
267                 switch (vi->index) {
268                 case PVR2_CVAL_INPUT_TV:
269                 case PVR2_CVAL_INPUT_RADIO:
270                         tmp.type = V4L2_INPUT_TYPE_TUNER;
271                         break;
272                 case PVR2_CVAL_INPUT_SVIDEO:
273                 case PVR2_CVAL_INPUT_COMPOSITE:
274                         tmp.type = V4L2_INPUT_TYPE_CAMERA;
275                         break;
276                 default:
277                         ret = -EINVAL;
278                         break;
279                 }
280                 if (ret < 0) break;
281
282                 cnt = 0;
283                 pvr2_ctrl_get_valname(cptr,vi->index,
284                                       tmp.name,sizeof(tmp.name)-1,&cnt);
285                 tmp.name[cnt] = 0;
286
287                 /* Don't bother with audioset, since this driver currently
288                    always switches the audio whenever the video is
289                    switched. */
290
291                 /* Handling std is a tougher problem.  It doesn't make
292                    sense in cases where a device might be multi-standard.
293                    We could just copy out the current value for the
294                    standard, but it can change over time.  For now just
295                    leave it zero. */
296
297                 memcpy(vi, &tmp, sizeof(tmp));
298
299                 ret = 0;
300                 break;
301         }
302
303         case VIDIOC_G_INPUT:
304         {
305                 struct pvr2_ctrl *cptr;
306                 struct v4l2_input *vi = (struct v4l2_input *)arg;
307                 int val;
308                 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
309                 val = 0;
310                 ret = pvr2_ctrl_get_value(cptr,&val);
311                 vi->index = val;
312                 break;
313         }
314
315         case VIDIOC_S_INPUT:
316         {
317                 struct v4l2_input *vi = (struct v4l2_input *)arg;
318                 ret = pvr2_ctrl_set_value(
319                         pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
320                         vi->index);
321                 break;
322         }
323
324         case VIDIOC_ENUMAUDIO:
325         {
326                 ret = -EINVAL;
327                 break;
328         }
329
330         case VIDIOC_G_AUDIO:
331         {
332                 ret = -EINVAL;
333                 break;
334         }
335
336         case VIDIOC_S_AUDIO:
337         {
338                 ret = -EINVAL;
339                 break;
340         }
341         case VIDIOC_G_TUNER:
342         {
343                 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
344                 unsigned int status_mask;
345                 int val;
346                 if (vt->index !=0) break;
347
348                 status_mask = pvr2_hdw_get_signal_status(hdw);
349
350                 memcpy(vt, &pvr_v4l2_tuners[vt->index],
351                        sizeof(struct v4l2_tuner));
352
353                 vt->signal = 0;
354                 if (status_mask & PVR2_SIGNAL_OK) {
355                         if (status_mask & PVR2_SIGNAL_STEREO) {
356                                 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
357                         } else {
358                                 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
359                         }
360                         if (status_mask & PVR2_SIGNAL_SAP) {
361                                 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
362                                                    V4L2_TUNER_SUB_LANG2);
363                         }
364                         vt->signal = 65535;
365                 }
366
367                 val = 0;
368                 ret = pvr2_ctrl_get_value(
369                         pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
370                         &val);
371                 vt->audmode = val;
372                 break;
373         }
374
375         case VIDIOC_S_TUNER:
376         {
377                 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
378
379                 if (vt->index != 0)
380                         break;
381
382                 ret = pvr2_ctrl_set_value(
383                         pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
384                         vt->audmode);
385         }
386
387         case VIDIOC_S_FREQUENCY:
388         {
389                 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
390                 ret = pvr2_ctrl_set_value(
391                         pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
392                         vf->frequency * 62500);
393                 break;
394         }
395
396         case VIDIOC_G_FREQUENCY:
397         {
398                 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
399                 int val = 0;
400                 ret = pvr2_ctrl_get_value(
401                         pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
402                         &val);
403                 val /= 62500;
404                 vf->frequency = val;
405                 break;
406         }
407
408         case VIDIOC_ENUM_FMT:
409         {
410                 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
411
412                 /* Only one format is supported : mpeg.*/
413                 if (fd->index != 0)
414                         break;
415
416                 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
417                 ret = 0;
418                 break;
419         }
420
421         case VIDIOC_G_FMT:
422         {
423                 struct v4l2_format *vf = (struct v4l2_format *)arg;
424                 int val;
425                 switch(vf->type) {
426                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
427                         memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
428                                sizeof(struct v4l2_format));
429                         val = 0;
430                         pvr2_ctrl_get_value(
431                                 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
432                                 &val);
433                         vf->fmt.pix.width = val;
434                         val = 0;
435                         pvr2_ctrl_get_value(
436                                 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
437                                 &val);
438                         vf->fmt.pix.height = val;
439                         ret = 0;
440                         break;
441                 case V4L2_BUF_TYPE_VBI_CAPTURE:
442                         // ????? Still need to figure out to do VBI correctly
443                         ret = -EINVAL;
444                         break;
445                 default:
446                         ret = -EINVAL;
447                         break;
448                 }
449                 break;
450         }
451
452         case VIDIOC_TRY_FMT:
453         case VIDIOC_S_FMT:
454         {
455                 struct v4l2_format *vf = (struct v4l2_format *)arg;
456
457                 ret = 0;
458                 switch(vf->type) {
459                 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
460                         int lmin,lmax;
461                         struct pvr2_ctrl *hcp,*vcp;
462                         int h = vf->fmt.pix.height;
463                         int w = vf->fmt.pix.width;
464                         hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
465                         vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
466
467                         lmin = pvr2_ctrl_get_min(hcp);
468                         lmax = pvr2_ctrl_get_max(hcp);
469                         if (w < lmin) {
470                                 w = lmin;
471                         } else if (w > lmax) {
472                                 w = lmax;
473                         }
474                         lmin = pvr2_ctrl_get_min(vcp);
475                         lmax = pvr2_ctrl_get_max(vcp);
476                         if (h < lmin) {
477                                 h = lmin;
478                         } else if (h > lmax) {
479                                 h = lmax;
480                         }
481
482                         memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
483                                sizeof(struct v4l2_format));
484                         vf->fmt.pix.width = w;
485                         vf->fmt.pix.height = h;
486
487                         if (cmd == VIDIOC_S_FMT) {
488                                 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
489                                 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
490                         }
491                 } break;
492                 case V4L2_BUF_TYPE_VBI_CAPTURE:
493                         // ????? Still need to figure out to do VBI correctly
494                         ret = -EINVAL;
495                         break;
496                 default:
497                         ret = -EINVAL;
498                         break;
499                 }
500                 break;
501         }
502
503         case VIDIOC_STREAMON:
504         {
505                 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
506                 if (ret < 0) return ret;
507                 ret = pvr2_hdw_set_streaming(hdw,!0);
508                 break;
509         }
510
511         case VIDIOC_STREAMOFF:
512         {
513                 ret = pvr2_hdw_set_streaming(hdw,0);
514                 break;
515         }
516
517         case VIDIOC_QUERYCTRL:
518         {
519                 struct pvr2_ctrl *cptr;
520                 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
521                 ret = 0;
522                 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
523                         cptr = pvr2_hdw_get_ctrl_nextv4l(
524                                 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
525                         if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
526                 } else {
527                         cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
528                 }
529                 if (!cptr) {
530                         pvr2_trace(PVR2_TRACE_V4LIOCTL,
531                                    "QUERYCTRL id=0x%x not implemented here",
532                                    vc->id);
533                         ret = -EINVAL;
534                         break;
535                 }
536
537                 pvr2_trace(PVR2_TRACE_V4LIOCTL,
538                            "QUERYCTRL id=0x%x mapping name=%s (%s)",
539                            vc->id,pvr2_ctrl_get_name(cptr),
540                            pvr2_ctrl_get_desc(cptr));
541                 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
542                 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
543                 vc->default_value = pvr2_ctrl_get_def(cptr);
544                 switch (pvr2_ctrl_get_type(cptr)) {
545                 case pvr2_ctl_enum:
546                         vc->type = V4L2_CTRL_TYPE_MENU;
547                         vc->minimum = 0;
548                         vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
549                         vc->step = 1;
550                         break;
551                 case pvr2_ctl_bool:
552                         vc->type = V4L2_CTRL_TYPE_BOOLEAN;
553                         vc->minimum = 0;
554                         vc->maximum = 1;
555                         vc->step = 1;
556                         break;
557                 case pvr2_ctl_int:
558                         vc->type = V4L2_CTRL_TYPE_INTEGER;
559                         vc->minimum = pvr2_ctrl_get_min(cptr);
560                         vc->maximum = pvr2_ctrl_get_max(cptr);
561                         vc->step = 1;
562                         break;
563                 default:
564                         pvr2_trace(PVR2_TRACE_V4LIOCTL,
565                                    "QUERYCTRL id=0x%x name=%s not mappable",
566                                    vc->id,pvr2_ctrl_get_name(cptr));
567                         ret = -EINVAL;
568                         break;
569                 }
570                 break;
571         }
572
573         case VIDIOC_QUERYMENU:
574         {
575                 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
576                 unsigned int cnt = 0;
577                 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
578                                             vm->index,
579                                             vm->name,sizeof(vm->name)-1,
580                                             &cnt);
581                 vm->name[cnt] = 0;
582                 break;
583         }
584
585         case VIDIOC_G_CTRL:
586         {
587                 struct v4l2_control *vc = (struct v4l2_control *)arg;
588                 int val = 0;
589                 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
590                                           &val);
591                 vc->value = val;
592                 break;
593         }
594
595         case VIDIOC_S_CTRL:
596         {
597                 struct v4l2_control *vc = (struct v4l2_control *)arg;
598                 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
599                                           vc->value);
600                 break;
601         }
602
603         case VIDIOC_G_EXT_CTRLS:
604         {
605                 struct v4l2_ext_controls *ctls =
606                         (struct v4l2_ext_controls *)arg;
607                 struct v4l2_ext_control *ctrl;
608                 unsigned int idx;
609                 int val;
610                 for (idx = 0; idx < ctls->count; idx++) {
611                         ctrl = ctls->controls + idx;
612                         ret = pvr2_ctrl_get_value(
613                                 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
614                         if (ret) {
615                                 ctls->error_idx = idx;
616                                 break;
617                         }
618                         /* Ensure that if read as a 64 bit value, the user
619                            will still get a hopefully sane value */
620                         ctrl->value64 = 0;
621                         ctrl->value = val;
622                 }
623                 break;
624         }
625
626         case VIDIOC_S_EXT_CTRLS:
627         {
628                 struct v4l2_ext_controls *ctls =
629                         (struct v4l2_ext_controls *)arg;
630                 struct v4l2_ext_control *ctrl;
631                 unsigned int idx;
632                 for (idx = 0; idx < ctls->count; idx++) {
633                         ctrl = ctls->controls + idx;
634                         ret = pvr2_ctrl_set_value(
635                                 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
636                                 ctrl->value);
637                         if (ret) {
638                                 ctls->error_idx = idx;
639                                 break;
640                         }
641                 }
642                 break;
643         }
644
645         case VIDIOC_TRY_EXT_CTRLS:
646         {
647                 struct v4l2_ext_controls *ctls =
648                         (struct v4l2_ext_controls *)arg;
649                 struct v4l2_ext_control *ctrl;
650                 struct pvr2_ctrl *pctl;
651                 unsigned int idx;
652                 /* For the moment just validate that the requested control
653                    actually exists. */
654                 for (idx = 0; idx < ctls->count; idx++) {
655                         ctrl = ctls->controls + idx;
656                         pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
657                         if (!pctl) {
658                                 ret = -EINVAL;
659                                 ctls->error_idx = idx;
660                                 break;
661                         }
662                 }
663                 break;
664         }
665
666         case VIDIOC_LOG_STATUS:
667         {
668                 pvr2_hdw_trigger_module_log(hdw);
669                 ret = 0;
670                 break;
671         }
672 #ifdef CONFIG_VIDEO_ADV_DEBUG
673         case VIDIOC_INT_G_REGISTER:
674         case VIDIOC_INT_S_REGISTER:
675         {
676                 u32 val;
677                 struct v4l2_register *req = (struct v4l2_register *)arg;
678                 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
679                 ret = pvr2_hdw_register_access(
680                         hdw,req->i2c_id,req->reg,
681                         cmd == VIDIOC_INT_S_REGISTER,&val);
682                 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
683                 break;
684         }
685 #endif
686
687         default :
688                 ret = v4l_compat_translate_ioctl(inode,file,cmd,
689                                                  arg,pvr2_v4l2_do_ioctl);
690         }
691
692         pvr2_hdw_commit_ctl(hdw);
693
694         if (ret < 0) {
695                 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
696                         pvr2_trace(PVR2_TRACE_V4LIOCTL,
697                                    "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
698                 } else {
699                         if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
700                                 pvr2_trace(PVR2_TRACE_V4LIOCTL,
701                                            "pvr2_v4l2_do_ioctl failure, ret=%d"
702                                            " command was:",ret);
703                                 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
704                                                 cmd);
705                         }
706                 }
707         } else {
708                 pvr2_trace(PVR2_TRACE_V4LIOCTL,
709                            "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
710                            ret,ret);
711         }
712         return ret;
713 }
714
715
716 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
717 {
718         printk(KERN_INFO "pvrusb2: unregistering device video%d [%s]\n",
719                dip->devbase.minor,pvr2_config_get_name(dip->config));
720
721         /* Paranoia */
722         dip->v4lp = NULL;
723         dip->stream = NULL;
724
725         /* Actual deallocation happens later when all internal references
726            are gone. */
727         video_unregister_device(&dip->devbase);
728 }
729
730
731 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
732 {
733         pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
734                                         pvr2_v4l_type_video,-1);
735         pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
736                                         pvr2_v4l_type_vbi,-1);
737         pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
738                                         pvr2_v4l_type_radio,-1);
739         pvr2_v4l2_dev_destroy(vp->vdev);
740
741         pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
742         pvr2_channel_done(&vp->channel);
743         kfree(vp);
744 }
745
746
747 static void pvr2_video_device_release(struct video_device *vdev)
748 {
749         struct pvr2_v4l2_dev *dev;
750         dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
751         kfree(dev);
752 }
753
754
755 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
756 {
757         struct pvr2_v4l2 *vp;
758         vp = container_of(chp,struct pvr2_v4l2,channel);
759         if (!vp->channel.mc_head->disconnect_flag) return;
760         if (vp->vfirst) return;
761         pvr2_v4l2_destroy_no_lock(vp);
762 }
763
764
765 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
766                            unsigned int cmd, unsigned long arg)
767 {
768
769 /* Temporary hack : use ivtv api until a v4l2 one is available. */
770 #define IVTV_IOC_G_CODEC        0xFFEE7703
771 #define IVTV_IOC_S_CODEC        0xFFEE7704
772         if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
773         return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
774 }
775
776
777 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
778 {
779         struct pvr2_v4l2_fh *fhp = file->private_data;
780         struct pvr2_v4l2 *vp = fhp->vhead;
781         struct pvr2_context *mp = fhp->vhead->channel.mc_head;
782
783         pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
784
785         if (fhp->rhp) {
786                 struct pvr2_stream *sp;
787                 struct pvr2_hdw *hdw;
788                 hdw = fhp->channel.mc_head->hdw;
789                 pvr2_hdw_set_streaming(hdw,0);
790                 sp = pvr2_ioread_get_stream(fhp->rhp);
791                 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
792                 pvr2_ioread_destroy(fhp->rhp);
793                 fhp->rhp = NULL;
794         }
795
796         if (fhp->dev_info->config == pvr2_config_radio) {
797                 int ret;
798                 struct pvr2_hdw *hdw;
799                 hdw = fhp->channel.mc_head->hdw;
800                 if ((ret = pvr2_ctrl_set_value(
801                         pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
802                         PVR2_CVAL_INPUT_TV))) {
803                         return ret;
804                 }
805         }
806
807         v4l2_prio_close(&vp->prio, &fhp->prio);
808         file->private_data = NULL;
809
810         pvr2_context_enter(mp); do {
811                 if (fhp->vnext) {
812                         fhp->vnext->vprev = fhp->vprev;
813                 } else {
814                         vp->vlast = fhp->vprev;
815                 }
816                 if (fhp->vprev) {
817                         fhp->vprev->vnext = fhp->vnext;
818                 } else {
819                         vp->vfirst = fhp->vnext;
820                 }
821                 fhp->vnext = NULL;
822                 fhp->vprev = NULL;
823                 fhp->vhead = NULL;
824                 pvr2_channel_done(&fhp->channel);
825                 pvr2_trace(PVR2_TRACE_STRUCT,
826                            "Destroying pvr_v4l2_fh id=%p",fhp);
827                 kfree(fhp);
828                 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
829                         pvr2_v4l2_destroy_no_lock(vp);
830                 }
831         } while (0); pvr2_context_exit(mp);
832         return 0;
833 }
834
835
836 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
837 {
838         struct pvr2_v4l2_dev *dip; /* Our own context pointer */
839         struct pvr2_v4l2_fh *fhp;
840         struct pvr2_v4l2 *vp;
841         struct pvr2_hdw *hdw;
842
843         dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
844
845         vp = dip->v4lp;
846         hdw = vp->channel.hdw;
847
848         pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
849
850         if (!pvr2_hdw_dev_ok(hdw)) {
851                 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
852                            "pvr2_v4l2_open: hardware not ready");
853                 return -EIO;
854         }
855
856         fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
857         if (!fhp) {
858                 return -ENOMEM;
859         }
860         memset(fhp,0,sizeof(*fhp));
861
862         init_waitqueue_head(&fhp->wait_data);
863         fhp->dev_info = dip;
864
865         pvr2_context_enter(vp->channel.mc_head); do {
866                 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
867                 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
868
869                 /* pk: warning, severe ugliness follows. 18+ only.
870                    please blaim V4L(ivtv) for braindamaged interfaces,
871                    not the implementor. This is probably flawed, but
872                    suggestions on how to do this "right" are welcome! */
873                 if (dip->config == pvr2_config_radio) {
874                         int ret;
875                         if ((pvr2_channel_check_stream_no_lock(&fhp->channel,
876                                               fhp->dev_info->stream)) != 0) {
877                                 /* We can 't switch modes while streaming */
878                                 pvr2_channel_done(&fhp->channel);
879                                 kfree(fhp);
880                                 pvr2_context_exit(vp->channel.mc_head);
881                                 return -EBUSY;
882                         }
883
884                         if ((ret = pvr2_ctrl_set_value(
885                                 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
886                                                   PVR2_CVAL_INPUT_RADIO))) {
887                                 pvr2_channel_done(&fhp->channel);
888                                 kfree(fhp);
889                                 pvr2_context_exit(vp->channel.mc_head);
890                                 return ret;
891                         }
892                 }
893
894                 fhp->vnext = NULL;
895                 fhp->vprev = vp->vlast;
896                 if (vp->vlast) {
897                         vp->vlast->vnext = fhp;
898                 } else {
899                         vp->vfirst = fhp;
900                 }
901                 vp->vlast = fhp;
902                 fhp->vhead = vp;
903         } while (0); pvr2_context_exit(vp->channel.mc_head);
904
905         fhp->file = file;
906         file->private_data = fhp;
907         v4l2_prio_open(&vp->prio,&fhp->prio);
908
909         fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
910
911         return 0;
912 }
913
914
915 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
916 {
917         wake_up(&fhp->wait_data);
918 }
919
920 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
921 {
922         int ret;
923         struct pvr2_stream *sp;
924         struct pvr2_hdw *hdw;
925         if (fh->rhp) return 0;
926
927         /* First read() attempt.  Try to claim the stream and start
928            it... */
929         if ((ret = pvr2_channel_claim_stream(&fh->channel,
930                                              fh->dev_info->stream)) != 0) {
931                 /* Someone else must already have it */
932                 return ret;
933         }
934
935         fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
936         if (!fh->rhp) {
937                 pvr2_channel_claim_stream(&fh->channel,NULL);
938                 return -ENOMEM;
939         }
940
941         hdw = fh->channel.mc_head->hdw;
942         sp = fh->dev_info->stream->stream;
943         pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
944         pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
945         pvr2_hdw_set_streaming(hdw,!0);
946         ret = pvr2_ioread_set_enabled(fh->rhp,!0);
947
948         return ret;
949 }
950
951
952 static ssize_t pvr2_v4l2_read(struct file *file,
953                               char __user *buff, size_t count, loff_t *ppos)
954 {
955         struct pvr2_v4l2_fh *fh = file->private_data;
956         int ret;
957
958         if (fh->fw_mode_flag) {
959                 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
960                 char *tbuf;
961                 int c1,c2;
962                 int tcnt = 0;
963                 unsigned int offs = *ppos;
964
965                 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
966                 if (!tbuf) return -ENOMEM;
967
968                 while (count) {
969                         c1 = count;
970                         if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
971                         c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
972                         if (c2 < 0) {
973                                 tcnt = c2;
974                                 break;
975                         }
976                         if (!c2) break;
977                         if (copy_to_user(buff,tbuf,c2)) {
978                                 tcnt = -EFAULT;
979                                 break;
980                         }
981                         offs += c2;
982                         tcnt += c2;
983                         buff += c2;
984                         count -= c2;
985                         *ppos += c2;
986                 }
987                 kfree(tbuf);
988                 return tcnt;
989         }
990
991         if (fh->dev_info->config == pvr2_config_radio) {
992                 /* Radio device nodes on this device
993                    cannot be read or written. */
994                 return -EPERM;
995         }
996
997         if (!fh->rhp) {
998                 ret = pvr2_v4l2_iosetup(fh);
999                 if (ret) {
1000                         return ret;
1001                 }
1002         }
1003
1004         for (;;) {
1005                 ret = pvr2_ioread_read(fh->rhp,buff,count);
1006                 if (ret >= 0) break;
1007                 if (ret != -EAGAIN) break;
1008                 if (file->f_flags & O_NONBLOCK) break;
1009                 /* Doing blocking I/O.  Wait here. */
1010                 ret = wait_event_interruptible(
1011                         fh->wait_data,
1012                         pvr2_ioread_avail(fh->rhp) >= 0);
1013                 if (ret < 0) break;
1014         }
1015
1016         return ret;
1017 }
1018
1019
1020 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1021 {
1022         unsigned int mask = 0;
1023         struct pvr2_v4l2_fh *fh = file->private_data;
1024         int ret;
1025
1026         if (fh->fw_mode_flag) {
1027                 mask |= POLLIN | POLLRDNORM;
1028                 return mask;
1029         }
1030
1031         if (fh->dev_info->config == pvr2_config_radio) {
1032                 /* Radio device nodes on this device
1033                    cannot be read or written. */
1034                 return -EPERM;
1035         }
1036
1037         if (!fh->rhp) {
1038                 ret = pvr2_v4l2_iosetup(fh);
1039                 if (ret) return POLLERR;
1040         }
1041
1042         poll_wait(file,&fh->wait_data,wait);
1043
1044         if (pvr2_ioread_avail(fh->rhp) >= 0) {
1045                 mask |= POLLIN | POLLRDNORM;
1046         }
1047
1048         return mask;
1049 }
1050
1051
1052 static const struct file_operations vdev_fops = {
1053         .owner      = THIS_MODULE,
1054         .open       = pvr2_v4l2_open,
1055         .release    = pvr2_v4l2_release,
1056         .read       = pvr2_v4l2_read,
1057         .ioctl      = pvr2_v4l2_ioctl,
1058         .llseek     = no_llseek,
1059         .poll       = pvr2_v4l2_poll,
1060 };
1061
1062
1063 #define VID_HARDWARE_PVRUSB2    38  /* FIXME : need a good value */
1064
1065 static struct video_device vdev_template = {
1066         .owner      = THIS_MODULE,
1067         .type       = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1068         .type2      = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1069                        | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1070                        | V4L2_CAP_READWRITE),
1071         .hardware   = VID_HARDWARE_PVRUSB2,
1072         .fops       = &vdev_fops,
1073 };
1074
1075
1076 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1077                                struct pvr2_v4l2 *vp,
1078                                enum pvr2_config cfg)
1079 {
1080         int mindevnum;
1081         int unit_number;
1082         int v4l_type;
1083         enum pvr2_v4l_type pvt;
1084         dip->v4lp = vp;
1085         dip->config = cfg;
1086
1087
1088         switch (cfg) {
1089         case pvr2_config_mpeg:
1090                 v4l_type = VFL_TYPE_GRABBER;
1091                 pvt = pvr2_v4l_type_video;
1092                 dip->stream = &vp->channel.mc_head->video_stream;
1093                 break;
1094         case pvr2_config_vbi:
1095                 v4l_type = VFL_TYPE_VBI;
1096                 pvt = pvr2_v4l_type_vbi;
1097                 break;
1098         case pvr2_config_radio:
1099                 v4l_type = VFL_TYPE_RADIO;
1100                 pvt = pvr2_v4l_type_radio;
1101                 break;
1102         default:
1103                 /* Bail out (this should be impossible) */
1104                 err("Failed to set up pvrusb2 v4l dev"
1105                     " due to unrecognized config");
1106                 return;
1107         }
1108
1109         /* radio device doesn 't need its own stream */
1110         if (!dip->stream && cfg != pvr2_config_radio) {
1111                 err("Failed to set up pvrusb2 v4l dev"
1112                     " due to missing stream instance");
1113                 return;
1114         }
1115
1116         memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1117         dip->devbase.release = pvr2_video_device_release;
1118
1119         mindevnum = -1;
1120         unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1121         if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1122                 switch (v4l_type) {
1123                 case VFL_TYPE_VBI:
1124                         mindevnum = vbi_nr[unit_number];
1125                         break;
1126                 case VFL_TYPE_RADIO:
1127                         mindevnum = radio_nr[unit_number];
1128                         break;
1129                 case VFL_TYPE_GRABBER:
1130                 default:
1131                         mindevnum = video_nr[unit_number];
1132                         break;
1133                 }
1134         }
1135         if ((video_register_device(&dip->devbase, v4l_type, mindevnum) < 0) &&
1136             (video_register_device(&dip->devbase, v4l_type, -1) < 0)) {
1137                 err("Failed to register pvrusb2 v4l device");
1138         }
1139         switch (cfg) {
1140         case pvr2_config_mpeg:
1141                 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
1142                        dip->devbase.minor & 0x1f,
1143                        pvr2_config_get_name(dip->config));
1144         break;
1145         case pvr2_config_vbi:
1146                 printk(KERN_INFO "pvrusb2: registered device vbi%d [%s]\n",
1147                        dip->devbase.minor & 0x1f,
1148                        pvr2_config_get_name(dip->config));
1149         break;
1150         case pvr2_config_radio:
1151                 printk(KERN_INFO "pvrusb2: registered device radio%d [%s]\n",
1152                        dip->devbase.minor & 0x1f,
1153                        pvr2_config_get_name(dip->config));
1154         break;
1155         default:
1156         break;
1157         }
1158
1159         pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1160                                         pvt,dip->devbase.minor);
1161 }
1162
1163
1164 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1165 {
1166         struct pvr2_v4l2 *vp;
1167
1168         vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1169         if (!vp) return vp;
1170         memset(vp,0,sizeof(*vp));
1171         vp->vdev = kmalloc(sizeof(*vp->vdev)*PVR2_NR_STREAMS,GFP_KERNEL);
1172         if (!vp->vdev) {
1173                 kfree(vp);
1174                 return NULL;
1175         }
1176         memset(vp->vdev,0,sizeof(*vp->vdev)*PVR2_NR_STREAMS);
1177         pvr2_channel_init(&vp->channel,mnp);
1178         pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1179
1180         vp->channel.check_func = pvr2_v4l2_internal_check;
1181
1182         /* register streams */
1183         pvr2_v4l2_dev_init(&vp->vdev[0],vp,pvr2_config_mpeg);
1184         pvr2_v4l2_dev_init(&vp->vdev[2],vp,pvr2_config_radio);
1185
1186         return vp;
1187 }
1188
1189 /*
1190   Stuff for Emacs to see, in order to encourage consistent editing style:
1191   *** Local Variables: ***
1192   *** mode: c ***
1193   *** fill-column: 75 ***
1194   *** tab-width: 8 ***
1195   *** c-basic-offset: 8 ***
1196   *** End: ***
1197   */