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