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