V4L/DVB (5058): Pvrusb2: bug fix involving switch into radio mode
[linux-2.6/x86.git] / drivers / media / video / pvrusb2 / pvrusb2-v4l2.c
blob8b9089cc32fed4fb637bc31789783e50e39ea6eb
1 /*
3 * $Id$
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
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
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.
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
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>
35 struct pvr2_v4l2_dev;
36 struct pvr2_v4l2_fh;
37 struct pvr2_v4l2;
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 */
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;
62 struct pvr2_v4l2 {
63 struct pvr2_channel channel;
64 struct pvr2_v4l2_fh *vfirst;
65 struct pvr2_v4l2_fh *vlast;
67 struct v4l2_prio_state prio;
69 /* streams - Note that these must be separately, individually,
70 * allocated pointers. This is because the v4l core is going to
71 * manage their deletion - separately, individually... */
72 struct pvr2_v4l2_dev *dev_video;
73 struct pvr2_v4l2_dev *dev_radio;
76 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
77 module_param_array(video_nr, int, NULL, 0444);
78 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
79 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
80 module_param_array(radio_nr, int, NULL, 0444);
81 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
82 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
83 module_param_array(vbi_nr, int, NULL, 0444);
84 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
86 static struct v4l2_capability pvr_capability ={
87 .driver = "pvrusb2",
88 .card = "Hauppauge WinTV pvr-usb2",
89 .bus_info = "usb",
90 .version = KERNEL_VERSION(0,8,0),
91 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
92 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
93 V4L2_CAP_READWRITE),
94 .reserved = {0,0,0,0}
97 static struct v4l2_tuner pvr_v4l2_tuners[]= {
99 .index = 0,
100 .name = "TV Tuner",
101 .type = V4L2_TUNER_ANALOG_TV,
102 .capability = (V4L2_TUNER_CAP_NORM |
103 V4L2_TUNER_CAP_STEREO |
104 V4L2_TUNER_CAP_LANG1 |
105 V4L2_TUNER_CAP_LANG2),
106 .rangelow = 0,
107 .rangehigh = 0,
108 .rxsubchans = V4L2_TUNER_SUB_STEREO,
109 .audmode = V4L2_TUNER_MODE_STEREO,
110 .signal = 0,
111 .afc = 0,
112 .reserved = {0,0,0,0}
116 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
118 .index = 0,
119 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
120 .flags = V4L2_FMT_FLAG_COMPRESSED,
121 .description = "MPEG1/2",
122 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
123 // breaks when I do that.
124 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
125 .reserved = { 0, 0, 0, 0 }
129 #define PVR_FORMAT_PIX 0
130 #define PVR_FORMAT_VBI 1
132 static struct v4l2_format pvr_format [] = {
133 [PVR_FORMAT_PIX] = {
134 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
135 .fmt = {
136 .pix = {
137 .width = 720,
138 .height = 576,
139 // This should really be V4L2_PIX_FMT_MPEG,
140 // but xawtv breaks when I do that.
141 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
142 .field = V4L2_FIELD_INTERLACED,
143 .bytesperline = 0, // doesn't make sense
144 // here
145 //FIXME : Don't know what to put here...
146 .sizeimage = (32*1024),
147 .colorspace = 0, // doesn't make sense here
148 .priv = 0
152 [PVR_FORMAT_VBI] = {
153 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
154 .fmt = {
155 .vbi = {
156 .sampling_rate = 27000000,
157 .offset = 248,
158 .samples_per_line = 1443,
159 .sample_format = V4L2_PIX_FMT_GREY,
160 .start = { 0, 0 },
161 .count = { 0, 0 },
162 .flags = 0,
163 .reserved = { 0, 0 }
170 static const char *get_v4l_name(int v4l_type)
172 switch (v4l_type) {
173 case VFL_TYPE_GRABBER: return "video";
174 case VFL_TYPE_RADIO: return "radio";
175 case VFL_TYPE_VBI: return "vbi";
176 default: return "?";
182 * pvr_ioctl()
184 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
187 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
188 unsigned int cmd, void *arg)
190 struct pvr2_v4l2_fh *fh = file->private_data;
191 struct pvr2_v4l2 *vp = fh->vhead;
192 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
193 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
194 int ret = -EINVAL;
196 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
197 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
200 if (!pvr2_hdw_dev_ok(hdw)) {
201 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
202 "ioctl failed - bad or no context");
203 return -EFAULT;
206 /* check priority */
207 switch (cmd) {
208 case VIDIOC_S_CTRL:
209 case VIDIOC_S_STD:
210 case VIDIOC_S_INPUT:
211 case VIDIOC_S_TUNER:
212 case VIDIOC_S_FREQUENCY:
213 ret = v4l2_prio_check(&vp->prio, &fh->prio);
214 if (ret)
215 return ret;
218 switch (cmd) {
219 case VIDIOC_QUERYCAP:
221 struct v4l2_capability *cap = arg;
223 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
225 ret = 0;
226 break;
229 case VIDIOC_G_PRIORITY:
231 enum v4l2_priority *p = arg;
233 *p = v4l2_prio_max(&vp->prio);
234 ret = 0;
235 break;
238 case VIDIOC_S_PRIORITY:
240 enum v4l2_priority *prio = arg;
242 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
243 break;
246 case VIDIOC_ENUMSTD:
248 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
249 int idx = vs->index;
250 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
251 break;
254 case VIDIOC_G_STD:
256 int val = 0;
257 ret = pvr2_ctrl_get_value(
258 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
259 *(v4l2_std_id *)arg = val;
260 break;
263 case VIDIOC_S_STD:
265 ret = pvr2_ctrl_set_value(
266 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
267 *(v4l2_std_id *)arg);
268 break;
271 case VIDIOC_ENUMINPUT:
273 struct pvr2_ctrl *cptr;
274 struct v4l2_input *vi = (struct v4l2_input *)arg;
275 struct v4l2_input tmp;
276 unsigned int cnt;
278 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
280 memset(&tmp,0,sizeof(tmp));
281 tmp.index = vi->index;
282 ret = 0;
283 switch (vi->index) {
284 case PVR2_CVAL_INPUT_TV:
285 case PVR2_CVAL_INPUT_RADIO:
286 tmp.type = V4L2_INPUT_TYPE_TUNER;
287 break;
288 case PVR2_CVAL_INPUT_SVIDEO:
289 case PVR2_CVAL_INPUT_COMPOSITE:
290 tmp.type = V4L2_INPUT_TYPE_CAMERA;
291 break;
292 default:
293 ret = -EINVAL;
294 break;
296 if (ret < 0) break;
298 cnt = 0;
299 pvr2_ctrl_get_valname(cptr,vi->index,
300 tmp.name,sizeof(tmp.name)-1,&cnt);
301 tmp.name[cnt] = 0;
303 /* Don't bother with audioset, since this driver currently
304 always switches the audio whenever the video is
305 switched. */
307 /* Handling std is a tougher problem. It doesn't make
308 sense in cases where a device might be multi-standard.
309 We could just copy out the current value for the
310 standard, but it can change over time. For now just
311 leave it zero. */
313 memcpy(vi, &tmp, sizeof(tmp));
315 ret = 0;
316 break;
319 case VIDIOC_G_INPUT:
321 struct pvr2_ctrl *cptr;
322 struct v4l2_input *vi = (struct v4l2_input *)arg;
323 int val;
324 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
325 val = 0;
326 ret = pvr2_ctrl_get_value(cptr,&val);
327 vi->index = val;
328 break;
331 case VIDIOC_S_INPUT:
333 struct v4l2_input *vi = (struct v4l2_input *)arg;
334 ret = pvr2_ctrl_set_value(
335 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
336 vi->index);
337 break;
340 case VIDIOC_ENUMAUDIO:
342 ret = -EINVAL;
343 break;
346 case VIDIOC_G_AUDIO:
348 ret = -EINVAL;
349 break;
352 case VIDIOC_S_AUDIO:
354 ret = -EINVAL;
355 break;
357 case VIDIOC_G_TUNER:
359 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
360 unsigned int status_mask;
361 int val;
362 if (vt->index !=0) break;
364 status_mask = pvr2_hdw_get_signal_status(hdw);
366 memcpy(vt, &pvr_v4l2_tuners[vt->index],
367 sizeof(struct v4l2_tuner));
369 vt->signal = 0;
370 if (status_mask & PVR2_SIGNAL_OK) {
371 if (status_mask & PVR2_SIGNAL_STEREO) {
372 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
373 } else {
374 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
376 if (status_mask & PVR2_SIGNAL_SAP) {
377 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
378 V4L2_TUNER_SUB_LANG2);
380 vt->signal = 65535;
383 val = 0;
384 ret = pvr2_ctrl_get_value(
385 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
386 &val);
387 vt->audmode = val;
388 break;
391 case VIDIOC_S_TUNER:
393 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
395 if (vt->index != 0)
396 break;
398 ret = pvr2_ctrl_set_value(
399 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
400 vt->audmode);
403 case VIDIOC_S_FREQUENCY:
405 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
406 unsigned long fv;
407 fv = vf->frequency;
408 if (vf->type == V4L2_TUNER_RADIO) {
409 fv = (fv * 125) / 2;
410 } else {
411 fv = fv * 62500;
413 ret = pvr2_ctrl_set_value(
414 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
415 break;
418 case VIDIOC_G_FREQUENCY:
420 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
421 int val = 0;
422 int cur_input = PVR2_CVAL_INPUT_TV;
423 ret = pvr2_ctrl_get_value(
424 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
425 &val);
426 if (ret != 0) break;
427 pvr2_ctrl_get_value(
428 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
429 &cur_input);
430 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
431 val = (val * 2) / 125;
432 vf->frequency = val;
433 vf->type = V4L2_TUNER_RADIO;
434 } else {
435 val /= 62500;
436 vf->frequency = val;
437 vf->type = V4L2_TUNER_ANALOG_TV;
439 break;
442 case VIDIOC_ENUM_FMT:
444 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
446 /* Only one format is supported : mpeg.*/
447 if (fd->index != 0)
448 break;
450 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
451 ret = 0;
452 break;
455 case VIDIOC_G_FMT:
457 struct v4l2_format *vf = (struct v4l2_format *)arg;
458 int val;
459 switch(vf->type) {
460 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
461 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
462 sizeof(struct v4l2_format));
463 val = 0;
464 pvr2_ctrl_get_value(
465 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
466 &val);
467 vf->fmt.pix.width = val;
468 val = 0;
469 pvr2_ctrl_get_value(
470 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
471 &val);
472 vf->fmt.pix.height = val;
473 ret = 0;
474 break;
475 case V4L2_BUF_TYPE_VBI_CAPTURE:
476 // ????? Still need to figure out to do VBI correctly
477 ret = -EINVAL;
478 break;
479 default:
480 ret = -EINVAL;
481 break;
483 break;
486 case VIDIOC_TRY_FMT:
487 case VIDIOC_S_FMT:
489 struct v4l2_format *vf = (struct v4l2_format *)arg;
491 ret = 0;
492 switch(vf->type) {
493 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
494 int lmin,lmax;
495 struct pvr2_ctrl *hcp,*vcp;
496 int h = vf->fmt.pix.height;
497 int w = vf->fmt.pix.width;
498 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
499 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
501 lmin = pvr2_ctrl_get_min(hcp);
502 lmax = pvr2_ctrl_get_max(hcp);
503 if (w < lmin) {
504 w = lmin;
505 } else if (w > lmax) {
506 w = lmax;
508 lmin = pvr2_ctrl_get_min(vcp);
509 lmax = pvr2_ctrl_get_max(vcp);
510 if (h < lmin) {
511 h = lmin;
512 } else if (h > lmax) {
513 h = lmax;
516 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
517 sizeof(struct v4l2_format));
518 vf->fmt.pix.width = w;
519 vf->fmt.pix.height = h;
521 if (cmd == VIDIOC_S_FMT) {
522 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
523 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
525 } break;
526 case V4L2_BUF_TYPE_VBI_CAPTURE:
527 // ????? Still need to figure out to do VBI correctly
528 ret = -EINVAL;
529 break;
530 default:
531 ret = -EINVAL;
532 break;
534 break;
537 case VIDIOC_STREAMON:
539 if (!fh->dev_info->stream) {
540 /* No stream defined for this node. This means
541 that we're not currently allowed to stream from
542 this node. */
543 ret = -EPERM;
544 break;
546 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
547 if (ret < 0) return ret;
548 ret = pvr2_hdw_set_streaming(hdw,!0);
549 break;
552 case VIDIOC_STREAMOFF:
554 if (!fh->dev_info->stream) {
555 /* No stream defined for this node. This means
556 that we're not currently allowed to stream from
557 this node. */
558 ret = -EPERM;
559 break;
561 ret = pvr2_hdw_set_streaming(hdw,0);
562 break;
565 case VIDIOC_QUERYCTRL:
567 struct pvr2_ctrl *cptr;
568 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
569 ret = 0;
570 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
571 cptr = pvr2_hdw_get_ctrl_nextv4l(
572 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
573 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
574 } else {
575 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
577 if (!cptr) {
578 pvr2_trace(PVR2_TRACE_V4LIOCTL,
579 "QUERYCTRL id=0x%x not implemented here",
580 vc->id);
581 ret = -EINVAL;
582 break;
585 pvr2_trace(PVR2_TRACE_V4LIOCTL,
586 "QUERYCTRL id=0x%x mapping name=%s (%s)",
587 vc->id,pvr2_ctrl_get_name(cptr),
588 pvr2_ctrl_get_desc(cptr));
589 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
590 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
591 vc->default_value = pvr2_ctrl_get_def(cptr);
592 switch (pvr2_ctrl_get_type(cptr)) {
593 case pvr2_ctl_enum:
594 vc->type = V4L2_CTRL_TYPE_MENU;
595 vc->minimum = 0;
596 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
597 vc->step = 1;
598 break;
599 case pvr2_ctl_bool:
600 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
601 vc->minimum = 0;
602 vc->maximum = 1;
603 vc->step = 1;
604 break;
605 case pvr2_ctl_int:
606 vc->type = V4L2_CTRL_TYPE_INTEGER;
607 vc->minimum = pvr2_ctrl_get_min(cptr);
608 vc->maximum = pvr2_ctrl_get_max(cptr);
609 vc->step = 1;
610 break;
611 default:
612 pvr2_trace(PVR2_TRACE_V4LIOCTL,
613 "QUERYCTRL id=0x%x name=%s not mappable",
614 vc->id,pvr2_ctrl_get_name(cptr));
615 ret = -EINVAL;
616 break;
618 break;
621 case VIDIOC_QUERYMENU:
623 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
624 unsigned int cnt = 0;
625 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
626 vm->index,
627 vm->name,sizeof(vm->name)-1,
628 &cnt);
629 vm->name[cnt] = 0;
630 break;
633 case VIDIOC_G_CTRL:
635 struct v4l2_control *vc = (struct v4l2_control *)arg;
636 int val = 0;
637 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
638 &val);
639 vc->value = val;
640 break;
643 case VIDIOC_S_CTRL:
645 struct v4l2_control *vc = (struct v4l2_control *)arg;
646 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
647 vc->value);
648 break;
651 case VIDIOC_G_EXT_CTRLS:
653 struct v4l2_ext_controls *ctls =
654 (struct v4l2_ext_controls *)arg;
655 struct v4l2_ext_control *ctrl;
656 unsigned int idx;
657 int val;
658 for (idx = 0; idx < ctls->count; idx++) {
659 ctrl = ctls->controls + idx;
660 ret = pvr2_ctrl_get_value(
661 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
662 if (ret) {
663 ctls->error_idx = idx;
664 break;
666 /* Ensure that if read as a 64 bit value, the user
667 will still get a hopefully sane value */
668 ctrl->value64 = 0;
669 ctrl->value = val;
671 break;
674 case VIDIOC_S_EXT_CTRLS:
676 struct v4l2_ext_controls *ctls =
677 (struct v4l2_ext_controls *)arg;
678 struct v4l2_ext_control *ctrl;
679 unsigned int idx;
680 for (idx = 0; idx < ctls->count; idx++) {
681 ctrl = ctls->controls + idx;
682 ret = pvr2_ctrl_set_value(
683 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
684 ctrl->value);
685 if (ret) {
686 ctls->error_idx = idx;
687 break;
690 break;
693 case VIDIOC_TRY_EXT_CTRLS:
695 struct v4l2_ext_controls *ctls =
696 (struct v4l2_ext_controls *)arg;
697 struct v4l2_ext_control *ctrl;
698 struct pvr2_ctrl *pctl;
699 unsigned int idx;
700 /* For the moment just validate that the requested control
701 actually exists. */
702 for (idx = 0; idx < ctls->count; idx++) {
703 ctrl = ctls->controls + idx;
704 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
705 if (!pctl) {
706 ret = -EINVAL;
707 ctls->error_idx = idx;
708 break;
711 break;
714 case VIDIOC_LOG_STATUS:
716 pvr2_hdw_trigger_module_log(hdw);
717 ret = 0;
718 break;
720 #ifdef CONFIG_VIDEO_ADV_DEBUG
721 case VIDIOC_INT_G_REGISTER:
722 case VIDIOC_INT_S_REGISTER:
724 u32 val;
725 struct v4l2_register *req = (struct v4l2_register *)arg;
726 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
727 ret = pvr2_hdw_register_access(
728 hdw,req->i2c_id,req->reg,
729 cmd == VIDIOC_INT_S_REGISTER,&val);
730 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
731 break;
733 #endif
735 default :
736 ret = v4l_compat_translate_ioctl(inode,file,cmd,
737 arg,pvr2_v4l2_do_ioctl);
740 pvr2_hdw_commit_ctl(hdw);
742 if (ret < 0) {
743 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
744 pvr2_trace(PVR2_TRACE_V4LIOCTL,
745 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
746 } else {
747 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
748 pvr2_trace(PVR2_TRACE_V4LIOCTL,
749 "pvr2_v4l2_do_ioctl failure, ret=%d"
750 " command was:",ret);
751 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
752 cmd);
755 } else {
756 pvr2_trace(PVR2_TRACE_V4LIOCTL,
757 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
758 ret,ret);
760 return ret;
764 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
766 int minor_id = dip->devbase.minor;
767 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
768 enum pvr2_config cfg = dip->config;
769 int v4l_type = dip->v4l_type;
771 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
773 /* Paranoia */
774 dip->v4lp = NULL;
775 dip->stream = NULL;
777 /* Actual deallocation happens later when all internal references
778 are gone. */
779 video_unregister_device(&dip->devbase);
781 printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
782 get_v4l_name(v4l_type),minor_id & 0x1f,
783 pvr2_config_get_name(cfg));
788 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
790 if (vp->dev_video) {
791 pvr2_v4l2_dev_destroy(vp->dev_video);
792 vp->dev_video = 0;
794 if (vp->dev_radio) {
795 pvr2_v4l2_dev_destroy(vp->dev_radio);
796 vp->dev_radio = 0;
799 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
800 pvr2_channel_done(&vp->channel);
801 kfree(vp);
805 static void pvr2_video_device_release(struct video_device *vdev)
807 struct pvr2_v4l2_dev *dev;
808 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
809 kfree(dev);
813 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
815 struct pvr2_v4l2 *vp;
816 vp = container_of(chp,struct pvr2_v4l2,channel);
817 if (!vp->channel.mc_head->disconnect_flag) return;
818 if (vp->vfirst) return;
819 pvr2_v4l2_destroy_no_lock(vp);
823 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
824 unsigned int cmd, unsigned long arg)
827 /* Temporary hack : use ivtv api until a v4l2 one is available. */
828 #define IVTV_IOC_G_CODEC 0xFFEE7703
829 #define IVTV_IOC_S_CODEC 0xFFEE7704
830 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
831 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
835 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
837 struct pvr2_v4l2_fh *fhp = file->private_data;
838 struct pvr2_v4l2 *vp = fhp->vhead;
839 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
841 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
843 if (fhp->rhp) {
844 struct pvr2_stream *sp;
845 struct pvr2_hdw *hdw;
846 hdw = fhp->channel.mc_head->hdw;
847 pvr2_hdw_set_streaming(hdw,0);
848 sp = pvr2_ioread_get_stream(fhp->rhp);
849 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
850 pvr2_ioread_destroy(fhp->rhp);
851 fhp->rhp = NULL;
854 v4l2_prio_close(&vp->prio, &fhp->prio);
855 file->private_data = NULL;
857 pvr2_context_enter(mp); do {
858 if (fhp->vnext) {
859 fhp->vnext->vprev = fhp->vprev;
860 } else {
861 vp->vlast = fhp->vprev;
863 if (fhp->vprev) {
864 fhp->vprev->vnext = fhp->vnext;
865 } else {
866 vp->vfirst = fhp->vnext;
868 fhp->vnext = NULL;
869 fhp->vprev = NULL;
870 fhp->vhead = NULL;
871 pvr2_channel_done(&fhp->channel);
872 pvr2_trace(PVR2_TRACE_STRUCT,
873 "Destroying pvr_v4l2_fh id=%p",fhp);
874 kfree(fhp);
875 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
876 pvr2_v4l2_destroy_no_lock(vp);
878 } while (0); pvr2_context_exit(mp);
879 return 0;
883 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
885 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
886 struct pvr2_v4l2_fh *fhp;
887 struct pvr2_v4l2 *vp;
888 struct pvr2_hdw *hdw;
890 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
892 vp = dip->v4lp;
893 hdw = vp->channel.hdw;
895 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
897 if (!pvr2_hdw_dev_ok(hdw)) {
898 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
899 "pvr2_v4l2_open: hardware not ready");
900 return -EIO;
903 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
904 if (!fhp) {
905 return -ENOMEM;
907 memset(fhp,0,sizeof(*fhp));
909 init_waitqueue_head(&fhp->wait_data);
910 fhp->dev_info = dip;
912 pvr2_context_enter(vp->channel.mc_head); do {
913 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
914 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
916 /* Opening the /dev/radioX device implies a mode switch.
917 So execute that here. Note that you can get the
918 IDENTICAL effect merely by opening the normal video
919 device and setting the input appropriately. */
920 if (dip->v4l_type == VFL_TYPE_RADIO) {
921 pvr2_ctrl_set_value(
922 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
923 PVR2_CVAL_INPUT_RADIO);
924 pvr2_hdw_commit_ctl(hdw);
927 fhp->vnext = NULL;
928 fhp->vprev = vp->vlast;
929 if (vp->vlast) {
930 vp->vlast->vnext = fhp;
931 } else {
932 vp->vfirst = fhp;
934 vp->vlast = fhp;
935 fhp->vhead = vp;
936 } while (0); pvr2_context_exit(vp->channel.mc_head);
938 fhp->file = file;
939 file->private_data = fhp;
940 v4l2_prio_open(&vp->prio,&fhp->prio);
942 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
944 return 0;
948 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
950 wake_up(&fhp->wait_data);
953 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
955 int ret;
956 struct pvr2_stream *sp;
957 struct pvr2_hdw *hdw;
958 if (fh->rhp) return 0;
960 if (!fh->dev_info->stream) {
961 /* No stream defined for this node. This means that we're
962 not currently allowed to stream from this node. */
963 return -EPERM;
966 /* First read() attempt. Try to claim the stream and start
967 it... */
968 if ((ret = pvr2_channel_claim_stream(&fh->channel,
969 fh->dev_info->stream)) != 0) {
970 /* Someone else must already have it */
971 return ret;
974 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
975 if (!fh->rhp) {
976 pvr2_channel_claim_stream(&fh->channel,NULL);
977 return -ENOMEM;
980 hdw = fh->channel.mc_head->hdw;
981 sp = fh->dev_info->stream->stream;
982 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
983 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
984 pvr2_hdw_set_streaming(hdw,!0);
985 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
987 return ret;
991 static ssize_t pvr2_v4l2_read(struct file *file,
992 char __user *buff, size_t count, loff_t *ppos)
994 struct pvr2_v4l2_fh *fh = file->private_data;
995 int ret;
997 if (fh->fw_mode_flag) {
998 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
999 char *tbuf;
1000 int c1,c2;
1001 int tcnt = 0;
1002 unsigned int offs = *ppos;
1004 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1005 if (!tbuf) return -ENOMEM;
1007 while (count) {
1008 c1 = count;
1009 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1010 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1011 if (c2 < 0) {
1012 tcnt = c2;
1013 break;
1015 if (!c2) break;
1016 if (copy_to_user(buff,tbuf,c2)) {
1017 tcnt = -EFAULT;
1018 break;
1020 offs += c2;
1021 tcnt += c2;
1022 buff += c2;
1023 count -= c2;
1024 *ppos += c2;
1026 kfree(tbuf);
1027 return tcnt;
1030 if (!fh->rhp) {
1031 ret = pvr2_v4l2_iosetup(fh);
1032 if (ret) {
1033 return ret;
1037 for (;;) {
1038 ret = pvr2_ioread_read(fh->rhp,buff,count);
1039 if (ret >= 0) break;
1040 if (ret != -EAGAIN) break;
1041 if (file->f_flags & O_NONBLOCK) break;
1042 /* Doing blocking I/O. Wait here. */
1043 ret = wait_event_interruptible(
1044 fh->wait_data,
1045 pvr2_ioread_avail(fh->rhp) >= 0);
1046 if (ret < 0) break;
1049 return ret;
1053 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1055 unsigned int mask = 0;
1056 struct pvr2_v4l2_fh *fh = file->private_data;
1057 int ret;
1059 if (fh->fw_mode_flag) {
1060 mask |= POLLIN | POLLRDNORM;
1061 return mask;
1064 if (!fh->rhp) {
1065 ret = pvr2_v4l2_iosetup(fh);
1066 if (ret) return POLLERR;
1069 poll_wait(file,&fh->wait_data,wait);
1071 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1072 mask |= POLLIN | POLLRDNORM;
1075 return mask;
1079 static const struct file_operations vdev_fops = {
1080 .owner = THIS_MODULE,
1081 .open = pvr2_v4l2_open,
1082 .release = pvr2_v4l2_release,
1083 .read = pvr2_v4l2_read,
1084 .ioctl = pvr2_v4l2_ioctl,
1085 .llseek = no_llseek,
1086 .poll = pvr2_v4l2_poll,
1090 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1092 static struct video_device vdev_template = {
1093 .owner = THIS_MODULE,
1094 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1095 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1096 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1097 | V4L2_CAP_READWRITE),
1098 .hardware = VID_HARDWARE_PVRUSB2,
1099 .fops = &vdev_fops,
1103 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1104 struct pvr2_v4l2 *vp,
1105 int v4l_type)
1107 int mindevnum;
1108 int unit_number;
1109 int *nr_ptr = 0;
1110 dip->v4lp = vp;
1113 dip->v4l_type = v4l_type;
1114 switch (v4l_type) {
1115 case VFL_TYPE_GRABBER:
1116 dip->stream = &vp->channel.mc_head->video_stream;
1117 dip->config = pvr2_config_mpeg;
1118 dip->minor_type = pvr2_v4l_type_video;
1119 nr_ptr = video_nr;
1120 break;
1121 case VFL_TYPE_VBI:
1122 dip->config = pvr2_config_vbi;
1123 dip->minor_type = pvr2_v4l_type_vbi;
1124 nr_ptr = vbi_nr;
1125 break;
1126 case VFL_TYPE_RADIO:
1127 dip->config = pvr2_config_pcm;
1128 dip->minor_type = pvr2_v4l_type_radio;
1129 nr_ptr = radio_nr;
1130 break;
1131 default:
1132 /* Bail out (this should be impossible) */
1133 err("Failed to set up pvrusb2 v4l dev"
1134 " due to unrecognized config");
1135 return;
1138 /* radio device doesn 't need its own stream */
1139 if (!dip->stream && dip->v4l_type == VFL_TYPE_GRABBER) {
1140 err("Failed to set up pvrusb2 v4l dev"
1141 " due to missing stream instance");
1142 return;
1145 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1146 dip->devbase.release = pvr2_video_device_release;
1148 mindevnum = -1;
1149 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1150 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1151 mindevnum = nr_ptr[unit_number];
1153 if ((video_register_device(&dip->devbase,
1154 dip->v4l_type, mindevnum) < 0) &&
1155 (video_register_device(&dip->devbase,
1156 dip->v4l_type, -1) < 0)) {
1157 err("Failed to register pvrusb2 v4l device");
1160 printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
1161 get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
1162 pvr2_config_get_name(dip->config));
1164 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1165 dip->minor_type,dip->devbase.minor);
1169 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1171 struct pvr2_v4l2 *vp;
1173 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1174 if (!vp) return vp;
1175 memset(vp,0,sizeof(*vp));
1176 vp->dev_video = kmalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1177 vp->dev_radio = kmalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1178 if (!(vp->dev_video && vp->dev_radio)) {
1179 if (vp->dev_video) kfree(vp->dev_video);
1180 if (vp->dev_radio) kfree(vp->dev_radio);
1181 kfree(vp);
1182 return NULL;
1184 memset(vp->dev_video,0,sizeof(*vp->dev_video));
1185 memset(vp->dev_radio,0,sizeof(*vp->dev_radio));
1186 pvr2_channel_init(&vp->channel,mnp);
1187 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1189 vp->channel.check_func = pvr2_v4l2_internal_check;
1191 /* register streams */
1192 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1193 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1195 return vp;
1199 Stuff for Emacs to see, in order to encourage consistent editing style:
1200 *** Local Variables: ***
1201 *** mode: c ***
1202 *** fill-column: 75 ***
1203 *** tab-width: 8 ***
1204 *** c-basic-offset: 8 ***
1205 *** End: ***