Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / media / video / pvrusb2 / pvrusb2-v4l2.c
blobe27f8ab76966ccdebd9f1a11338b0006ea759a84
1 /*
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/slab.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 #include <media/v4l2-ioctl.h>
36 struct pvr2_v4l2_dev;
37 struct pvr2_v4l2_fh;
38 struct pvr2_v4l2;
40 struct pvr2_v4l2_dev {
41 struct video_device devbase; /* MUST be first! */
42 struct pvr2_v4l2 *v4lp;
43 struct pvr2_context_stream *stream;
44 /* Information about this device: */
45 enum pvr2_config config; /* Expected stream format */
46 int v4l_type; /* V4L defined type for this device node */
47 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
50 struct pvr2_v4l2_fh {
51 struct pvr2_channel channel;
52 struct pvr2_v4l2_dev *pdi;
53 enum v4l2_priority prio;
54 struct pvr2_ioread *rhp;
55 struct file *file;
56 struct pvr2_v4l2 *vhead;
57 struct pvr2_v4l2_fh *vnext;
58 struct pvr2_v4l2_fh *vprev;
59 wait_queue_head_t wait_data;
60 int fw_mode_flag;
61 /* Map contiguous ordinal value to input id */
62 unsigned char *input_map;
63 unsigned int input_cnt;
66 struct pvr2_v4l2 {
67 struct pvr2_channel channel;
68 struct pvr2_v4l2_fh *vfirst;
69 struct pvr2_v4l2_fh *vlast;
71 struct v4l2_prio_state prio;
73 /* streams - Note that these must be separately, individually,
74 * allocated pointers. This is because the v4l core is going to
75 * manage their deletion - separately, individually... */
76 struct pvr2_v4l2_dev *dev_video;
77 struct pvr2_v4l2_dev *dev_radio;
80 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
81 module_param_array(video_nr, int, NULL, 0444);
82 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
83 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
84 module_param_array(radio_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
86 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
87 module_param_array(vbi_nr, int, NULL, 0444);
88 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
90 static struct v4l2_capability pvr_capability ={
91 .driver = "pvrusb2",
92 .card = "Hauppauge WinTV pvr-usb2",
93 .bus_info = "usb",
94 .version = LINUX_VERSION_CODE,
95 .capabilities = (V4L2_CAP_VIDEO_CAPTURE |
96 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
97 V4L2_CAP_READWRITE),
98 .reserved = {0,0,0,0}
101 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
103 .index = 0,
104 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
105 .flags = V4L2_FMT_FLAG_COMPRESSED,
106 .description = "MPEG1/2",
107 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
108 // breaks when I do that.
109 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
110 .reserved = { 0, 0, 0, 0 }
114 #define PVR_FORMAT_PIX 0
115 #define PVR_FORMAT_VBI 1
117 static struct v4l2_format pvr_format [] = {
118 [PVR_FORMAT_PIX] = {
119 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
120 .fmt = {
121 .pix = {
122 .width = 720,
123 .height = 576,
124 // This should really be V4L2_PIX_FMT_MPEG,
125 // but xawtv breaks when I do that.
126 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
127 .field = V4L2_FIELD_INTERLACED,
128 .bytesperline = 0, // doesn't make sense
129 // here
130 //FIXME : Don't know what to put here...
131 .sizeimage = (32*1024),
132 .colorspace = 0, // doesn't make sense here
133 .priv = 0
137 [PVR_FORMAT_VBI] = {
138 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
139 .fmt = {
140 .vbi = {
141 .sampling_rate = 27000000,
142 .offset = 248,
143 .samples_per_line = 1443,
144 .sample_format = V4L2_PIX_FMT_GREY,
145 .start = { 0, 0 },
146 .count = { 0, 0 },
147 .flags = 0,
148 .reserved = { 0, 0 }
156 * pvr_ioctl()
158 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
161 static long pvr2_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
163 struct pvr2_v4l2_fh *fh = file->private_data;
164 struct pvr2_v4l2 *vp = fh->vhead;
165 struct pvr2_v4l2_dev *pdi = fh->pdi;
166 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
167 long ret = -EINVAL;
169 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
170 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
173 if (!pvr2_hdw_dev_ok(hdw)) {
174 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
175 "ioctl failed - bad or no context");
176 return -EFAULT;
179 /* check priority */
180 switch (cmd) {
181 case VIDIOC_S_CTRL:
182 case VIDIOC_S_STD:
183 case VIDIOC_S_INPUT:
184 case VIDIOC_S_TUNER:
185 case VIDIOC_S_FREQUENCY:
186 ret = v4l2_prio_check(&vp->prio, fh->prio);
187 if (ret)
188 return ret;
191 switch (cmd) {
192 case VIDIOC_QUERYCAP:
194 struct v4l2_capability *cap = arg;
196 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
197 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
198 sizeof(cap->bus_info));
199 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
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;
253 int val;
255 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
257 memset(&tmp,0,sizeof(tmp));
258 tmp.index = vi->index;
259 ret = 0;
260 if (vi->index >= fh->input_cnt) {
261 ret = -EINVAL;
262 break;
264 val = fh->input_map[vi->index];
265 switch (val) {
266 case PVR2_CVAL_INPUT_TV:
267 case PVR2_CVAL_INPUT_DTV:
268 case PVR2_CVAL_INPUT_RADIO:
269 tmp.type = V4L2_INPUT_TYPE_TUNER;
270 break;
271 case PVR2_CVAL_INPUT_SVIDEO:
272 case PVR2_CVAL_INPUT_COMPOSITE:
273 tmp.type = V4L2_INPUT_TYPE_CAMERA;
274 break;
275 default:
276 ret = -EINVAL;
277 break;
279 if (ret < 0) break;
281 cnt = 0;
282 pvr2_ctrl_get_valname(cptr,val,
283 tmp.name,sizeof(tmp.name)-1,&cnt);
284 tmp.name[cnt] = 0;
286 /* Don't bother with audioset, since this driver currently
287 always switches the audio whenever the video is
288 switched. */
290 /* Handling std is a tougher problem. It doesn't make
291 sense in cases where a device might be multi-standard.
292 We could just copy out the current value for the
293 standard, but it can change over time. For now just
294 leave it zero. */
296 memcpy(vi, &tmp, sizeof(tmp));
298 ret = 0;
299 break;
302 case VIDIOC_G_INPUT:
304 unsigned int idx;
305 struct pvr2_ctrl *cptr;
306 struct v4l2_input *vi = (struct v4l2_input *)arg;
307 int val;
308 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
309 val = 0;
310 ret = pvr2_ctrl_get_value(cptr,&val);
311 vi->index = 0;
312 for (idx = 0; idx < fh->input_cnt; idx++) {
313 if (fh->input_map[idx] == val) {
314 vi->index = idx;
315 break;
318 break;
321 case VIDIOC_S_INPUT:
323 struct v4l2_input *vi = (struct v4l2_input *)arg;
324 if (vi->index >= fh->input_cnt) {
325 ret = -ERANGE;
326 break;
328 ret = pvr2_ctrl_set_value(
329 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
330 fh->input_map[vi->index]);
331 break;
334 case VIDIOC_ENUMAUDIO:
336 /* pkt: FIXME: We are returning one "fake" input here
337 which could very well be called "whatever_we_like".
338 This is for apps that want to see an audio input
339 just to feel comfortable, as well as to test if
340 it can do stereo or sth. There is actually no guarantee
341 that the actual audio input cannot change behind the app's
342 back, but most applications should not mind that either.
344 Hopefully, mplayer people will work with us on this (this
345 whole mess is to support mplayer pvr://), or Hans will come
346 up with a more standard way to say "we have inputs but we
347 don 't want you to change them independent of video" which
348 will sort this mess.
350 struct v4l2_audio *vin = arg;
351 ret = -EINVAL;
352 if (vin->index > 0) break;
353 strncpy(vin->name, "PVRUSB2 Audio",14);
354 vin->capability = V4L2_AUDCAP_STEREO;
355 ret = 0;
356 break;
357 break;
360 case VIDIOC_G_AUDIO:
362 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
363 struct v4l2_audio *vin = arg;
364 memset(vin,0,sizeof(*vin));
365 vin->index = 0;
366 strncpy(vin->name, "PVRUSB2 Audio",14);
367 vin->capability = V4L2_AUDCAP_STEREO;
368 ret = 0;
369 break;
372 case VIDIOC_G_TUNER:
374 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
376 if (vt->index != 0) break; /* Only answer for the 1st tuner */
378 pvr2_hdw_execute_tuner_poll(hdw);
379 ret = pvr2_hdw_get_tuner_status(hdw,vt);
380 break;
383 case VIDIOC_S_TUNER:
385 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
387 if (vt->index != 0)
388 break;
390 ret = pvr2_ctrl_set_value(
391 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
392 vt->audmode);
393 break;
396 case VIDIOC_S_FREQUENCY:
398 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
399 unsigned long fv;
400 struct v4l2_tuner vt;
401 int cur_input;
402 struct pvr2_ctrl *ctrlp;
403 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
404 if (ret != 0) break;
405 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
406 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
407 if (ret != 0) break;
408 if (vf->type == V4L2_TUNER_RADIO) {
409 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
410 pvr2_ctrl_set_value(ctrlp,
411 PVR2_CVAL_INPUT_RADIO);
413 } else {
414 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
415 pvr2_ctrl_set_value(ctrlp,
416 PVR2_CVAL_INPUT_TV);
419 fv = vf->frequency;
420 if (vt.capability & V4L2_TUNER_CAP_LOW) {
421 fv = (fv * 125) / 2;
422 } else {
423 fv = fv * 62500;
425 ret = pvr2_ctrl_set_value(
426 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
427 break;
430 case VIDIOC_G_FREQUENCY:
432 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
433 int val = 0;
434 int cur_input;
435 struct v4l2_tuner vt;
436 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
437 if (ret != 0) break;
438 ret = pvr2_ctrl_get_value(
439 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
440 &val);
441 if (ret != 0) break;
442 pvr2_ctrl_get_value(
443 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
444 &cur_input);
445 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
446 vf->type = V4L2_TUNER_RADIO;
447 } else {
448 vf->type = V4L2_TUNER_ANALOG_TV;
450 if (vt.capability & V4L2_TUNER_CAP_LOW) {
451 val = (val * 2) / 125;
452 } else {
453 val /= 62500;
455 vf->frequency = val;
456 break;
459 case VIDIOC_ENUM_FMT:
461 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
463 /* Only one format is supported : mpeg.*/
464 if (fd->index != 0)
465 break;
467 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
468 ret = 0;
469 break;
472 case VIDIOC_G_FMT:
474 struct v4l2_format *vf = (struct v4l2_format *)arg;
475 int val;
476 switch(vf->type) {
477 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
478 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
479 sizeof(struct v4l2_format));
480 val = 0;
481 pvr2_ctrl_get_value(
482 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
483 &val);
484 vf->fmt.pix.width = val;
485 val = 0;
486 pvr2_ctrl_get_value(
487 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
488 &val);
489 vf->fmt.pix.height = val;
490 ret = 0;
491 break;
492 case V4L2_BUF_TYPE_VBI_CAPTURE:
493 // ????? Still need to figure out to do VBI correctly
494 ret = -EINVAL;
495 break;
496 default:
497 ret = -EINVAL;
498 break;
500 break;
503 case VIDIOC_TRY_FMT:
504 case VIDIOC_S_FMT:
506 struct v4l2_format *vf = (struct v4l2_format *)arg;
508 ret = 0;
509 switch(vf->type) {
510 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
511 int lmin,lmax,ldef;
512 struct pvr2_ctrl *hcp,*vcp;
513 int h = vf->fmt.pix.height;
514 int w = vf->fmt.pix.width;
515 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
516 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
518 lmin = pvr2_ctrl_get_min(hcp);
519 lmax = pvr2_ctrl_get_max(hcp);
520 pvr2_ctrl_get_def(hcp, &ldef);
521 if (w == -1) {
522 w = ldef;
523 } else if (w < lmin) {
524 w = lmin;
525 } else if (w > lmax) {
526 w = lmax;
528 lmin = pvr2_ctrl_get_min(vcp);
529 lmax = pvr2_ctrl_get_max(vcp);
530 pvr2_ctrl_get_def(vcp, &ldef);
531 if (h == -1) {
532 h = ldef;
533 } else if (h < lmin) {
534 h = lmin;
535 } else if (h > lmax) {
536 h = lmax;
539 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
540 sizeof(struct v4l2_format));
541 vf->fmt.pix.width = w;
542 vf->fmt.pix.height = h;
544 if (cmd == VIDIOC_S_FMT) {
545 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
546 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
548 } break;
549 case V4L2_BUF_TYPE_VBI_CAPTURE:
550 // ????? Still need to figure out to do VBI correctly
551 ret = -EINVAL;
552 break;
553 default:
554 ret = -EINVAL;
555 break;
557 break;
560 case VIDIOC_STREAMON:
562 if (!fh->pdi->stream) {
563 /* No stream defined for this node. This means
564 that we're not currently allowed to stream from
565 this node. */
566 ret = -EPERM;
567 break;
569 ret = pvr2_hdw_set_stream_type(hdw,pdi->config);
570 if (ret < 0) return ret;
571 ret = pvr2_hdw_set_streaming(hdw,!0);
572 break;
575 case VIDIOC_STREAMOFF:
577 if (!fh->pdi->stream) {
578 /* No stream defined for this node. This means
579 that we're not currently allowed to stream from
580 this node. */
581 ret = -EPERM;
582 break;
584 ret = pvr2_hdw_set_streaming(hdw,0);
585 break;
588 case VIDIOC_QUERYCTRL:
590 struct pvr2_ctrl *cptr;
591 int val;
592 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
593 ret = 0;
594 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
595 cptr = pvr2_hdw_get_ctrl_nextv4l(
596 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
597 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
598 } else {
599 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
601 if (!cptr) {
602 pvr2_trace(PVR2_TRACE_V4LIOCTL,
603 "QUERYCTRL id=0x%x not implemented here",
604 vc->id);
605 ret = -EINVAL;
606 break;
609 pvr2_trace(PVR2_TRACE_V4LIOCTL,
610 "QUERYCTRL id=0x%x mapping name=%s (%s)",
611 vc->id,pvr2_ctrl_get_name(cptr),
612 pvr2_ctrl_get_desc(cptr));
613 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
614 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
615 pvr2_ctrl_get_def(cptr, &val);
616 vc->default_value = val;
617 switch (pvr2_ctrl_get_type(cptr)) {
618 case pvr2_ctl_enum:
619 vc->type = V4L2_CTRL_TYPE_MENU;
620 vc->minimum = 0;
621 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
622 vc->step = 1;
623 break;
624 case pvr2_ctl_bool:
625 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
626 vc->minimum = 0;
627 vc->maximum = 1;
628 vc->step = 1;
629 break;
630 case pvr2_ctl_int:
631 vc->type = V4L2_CTRL_TYPE_INTEGER;
632 vc->minimum = pvr2_ctrl_get_min(cptr);
633 vc->maximum = pvr2_ctrl_get_max(cptr);
634 vc->step = 1;
635 break;
636 default:
637 pvr2_trace(PVR2_TRACE_V4LIOCTL,
638 "QUERYCTRL id=0x%x name=%s not mappable",
639 vc->id,pvr2_ctrl_get_name(cptr));
640 ret = -EINVAL;
641 break;
643 break;
646 case VIDIOC_QUERYMENU:
648 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
649 unsigned int cnt = 0;
650 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
651 vm->index,
652 vm->name,sizeof(vm->name)-1,
653 &cnt);
654 vm->name[cnt] = 0;
655 break;
658 case VIDIOC_G_CTRL:
660 struct v4l2_control *vc = (struct v4l2_control *)arg;
661 int val = 0;
662 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
663 &val);
664 vc->value = val;
665 break;
668 case VIDIOC_S_CTRL:
670 struct v4l2_control *vc = (struct v4l2_control *)arg;
671 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
672 vc->value);
673 break;
676 case VIDIOC_G_EXT_CTRLS:
678 struct v4l2_ext_controls *ctls =
679 (struct v4l2_ext_controls *)arg;
680 struct v4l2_ext_control *ctrl;
681 unsigned int idx;
682 int val;
683 ret = 0;
684 for (idx = 0; idx < ctls->count; idx++) {
685 ctrl = ctls->controls + idx;
686 ret = pvr2_ctrl_get_value(
687 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
688 if (ret) {
689 ctls->error_idx = idx;
690 break;
692 /* Ensure that if read as a 64 bit value, the user
693 will still get a hopefully sane value */
694 ctrl->value64 = 0;
695 ctrl->value = val;
697 break;
700 case VIDIOC_S_EXT_CTRLS:
702 struct v4l2_ext_controls *ctls =
703 (struct v4l2_ext_controls *)arg;
704 struct v4l2_ext_control *ctrl;
705 unsigned int idx;
706 ret = 0;
707 for (idx = 0; idx < ctls->count; idx++) {
708 ctrl = ctls->controls + idx;
709 ret = pvr2_ctrl_set_value(
710 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
711 ctrl->value);
712 if (ret) {
713 ctls->error_idx = idx;
714 break;
717 break;
720 case VIDIOC_TRY_EXT_CTRLS:
722 struct v4l2_ext_controls *ctls =
723 (struct v4l2_ext_controls *)arg;
724 struct v4l2_ext_control *ctrl;
725 struct pvr2_ctrl *pctl;
726 unsigned int idx;
727 /* For the moment just validate that the requested control
728 actually exists. */
729 ret = 0;
730 for (idx = 0; idx < ctls->count; idx++) {
731 ctrl = ctls->controls + idx;
732 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
733 if (!pctl) {
734 ret = -EINVAL;
735 ctls->error_idx = idx;
736 break;
739 break;
742 case VIDIOC_CROPCAP:
744 struct v4l2_cropcap *cap = (struct v4l2_cropcap *)arg;
745 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
746 ret = -EINVAL;
747 break;
749 ret = pvr2_hdw_get_cropcap(hdw, cap);
750 cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
751 break;
753 case VIDIOC_G_CROP:
755 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
756 int val = 0;
757 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
758 ret = -EINVAL;
759 break;
761 ret = pvr2_ctrl_get_value(
762 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
763 if (ret != 0) {
764 ret = -EINVAL;
765 break;
767 crop->c.left = val;
768 ret = pvr2_ctrl_get_value(
769 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
770 if (ret != 0) {
771 ret = -EINVAL;
772 break;
774 crop->c.top = val;
775 ret = pvr2_ctrl_get_value(
776 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
777 if (ret != 0) {
778 ret = -EINVAL;
779 break;
781 crop->c.width = val;
782 ret = pvr2_ctrl_get_value(
783 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
784 if (ret != 0) {
785 ret = -EINVAL;
786 break;
788 crop->c.height = val;
790 case VIDIOC_S_CROP:
792 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
793 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
794 ret = -EINVAL;
795 break;
797 ret = pvr2_ctrl_set_value(
798 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
799 crop->c.left);
800 if (ret != 0) {
801 ret = -EINVAL;
802 break;
804 ret = pvr2_ctrl_set_value(
805 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
806 crop->c.top);
807 if (ret != 0) {
808 ret = -EINVAL;
809 break;
811 ret = pvr2_ctrl_set_value(
812 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
813 crop->c.width);
814 if (ret != 0) {
815 ret = -EINVAL;
816 break;
818 ret = pvr2_ctrl_set_value(
819 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
820 crop->c.height);
821 if (ret != 0) {
822 ret = -EINVAL;
823 break;
826 case VIDIOC_LOG_STATUS:
828 pvr2_hdw_trigger_module_log(hdw);
829 ret = 0;
830 break;
832 #ifdef CONFIG_VIDEO_ADV_DEBUG
833 case VIDIOC_DBG_S_REGISTER:
834 case VIDIOC_DBG_G_REGISTER:
836 u64 val;
837 struct v4l2_dbg_register *req = (struct v4l2_dbg_register *)arg;
838 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
839 ret = pvr2_hdw_register_access(
840 hdw, &req->match, req->reg,
841 cmd == VIDIOC_DBG_S_REGISTER, &val);
842 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
843 break;
845 #endif
847 default :
848 ret = -ENOTTY;
849 break;
852 pvr2_hdw_commit_ctl(hdw);
854 if (ret < 0) {
855 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
856 pvr2_trace(PVR2_TRACE_V4LIOCTL,
857 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret);
858 } else {
859 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
860 pvr2_trace(PVR2_TRACE_V4LIOCTL,
861 "pvr2_v4l2_do_ioctl failure, ret=%ld"
862 " command was:", ret);
863 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
864 cmd);
867 } else {
868 pvr2_trace(PVR2_TRACE_V4LIOCTL,
869 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
870 ret, ret);
872 return ret;
875 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
877 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
878 enum pvr2_config cfg = dip->config;
879 char msg[80];
880 unsigned int mcnt;
882 /* Construct the unregistration message *before* we actually
883 perform the unregistration step. By doing it this way we don't
884 have to worry about potentially touching deleted resources. */
885 mcnt = scnprintf(msg, sizeof(msg) - 1,
886 "pvrusb2: unregistered device %s [%s]",
887 video_device_node_name(&dip->devbase),
888 pvr2_config_get_name(cfg));
889 msg[mcnt] = 0;
891 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
893 /* Paranoia */
894 dip->v4lp = NULL;
895 dip->stream = NULL;
897 /* Actual deallocation happens later when all internal references
898 are gone. */
899 video_unregister_device(&dip->devbase);
901 printk(KERN_INFO "%s\n", msg);
906 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
908 if (!dip) return;
909 if (!dip->devbase.parent) return;
910 dip->devbase.parent = NULL;
911 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
915 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
917 if (vp->dev_video) {
918 pvr2_v4l2_dev_destroy(vp->dev_video);
919 vp->dev_video = NULL;
921 if (vp->dev_radio) {
922 pvr2_v4l2_dev_destroy(vp->dev_radio);
923 vp->dev_radio = NULL;
926 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
927 pvr2_channel_done(&vp->channel);
928 kfree(vp);
932 static void pvr2_video_device_release(struct video_device *vdev)
934 struct pvr2_v4l2_dev *dev;
935 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
936 kfree(dev);
940 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
942 struct pvr2_v4l2 *vp;
943 vp = container_of(chp,struct pvr2_v4l2,channel);
944 if (!vp->channel.mc_head->disconnect_flag) return;
945 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
946 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
947 if (vp->vfirst) return;
948 pvr2_v4l2_destroy_no_lock(vp);
952 static long pvr2_v4l2_ioctl(struct file *file,
953 unsigned int cmd, unsigned long arg)
956 return video_usercopy(file, cmd, arg, pvr2_v4l2_do_ioctl);
960 static int pvr2_v4l2_release(struct file *file)
962 struct pvr2_v4l2_fh *fhp = file->private_data;
963 struct pvr2_v4l2 *vp = fhp->vhead;
964 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
966 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
968 if (fhp->rhp) {
969 struct pvr2_stream *sp;
970 pvr2_hdw_set_streaming(hdw,0);
971 sp = pvr2_ioread_get_stream(fhp->rhp);
972 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
973 pvr2_ioread_destroy(fhp->rhp);
974 fhp->rhp = NULL;
977 v4l2_prio_close(&vp->prio, fhp->prio);
978 file->private_data = NULL;
980 if (fhp->vnext) {
981 fhp->vnext->vprev = fhp->vprev;
982 } else {
983 vp->vlast = fhp->vprev;
985 if (fhp->vprev) {
986 fhp->vprev->vnext = fhp->vnext;
987 } else {
988 vp->vfirst = fhp->vnext;
990 fhp->vnext = NULL;
991 fhp->vprev = NULL;
992 fhp->vhead = NULL;
993 pvr2_channel_done(&fhp->channel);
994 pvr2_trace(PVR2_TRACE_STRUCT,
995 "Destroying pvr_v4l2_fh id=%p",fhp);
996 if (fhp->input_map) {
997 kfree(fhp->input_map);
998 fhp->input_map = NULL;
1000 kfree(fhp);
1001 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
1002 pvr2_v4l2_destroy_no_lock(vp);
1004 return 0;
1008 static int pvr2_v4l2_open(struct file *file)
1010 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
1011 struct pvr2_v4l2_fh *fhp;
1012 struct pvr2_v4l2 *vp;
1013 struct pvr2_hdw *hdw;
1014 unsigned int input_mask = 0;
1015 unsigned int input_cnt,idx;
1016 int ret = 0;
1018 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
1020 vp = dip->v4lp;
1021 hdw = vp->channel.hdw;
1023 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
1025 if (!pvr2_hdw_dev_ok(hdw)) {
1026 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
1027 "pvr2_v4l2_open: hardware not ready");
1028 return -EIO;
1031 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
1032 if (!fhp) {
1033 return -ENOMEM;
1036 init_waitqueue_head(&fhp->wait_data);
1037 fhp->pdi = dip;
1039 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1040 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
1042 if (dip->v4l_type == VFL_TYPE_RADIO) {
1043 /* Opening device as a radio, legal input selection subset
1044 is just the radio. */
1045 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1046 } else {
1047 /* Opening the main V4L device, legal input selection
1048 subset includes all analog inputs. */
1049 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1050 (1 << PVR2_CVAL_INPUT_TV) |
1051 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1052 (1 << PVR2_CVAL_INPUT_SVIDEO));
1054 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1055 if (ret) {
1056 pvr2_channel_done(&fhp->channel);
1057 pvr2_trace(PVR2_TRACE_STRUCT,
1058 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1059 fhp);
1061 kfree(fhp);
1062 return ret;
1065 input_mask &= pvr2_hdw_get_input_available(hdw);
1066 input_cnt = 0;
1067 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1068 if (input_mask & (1 << idx)) input_cnt++;
1070 fhp->input_cnt = input_cnt;
1071 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1072 if (!fhp->input_map) {
1073 pvr2_channel_done(&fhp->channel);
1074 pvr2_trace(PVR2_TRACE_STRUCT,
1075 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1076 fhp);
1077 kfree(fhp);
1078 return -ENOMEM;
1080 input_cnt = 0;
1081 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1082 if (!(input_mask & (1 << idx))) continue;
1083 fhp->input_map[input_cnt++] = idx;
1086 fhp->vnext = NULL;
1087 fhp->vprev = vp->vlast;
1088 if (vp->vlast) {
1089 vp->vlast->vnext = fhp;
1090 } else {
1091 vp->vfirst = fhp;
1093 vp->vlast = fhp;
1094 fhp->vhead = vp;
1096 fhp->file = file;
1097 file->private_data = fhp;
1098 v4l2_prio_open(&vp->prio, &fhp->prio);
1100 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1102 return 0;
1106 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1108 wake_up(&fhp->wait_data);
1111 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1113 int ret;
1114 struct pvr2_stream *sp;
1115 struct pvr2_hdw *hdw;
1116 if (fh->rhp) return 0;
1118 if (!fh->pdi->stream) {
1119 /* No stream defined for this node. This means that we're
1120 not currently allowed to stream from this node. */
1121 return -EPERM;
1124 /* First read() attempt. Try to claim the stream and start
1125 it... */
1126 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1127 fh->pdi->stream)) != 0) {
1128 /* Someone else must already have it */
1129 return ret;
1132 fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
1133 if (!fh->rhp) {
1134 pvr2_channel_claim_stream(&fh->channel,NULL);
1135 return -ENOMEM;
1138 hdw = fh->channel.mc_head->hdw;
1139 sp = fh->pdi->stream->stream;
1140 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1141 pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
1142 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1143 return pvr2_ioread_set_enabled(fh->rhp,!0);
1147 static ssize_t pvr2_v4l2_read(struct file *file,
1148 char __user *buff, size_t count, loff_t *ppos)
1150 struct pvr2_v4l2_fh *fh = file->private_data;
1151 int ret;
1153 if (fh->fw_mode_flag) {
1154 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1155 char *tbuf;
1156 int c1,c2;
1157 int tcnt = 0;
1158 unsigned int offs = *ppos;
1160 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1161 if (!tbuf) return -ENOMEM;
1163 while (count) {
1164 c1 = count;
1165 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1166 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1167 if (c2 < 0) {
1168 tcnt = c2;
1169 break;
1171 if (!c2) break;
1172 if (copy_to_user(buff,tbuf,c2)) {
1173 tcnt = -EFAULT;
1174 break;
1176 offs += c2;
1177 tcnt += c2;
1178 buff += c2;
1179 count -= c2;
1180 *ppos += c2;
1182 kfree(tbuf);
1183 return tcnt;
1186 if (!fh->rhp) {
1187 ret = pvr2_v4l2_iosetup(fh);
1188 if (ret) {
1189 return ret;
1193 for (;;) {
1194 ret = pvr2_ioread_read(fh->rhp,buff,count);
1195 if (ret >= 0) break;
1196 if (ret != -EAGAIN) break;
1197 if (file->f_flags & O_NONBLOCK) break;
1198 /* Doing blocking I/O. Wait here. */
1199 ret = wait_event_interruptible(
1200 fh->wait_data,
1201 pvr2_ioread_avail(fh->rhp) >= 0);
1202 if (ret < 0) break;
1205 return ret;
1209 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1211 unsigned int mask = 0;
1212 struct pvr2_v4l2_fh *fh = file->private_data;
1213 int ret;
1215 if (fh->fw_mode_flag) {
1216 mask |= POLLIN | POLLRDNORM;
1217 return mask;
1220 if (!fh->rhp) {
1221 ret = pvr2_v4l2_iosetup(fh);
1222 if (ret) return POLLERR;
1225 poll_wait(file,&fh->wait_data,wait);
1227 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1228 mask |= POLLIN | POLLRDNORM;
1231 return mask;
1235 static const struct v4l2_file_operations vdev_fops = {
1236 .owner = THIS_MODULE,
1237 .open = pvr2_v4l2_open,
1238 .release = pvr2_v4l2_release,
1239 .read = pvr2_v4l2_read,
1240 .ioctl = pvr2_v4l2_ioctl,
1241 .poll = pvr2_v4l2_poll,
1245 static struct video_device vdev_template = {
1246 .fops = &vdev_fops,
1250 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1251 struct pvr2_v4l2 *vp,
1252 int v4l_type)
1254 struct usb_device *usbdev;
1255 int mindevnum;
1256 int unit_number;
1257 int *nr_ptr = NULL;
1258 dip->v4lp = vp;
1260 usbdev = pvr2_hdw_get_dev(vp->channel.mc_head->hdw);
1261 dip->v4l_type = v4l_type;
1262 switch (v4l_type) {
1263 case VFL_TYPE_GRABBER:
1264 dip->stream = &vp->channel.mc_head->video_stream;
1265 dip->config = pvr2_config_mpeg;
1266 dip->minor_type = pvr2_v4l_type_video;
1267 nr_ptr = video_nr;
1268 if (!dip->stream) {
1269 pr_err(KBUILD_MODNAME
1270 ": Failed to set up pvrusb2 v4l video dev"
1271 " due to missing stream instance\n");
1272 return;
1274 break;
1275 case VFL_TYPE_VBI:
1276 dip->config = pvr2_config_vbi;
1277 dip->minor_type = pvr2_v4l_type_vbi;
1278 nr_ptr = vbi_nr;
1279 break;
1280 case VFL_TYPE_RADIO:
1281 dip->stream = &vp->channel.mc_head->video_stream;
1282 dip->config = pvr2_config_mpeg;
1283 dip->minor_type = pvr2_v4l_type_radio;
1284 nr_ptr = radio_nr;
1285 break;
1286 default:
1287 /* Bail out (this should be impossible) */
1288 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
1289 " due to unrecognized config\n");
1290 return;
1293 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1294 dip->devbase.release = pvr2_video_device_release;
1296 mindevnum = -1;
1297 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1298 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1299 mindevnum = nr_ptr[unit_number];
1301 dip->devbase.parent = &usbdev->dev;
1302 if ((video_register_device(&dip->devbase,
1303 dip->v4l_type, mindevnum) < 0) &&
1304 (video_register_device(&dip->devbase,
1305 dip->v4l_type, -1) < 0)) {
1306 pr_err(KBUILD_MODNAME
1307 ": Failed to register pvrusb2 v4l device\n");
1310 printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1311 video_device_node_name(&dip->devbase),
1312 pvr2_config_get_name(dip->config));
1314 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1315 dip->minor_type,dip->devbase.minor);
1319 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1321 struct pvr2_v4l2 *vp;
1323 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1324 if (!vp) return vp;
1325 pvr2_channel_init(&vp->channel,mnp);
1326 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1328 vp->channel.check_func = pvr2_v4l2_internal_check;
1330 /* register streams */
1331 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1332 if (!vp->dev_video) goto fail;
1333 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1334 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1335 (1 << PVR2_CVAL_INPUT_RADIO)) {
1336 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1337 if (!vp->dev_radio) goto fail;
1338 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1341 return vp;
1342 fail:
1343 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1344 pvr2_v4l2_destroy_no_lock(vp);
1345 return NULL;
1349 Stuff for Emacs to see, in order to encourage consistent editing style:
1350 *** Local Variables: ***
1351 *** mode: c ***
1352 *** fill-column: 75 ***
1353 *** tab-width: 8 ***
1354 *** c-basic-offset: 8 ***
1355 *** End: ***