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