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