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