V4L/DVB (4664): Pvrusb2: Don't use videodev.h; use v4l2-dev.h in its place
[linux-2.6/kvm.git] / drivers / media / video / pvrusb2 / pvrusb2-v4l2.c
blob49186984a0d783a2b754dd5131b7ed163f5e9bf2
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 enum pvr2_config config;
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;
59 struct pvr2_v4l2 {
60 struct pvr2_channel channel;
61 struct pvr2_v4l2_fh *vfirst;
62 struct pvr2_v4l2_fh *vlast;
64 struct v4l2_prio_state prio;
66 /* streams */
67 struct pvr2_v4l2_dev *vdev;
70 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
71 module_param_array(video_nr, int, NULL, 0444);
72 MODULE_PARM_DESC(video_nr, "Offset for device's minor");
74 static struct v4l2_capability pvr_capability ={
75 .driver = "pvrusb2",
76 .card = "Hauppauge WinTV pvr-usb2",
77 .bus_info = "usb",
78 .version = KERNEL_VERSION(0,8,0),
79 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
80 V4L2_CAP_TUNER | V4L2_CAP_AUDIO |
81 V4L2_CAP_READWRITE),
82 .reserved = {0,0,0,0}
85 static struct v4l2_tuner pvr_v4l2_tuners[]= {
87 .index = 0,
88 .name = "TV Tuner",
89 .type = V4L2_TUNER_ANALOG_TV,
90 .capability = (V4L2_TUNER_CAP_NORM |
91 V4L2_TUNER_CAP_STEREO |
92 V4L2_TUNER_CAP_LANG1 |
93 V4L2_TUNER_CAP_LANG2),
94 .rangelow = 0,
95 .rangehigh = 0,
96 .rxsubchans = V4L2_TUNER_SUB_STEREO,
97 .audmode = V4L2_TUNER_MODE_STEREO,
98 .signal = 0,
99 .afc = 0,
100 .reserved = {0,0,0,0}
104 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
106 .index = 0,
107 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
108 .flags = V4L2_FMT_FLAG_COMPRESSED,
109 .description = "MPEG1/2",
110 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
111 // breaks when I do that.
112 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
113 .reserved = { 0, 0, 0, 0 }
117 #define PVR_FORMAT_PIX 0
118 #define PVR_FORMAT_VBI 1
120 static struct v4l2_format pvr_format [] = {
121 [PVR_FORMAT_PIX] = {
122 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
123 .fmt = {
124 .pix = {
125 .width = 720,
126 .height = 576,
127 // This should really be V4L2_PIX_FMT_MPEG,
128 // but xawtv breaks when I do that.
129 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
130 .field = V4L2_FIELD_INTERLACED,
131 .bytesperline = 0, // doesn't make sense
132 // here
133 //FIXME : Don't know what to put here...
134 .sizeimage = (32*1024),
135 .colorspace = 0, // doesn't make sense here
136 .priv = 0
140 [PVR_FORMAT_VBI] = {
141 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
142 .fmt = {
143 .vbi = {
144 .sampling_rate = 27000000,
145 .offset = 248,
146 .samples_per_line = 1443,
147 .sample_format = V4L2_PIX_FMT_GREY,
148 .start = { 0, 0 },
149 .count = { 0, 0 },
150 .flags = 0,
151 .reserved = { 0, 0 }
158 * pvr_ioctl()
160 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
163 static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
164 unsigned int cmd, void *arg)
166 struct pvr2_v4l2_fh *fh = file->private_data;
167 struct pvr2_v4l2 *vp = fh->vhead;
168 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
169 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
170 int ret = -EINVAL;
172 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
173 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
176 if (!pvr2_hdw_dev_ok(hdw)) {
177 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
178 "ioctl failed - bad or no context");
179 return -EFAULT;
182 /* check priority */
183 switch (cmd) {
184 case VIDIOC_S_CTRL:
185 case VIDIOC_S_STD:
186 case VIDIOC_S_INPUT:
187 case VIDIOC_S_TUNER:
188 case VIDIOC_S_FREQUENCY:
189 ret = v4l2_prio_check(&vp->prio, &fh->prio);
190 if (ret)
191 return ret;
194 switch (cmd) {
195 case VIDIOC_QUERYCAP:
197 struct v4l2_capability *cap = arg;
199 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
201 ret = 0;
202 break;
205 case VIDIOC_G_PRIORITY:
207 enum v4l2_priority *p = arg;
209 *p = v4l2_prio_max(&vp->prio);
210 ret = 0;
211 break;
214 case VIDIOC_S_PRIORITY:
216 enum v4l2_priority *prio = arg;
218 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
219 break;
222 case VIDIOC_ENUMSTD:
224 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
225 int idx = vs->index;
226 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
227 break;
230 case VIDIOC_G_STD:
232 int val = 0;
233 ret = pvr2_ctrl_get_value(
234 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
235 *(v4l2_std_id *)arg = val;
236 break;
239 case VIDIOC_S_STD:
241 ret = pvr2_ctrl_set_value(
242 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
243 *(v4l2_std_id *)arg);
244 break;
247 case VIDIOC_ENUMINPUT:
249 struct pvr2_ctrl *cptr;
250 struct v4l2_input *vi = (struct v4l2_input *)arg;
251 struct v4l2_input tmp;
252 unsigned int cnt;
254 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
256 memset(&tmp,0,sizeof(tmp));
257 tmp.index = vi->index;
258 ret = 0;
259 switch (vi->index) {
260 case PVR2_CVAL_INPUT_TV:
261 case PVR2_CVAL_INPUT_RADIO:
262 tmp.type = V4L2_INPUT_TYPE_TUNER;
263 break;
264 case PVR2_CVAL_INPUT_SVIDEO:
265 case PVR2_CVAL_INPUT_COMPOSITE:
266 tmp.type = V4L2_INPUT_TYPE_CAMERA;
267 break;
268 default:
269 ret = -EINVAL;
270 break;
272 if (ret < 0) break;
274 cnt = 0;
275 pvr2_ctrl_get_valname(cptr,vi->index,
276 tmp.name,sizeof(tmp.name)-1,&cnt);
277 tmp.name[cnt] = 0;
279 /* Don't bother with audioset, since this driver currently
280 always switches the audio whenever the video is
281 switched. */
283 /* Handling std is a tougher problem. It doesn't make
284 sense in cases where a device might be multi-standard.
285 We could just copy out the current value for the
286 standard, but it can change over time. For now just
287 leave it zero. */
289 memcpy(vi, &tmp, sizeof(tmp));
291 ret = 0;
292 break;
295 case VIDIOC_G_INPUT:
297 struct pvr2_ctrl *cptr;
298 struct v4l2_input *vi = (struct v4l2_input *)arg;
299 int val;
300 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
301 val = 0;
302 ret = pvr2_ctrl_get_value(cptr,&val);
303 vi->index = val;
304 break;
307 case VIDIOC_S_INPUT:
309 struct v4l2_input *vi = (struct v4l2_input *)arg;
310 ret = pvr2_ctrl_set_value(
311 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
312 vi->index);
313 break;
316 case VIDIOC_ENUMAUDIO:
318 ret = -EINVAL;
319 break;
322 case VIDIOC_G_AUDIO:
324 ret = -EINVAL;
325 break;
328 case VIDIOC_S_AUDIO:
330 ret = -EINVAL;
331 break;
333 case VIDIOC_G_TUNER:
335 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
336 unsigned int status_mask;
337 int val;
338 if (vt->index !=0) break;
340 status_mask = pvr2_hdw_get_signal_status(hdw);
342 memcpy(vt, &pvr_v4l2_tuners[vt->index],
343 sizeof(struct v4l2_tuner));
345 vt->signal = 0;
346 if (status_mask & PVR2_SIGNAL_OK) {
347 if (status_mask & PVR2_SIGNAL_STEREO) {
348 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
349 } else {
350 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
352 if (status_mask & PVR2_SIGNAL_SAP) {
353 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
354 V4L2_TUNER_SUB_LANG2);
356 vt->signal = 65535;
359 val = 0;
360 ret = pvr2_ctrl_get_value(
361 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
362 &val);
363 vt->audmode = val;
364 break;
367 case VIDIOC_S_TUNER:
369 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
371 if (vt->index != 0)
372 break;
374 ret = pvr2_ctrl_set_value(
375 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
376 vt->audmode);
379 case VIDIOC_S_FREQUENCY:
381 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
382 ret = pvr2_ctrl_set_value(
383 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
384 vf->frequency * 62500);
385 break;
388 case VIDIOC_G_FREQUENCY:
390 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
391 int val = 0;
392 ret = pvr2_ctrl_get_value(
393 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
394 &val);
395 val /= 62500;
396 vf->frequency = val;
397 break;
400 case VIDIOC_ENUM_FMT:
402 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
404 /* Only one format is supported : mpeg.*/
405 if (fd->index != 0)
406 break;
408 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
409 ret = 0;
410 break;
413 case VIDIOC_G_FMT:
415 struct v4l2_format *vf = (struct v4l2_format *)arg;
416 int val;
417 switch(vf->type) {
418 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
419 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
420 sizeof(struct v4l2_format));
421 val = 0;
422 pvr2_ctrl_get_value(
423 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
424 &val);
425 vf->fmt.pix.width = val;
426 val = 0;
427 pvr2_ctrl_get_value(
428 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
429 &val);
430 vf->fmt.pix.height = val;
431 ret = 0;
432 break;
433 case V4L2_BUF_TYPE_VBI_CAPTURE:
434 // ????? Still need to figure out to do VBI correctly
435 ret = -EINVAL;
436 break;
437 default:
438 ret = -EINVAL;
439 break;
441 break;
444 case VIDIOC_TRY_FMT:
445 case VIDIOC_S_FMT:
447 struct v4l2_format *vf = (struct v4l2_format *)arg;
449 ret = 0;
450 switch(vf->type) {
451 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
452 int lmin,lmax;
453 struct pvr2_ctrl *hcp,*vcp;
454 int h = vf->fmt.pix.height;
455 int w = vf->fmt.pix.width;
456 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
457 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
459 lmin = pvr2_ctrl_get_min(hcp);
460 lmax = pvr2_ctrl_get_max(hcp);
461 if (w < lmin) {
462 w = lmin;
463 } else if (w > lmax) {
464 w = lmax;
466 lmin = pvr2_ctrl_get_min(vcp);
467 lmax = pvr2_ctrl_get_max(vcp);
468 if (h < lmin) {
469 h = lmin;
470 } else if (h > lmax) {
471 h = lmax;
474 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
475 sizeof(struct v4l2_format));
476 vf->fmt.pix.width = w;
477 vf->fmt.pix.height = h;
479 if (cmd == VIDIOC_S_FMT) {
480 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
481 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
483 } break;
484 case V4L2_BUF_TYPE_VBI_CAPTURE:
485 // ????? Still need to figure out to do VBI correctly
486 ret = -EINVAL;
487 break;
488 default:
489 ret = -EINVAL;
490 break;
492 break;
495 case VIDIOC_STREAMON:
497 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
498 if (ret < 0) return ret;
499 ret = pvr2_hdw_set_streaming(hdw,!0);
500 break;
503 case VIDIOC_STREAMOFF:
505 ret = pvr2_hdw_set_streaming(hdw,0);
506 break;
509 case VIDIOC_QUERYCTRL:
511 struct pvr2_ctrl *cptr;
512 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
513 ret = 0;
514 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
515 cptr = pvr2_hdw_get_ctrl_nextv4l(
516 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
517 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
518 } else {
519 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
521 if (!cptr) {
522 pvr2_trace(PVR2_TRACE_V4LIOCTL,
523 "QUERYCTRL id=0x%x not implemented here",
524 vc->id);
525 ret = -EINVAL;
526 break;
529 pvr2_trace(PVR2_TRACE_V4LIOCTL,
530 "QUERYCTRL id=0x%x mapping name=%s (%s)",
531 vc->id,pvr2_ctrl_get_name(cptr),
532 pvr2_ctrl_get_desc(cptr));
533 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
534 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
535 vc->default_value = pvr2_ctrl_get_def(cptr);
536 switch (pvr2_ctrl_get_type(cptr)) {
537 case pvr2_ctl_enum:
538 vc->type = V4L2_CTRL_TYPE_MENU;
539 vc->minimum = 0;
540 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
541 vc->step = 1;
542 break;
543 case pvr2_ctl_bool:
544 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
545 vc->minimum = 0;
546 vc->maximum = 1;
547 vc->step = 1;
548 break;
549 case pvr2_ctl_int:
550 vc->type = V4L2_CTRL_TYPE_INTEGER;
551 vc->minimum = pvr2_ctrl_get_min(cptr);
552 vc->maximum = pvr2_ctrl_get_max(cptr);
553 vc->step = 1;
554 break;
555 default:
556 pvr2_trace(PVR2_TRACE_V4LIOCTL,
557 "QUERYCTRL id=0x%x name=%s not mappable",
558 vc->id,pvr2_ctrl_get_name(cptr));
559 ret = -EINVAL;
560 break;
562 break;
565 case VIDIOC_QUERYMENU:
567 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
568 unsigned int cnt = 0;
569 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
570 vm->index,
571 vm->name,sizeof(vm->name)-1,
572 &cnt);
573 vm->name[cnt] = 0;
574 break;
577 case VIDIOC_G_CTRL:
579 struct v4l2_control *vc = (struct v4l2_control *)arg;
580 int val = 0;
581 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
582 &val);
583 vc->value = val;
584 break;
587 case VIDIOC_S_CTRL:
589 struct v4l2_control *vc = (struct v4l2_control *)arg;
590 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
591 vc->value);
592 break;
595 case VIDIOC_G_EXT_CTRLS:
597 struct v4l2_ext_controls *ctls =
598 (struct v4l2_ext_controls *)arg;
599 struct v4l2_ext_control *ctrl;
600 unsigned int idx;
601 int val;
602 for (idx = 0; idx < ctls->count; idx++) {
603 ctrl = ctls->controls + idx;
604 ret = pvr2_ctrl_get_value(
605 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
606 if (ret) {
607 ctls->error_idx = idx;
608 break;
610 /* Ensure that if read as a 64 bit value, the user
611 will still get a hopefully sane value */
612 ctrl->value64 = 0;
613 ctrl->value = val;
615 break;
618 case VIDIOC_S_EXT_CTRLS:
620 struct v4l2_ext_controls *ctls =
621 (struct v4l2_ext_controls *)arg;
622 struct v4l2_ext_control *ctrl;
623 unsigned int idx;
624 for (idx = 0; idx < ctls->count; idx++) {
625 ctrl = ctls->controls + idx;
626 ret = pvr2_ctrl_set_value(
627 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
628 ctrl->value);
629 if (ret) {
630 ctls->error_idx = idx;
631 break;
634 break;
637 case VIDIOC_TRY_EXT_CTRLS:
639 struct v4l2_ext_controls *ctls =
640 (struct v4l2_ext_controls *)arg;
641 struct v4l2_ext_control *ctrl;
642 struct pvr2_ctrl *pctl;
643 unsigned int idx;
644 /* For the moment just validate that the requested control
645 actually exists. */
646 for (idx = 0; idx < ctls->count; idx++) {
647 ctrl = ctls->controls + idx;
648 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
649 if (!pctl) {
650 ret = -EINVAL;
651 ctls->error_idx = idx;
652 break;
655 break;
658 case VIDIOC_LOG_STATUS:
660 pvr2_hdw_trigger_module_log(hdw);
661 ret = 0;
662 break;
664 #ifdef CONFIG_VIDEO_ADV_DEBUG
665 case VIDIOC_INT_G_REGISTER:
666 case VIDIOC_INT_S_REGISTER:
668 u32 val;
669 struct v4l2_register *req = (struct v4l2_register *)arg;
670 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
671 ret = pvr2_hdw_register_access(
672 hdw,req->i2c_id,req->reg,
673 cmd == VIDIOC_INT_S_REGISTER,&val);
674 if (cmd == 0) req->val = val;
675 break;
677 #endif
679 default :
680 ret = v4l_compat_translate_ioctl(inode,file,cmd,
681 arg,pvr2_v4l2_do_ioctl);
684 pvr2_hdw_commit_ctl(hdw);
686 if (ret < 0) {
687 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
688 pvr2_trace(PVR2_TRACE_V4LIOCTL,
689 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
690 } else {
691 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
692 pvr2_trace(PVR2_TRACE_V4LIOCTL,
693 "pvr2_v4l2_do_ioctl failure, ret=%d"
694 " command was:",ret);
695 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
696 cmd);
699 } else {
700 pvr2_trace(PVR2_TRACE_V4LIOCTL,
701 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
702 ret,ret);
704 return ret;
708 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
710 printk(KERN_INFO "pvrusb2: unregistering device video%d [%s]\n",
711 dip->devbase.minor,pvr2_config_get_name(dip->config));
713 /* Paranoia */
714 dip->v4lp = 0;
715 dip->stream = 0;
717 /* Actual deallocation happens later when all internal references
718 are gone. */
719 video_unregister_device(&dip->devbase);
723 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
725 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,-1);
726 pvr2_v4l2_dev_destroy(vp->vdev);
728 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
729 pvr2_channel_done(&vp->channel);
730 kfree(vp);
734 static void pvr2_video_device_release(struct video_device *vdev)
736 struct pvr2_v4l2_dev *dev;
737 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
738 kfree(dev);
742 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
744 struct pvr2_v4l2 *vp;
745 vp = container_of(chp,struct pvr2_v4l2,channel);
746 if (!vp->channel.mc_head->disconnect_flag) return;
747 if (vp->vfirst) return;
748 pvr2_v4l2_destroy_no_lock(vp);
752 static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
753 unsigned int cmd, unsigned long arg)
756 /* Temporary hack : use ivtv api until a v4l2 one is available. */
757 #define IVTV_IOC_G_CODEC 0xFFEE7703
758 #define IVTV_IOC_S_CODEC 0xFFEE7704
759 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
760 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
764 static int pvr2_v4l2_release(struct inode *inode, struct file *file)
766 struct pvr2_v4l2_fh *fhp = file->private_data;
767 struct pvr2_v4l2 *vp = fhp->vhead;
768 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
770 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
772 if (fhp->rhp) {
773 struct pvr2_stream *sp;
774 struct pvr2_hdw *hdw;
775 hdw = fhp->channel.mc_head->hdw;
776 pvr2_hdw_set_streaming(hdw,0);
777 sp = pvr2_ioread_get_stream(fhp->rhp);
778 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
779 pvr2_ioread_destroy(fhp->rhp);
780 fhp->rhp = NULL;
782 v4l2_prio_close(&vp->prio, &fhp->prio);
783 file->private_data = NULL;
785 pvr2_context_enter(mp); do {
786 if (fhp->vnext) {
787 fhp->vnext->vprev = fhp->vprev;
788 } else {
789 vp->vlast = fhp->vprev;
791 if (fhp->vprev) {
792 fhp->vprev->vnext = fhp->vnext;
793 } else {
794 vp->vfirst = fhp->vnext;
796 fhp->vnext = NULL;
797 fhp->vprev = NULL;
798 fhp->vhead = NULL;
799 pvr2_channel_done(&fhp->channel);
800 pvr2_trace(PVR2_TRACE_STRUCT,
801 "Destroying pvr_v4l2_fh id=%p",fhp);
802 kfree(fhp);
803 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
804 pvr2_v4l2_destroy_no_lock(vp);
806 } while (0); pvr2_context_exit(mp);
807 return 0;
811 static int pvr2_v4l2_open(struct inode *inode, struct file *file)
813 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
814 struct pvr2_v4l2_fh *fhp;
815 struct pvr2_v4l2 *vp;
816 struct pvr2_hdw *hdw;
818 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
820 vp = dip->v4lp;
821 hdw = vp->channel.hdw;
823 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
825 if (!pvr2_hdw_dev_ok(hdw)) {
826 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
827 "pvr2_v4l2_open: hardware not ready");
828 return -EIO;
831 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
832 if (!fhp) {
833 return -ENOMEM;
835 memset(fhp,0,sizeof(*fhp));
837 init_waitqueue_head(&fhp->wait_data);
838 fhp->dev_info = dip;
840 pvr2_context_enter(vp->channel.mc_head); do {
841 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
842 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
843 fhp->vnext = NULL;
844 fhp->vprev = vp->vlast;
845 if (vp->vlast) {
846 vp->vlast->vnext = fhp;
847 } else {
848 vp->vfirst = fhp;
850 vp->vlast = fhp;
851 fhp->vhead = vp;
852 } while (0); pvr2_context_exit(vp->channel.mc_head);
854 fhp->file = file;
855 file->private_data = fhp;
856 v4l2_prio_open(&vp->prio,&fhp->prio);
858 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
860 return 0;
864 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
866 wake_up(&fhp->wait_data);
869 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
871 int ret;
872 struct pvr2_stream *sp;
873 struct pvr2_hdw *hdw;
874 if (fh->rhp) return 0;
876 /* First read() attempt. Try to claim the stream and start
877 it... */
878 if ((ret = pvr2_channel_claim_stream(&fh->channel,
879 fh->dev_info->stream)) != 0) {
880 /* Someone else must already have it */
881 return ret;
884 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
885 if (!fh->rhp) {
886 pvr2_channel_claim_stream(&fh->channel,NULL);
887 return -ENOMEM;
890 hdw = fh->channel.mc_head->hdw;
891 sp = fh->dev_info->stream->stream;
892 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
893 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
894 pvr2_hdw_set_streaming(hdw,!0);
895 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
897 return ret;
901 static ssize_t pvr2_v4l2_read(struct file *file,
902 char __user *buff, size_t count, loff_t *ppos)
904 struct pvr2_v4l2_fh *fh = file->private_data;
905 int ret;
907 if (fh->fw_mode_flag) {
908 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
909 char *tbuf;
910 int c1,c2;
911 int tcnt = 0;
912 unsigned int offs = *ppos;
914 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
915 if (!tbuf) return -ENOMEM;
917 while (count) {
918 c1 = count;
919 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
920 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
921 if (c2 < 0) {
922 tcnt = c2;
923 break;
925 if (!c2) break;
926 if (copy_to_user(buff,tbuf,c2)) {
927 tcnt = -EFAULT;
928 break;
930 offs += c2;
931 tcnt += c2;
932 buff += c2;
933 count -= c2;
934 *ppos += c2;
936 kfree(tbuf);
937 return tcnt;
940 if (!fh->rhp) {
941 ret = pvr2_v4l2_iosetup(fh);
942 if (ret) {
943 return ret;
947 for (;;) {
948 ret = pvr2_ioread_read(fh->rhp,buff,count);
949 if (ret >= 0) break;
950 if (ret != -EAGAIN) break;
951 if (file->f_flags & O_NONBLOCK) break;
952 /* Doing blocking I/O. Wait here. */
953 ret = wait_event_interruptible(
954 fh->wait_data,
955 pvr2_ioread_avail(fh->rhp) >= 0);
956 if (ret < 0) break;
959 return ret;
963 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
965 unsigned int mask = 0;
966 struct pvr2_v4l2_fh *fh = file->private_data;
967 int ret;
969 if (fh->fw_mode_flag) {
970 mask |= POLLIN | POLLRDNORM;
971 return mask;
974 if (!fh->rhp) {
975 ret = pvr2_v4l2_iosetup(fh);
976 if (ret) return POLLERR;
979 poll_wait(file,&fh->wait_data,wait);
981 if (pvr2_ioread_avail(fh->rhp) >= 0) {
982 mask |= POLLIN | POLLRDNORM;
985 return mask;
989 static struct file_operations vdev_fops = {
990 .owner = THIS_MODULE,
991 .open = pvr2_v4l2_open,
992 .release = pvr2_v4l2_release,
993 .read = pvr2_v4l2_read,
994 .ioctl = pvr2_v4l2_ioctl,
995 .llseek = no_llseek,
996 .poll = pvr2_v4l2_poll,
1000 #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1002 static struct video_device vdev_template = {
1003 .owner = THIS_MODULE,
1004 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1005 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1006 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1007 | V4L2_CAP_READWRITE),
1008 .hardware = VID_HARDWARE_PVRUSB2,
1009 .fops = &vdev_fops,
1013 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1014 struct pvr2_v4l2 *vp,
1015 enum pvr2_config cfg)
1017 int mindevnum;
1018 int unit_number;
1019 int v4l_type;
1020 dip->v4lp = vp;
1021 dip->config = cfg;
1024 switch (cfg) {
1025 case pvr2_config_mpeg:
1026 v4l_type = VFL_TYPE_GRABBER;
1027 dip->stream = &vp->channel.mc_head->video_stream;
1028 break;
1029 case pvr2_config_vbi:
1030 v4l_type = VFL_TYPE_VBI;
1031 break;
1032 case pvr2_config_radio:
1033 v4l_type = VFL_TYPE_RADIO;
1034 break;
1035 default:
1036 /* Bail out (this should be impossible) */
1037 err("Failed to set up pvrusb2 v4l dev"
1038 " due to unrecognized config");
1039 return;
1042 if (!dip->stream) {
1043 err("Failed to set up pvrusb2 v4l dev"
1044 " due to missing stream instance");
1045 return;
1048 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1049 dip->devbase.release = pvr2_video_device_release;
1051 mindevnum = -1;
1052 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1053 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1054 mindevnum = video_nr[unit_number];
1056 if ((video_register_device(&dip->devbase, v4l_type, mindevnum) < 0) &&
1057 (video_register_device(&dip->devbase, v4l_type, -1) < 0)) {
1058 err("Failed to register pvrusb2 v4l video device");
1059 } else {
1060 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
1061 dip->devbase.minor,pvr2_config_get_name(dip->config));
1064 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1065 dip->devbase.minor);
1069 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1071 struct pvr2_v4l2 *vp;
1073 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1074 if (!vp) return vp;
1075 memset(vp,0,sizeof(*vp));
1076 vp->vdev = kmalloc(sizeof(*vp->vdev),GFP_KERNEL);
1077 if (!vp->vdev) {
1078 kfree(vp);
1079 return 0;
1081 memset(vp->vdev,0,sizeof(*vp->vdev));
1082 pvr2_channel_init(&vp->channel,mnp);
1083 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1085 vp->channel.check_func = pvr2_v4l2_internal_check;
1087 /* register streams */
1088 pvr2_v4l2_dev_init(vp->vdev,vp,pvr2_config_mpeg);
1090 return vp;
1094 Stuff for Emacs to see, in order to encourage consistent editing style:
1095 *** Local Variables: ***
1096 *** mode: c ***
1097 *** fill-column: 75 ***
1098 *** tab-width: 8 ***
1099 *** c-basic-offset: 8 ***
1100 *** End: ***