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