V4L/DVB (13550): v4l: Use the new video_device_node_name function
[linux-2.6/btrfs-unstable.git] / drivers / media / video / pvrusb2 / pvrusb2-v4l2.c
blobcc8ddb2d2382902bb8e67194e3b79dfeaad0a65c
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/version.h>
24 #include "pvrusb2-context.h"
25 #include "pvrusb2-hdw.h"
26 #include "pvrusb2.h"
27 #include "pvrusb2-debug.h"
28 #include "pvrusb2-v4l2.h"
29 #include "pvrusb2-ioread.h"
30 #include <linux/videodev2.h>
31 #include <media/v4l2-dev.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.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;
60 /* Map contiguous ordinal value to input id */
61 unsigned char *input_map;
62 unsigned int input_cnt;
65 struct pvr2_v4l2 {
66 struct pvr2_channel channel;
67 struct pvr2_v4l2_fh *vfirst;
68 struct pvr2_v4l2_fh *vlast;
70 struct v4l2_prio_state prio;
72 /* streams - Note that these must be separately, individually,
73 * allocated pointers. This is because the v4l core is going to
74 * manage their deletion - separately, individually... */
75 struct pvr2_v4l2_dev *dev_video;
76 struct pvr2_v4l2_dev *dev_radio;
79 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
80 module_param_array(video_nr, int, NULL, 0444);
81 MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
82 static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
83 module_param_array(radio_nr, int, NULL, 0444);
84 MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
85 static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
86 module_param_array(vbi_nr, int, NULL, 0444);
87 MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
89 static struct v4l2_capability pvr_capability ={
90 .driver = "pvrusb2",
91 .card = "Hauppauge WinTV pvr-usb2",
92 .bus_info = "usb",
93 .version = KERNEL_VERSION(0, 9, 0),
94 .capabilities = (V4L2_CAP_VIDEO_CAPTURE |
95 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
96 V4L2_CAP_READWRITE),
97 .reserved = {0,0,0,0}
100 static struct v4l2_fmtdesc pvr_fmtdesc [] = {
102 .index = 0,
103 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
104 .flags = V4L2_FMT_FLAG_COMPRESSED,
105 .description = "MPEG1/2",
106 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
107 // breaks when I do that.
108 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
109 .reserved = { 0, 0, 0, 0 }
113 #define PVR_FORMAT_PIX 0
114 #define PVR_FORMAT_VBI 1
116 static struct v4l2_format pvr_format [] = {
117 [PVR_FORMAT_PIX] = {
118 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
119 .fmt = {
120 .pix = {
121 .width = 720,
122 .height = 576,
123 // This should really be V4L2_PIX_FMT_MPEG,
124 // but xawtv breaks when I do that.
125 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
126 .field = V4L2_FIELD_INTERLACED,
127 .bytesperline = 0, // doesn't make sense
128 // here
129 //FIXME : Don't know what to put here...
130 .sizeimage = (32*1024),
131 .colorspace = 0, // doesn't make sense here
132 .priv = 0
136 [PVR_FORMAT_VBI] = {
137 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
138 .fmt = {
139 .vbi = {
140 .sampling_rate = 27000000,
141 .offset = 248,
142 .samples_per_line = 1443,
143 .sample_format = V4L2_PIX_FMT_GREY,
144 .start = { 0, 0 },
145 .count = { 0, 0 },
146 .flags = 0,
147 .reserved = { 0, 0 }
155 * pvr_ioctl()
157 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
160 static long pvr2_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
162 struct pvr2_v4l2_fh *fh = file->private_data;
163 struct pvr2_v4l2 *vp = fh->vhead;
164 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
165 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
166 long ret = -EINVAL;
168 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
169 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
172 if (!pvr2_hdw_dev_ok(hdw)) {
173 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
174 "ioctl failed - bad or no context");
175 return -EFAULT;
178 /* check priority */
179 switch (cmd) {
180 case VIDIOC_S_CTRL:
181 case VIDIOC_S_STD:
182 case VIDIOC_S_INPUT:
183 case VIDIOC_S_TUNER:
184 case VIDIOC_S_FREQUENCY:
185 ret = v4l2_prio_check(&vp->prio, &fh->prio);
186 if (ret)
187 return ret;
190 switch (cmd) {
191 case VIDIOC_QUERYCAP:
193 struct v4l2_capability *cap = arg;
195 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
196 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
197 sizeof(cap->bus_info));
198 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
200 ret = 0;
201 break;
204 case VIDIOC_G_PRIORITY:
206 enum v4l2_priority *p = arg;
208 *p = v4l2_prio_max(&vp->prio);
209 ret = 0;
210 break;
213 case VIDIOC_S_PRIORITY:
215 enum v4l2_priority *prio = arg;
217 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
218 break;
221 case VIDIOC_ENUMSTD:
223 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
224 int idx = vs->index;
225 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
226 break;
229 case VIDIOC_G_STD:
231 int val = 0;
232 ret = pvr2_ctrl_get_value(
233 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
234 *(v4l2_std_id *)arg = val;
235 break;
238 case VIDIOC_S_STD:
240 ret = pvr2_ctrl_set_value(
241 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
242 *(v4l2_std_id *)arg);
243 break;
246 case VIDIOC_ENUMINPUT:
248 struct pvr2_ctrl *cptr;
249 struct v4l2_input *vi = (struct v4l2_input *)arg;
250 struct v4l2_input tmp;
251 unsigned int cnt;
252 int val;
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 if (vi->index >= fh->input_cnt) {
260 ret = -EINVAL;
261 break;
263 val = fh->input_map[vi->index];
264 switch (val) {
265 case PVR2_CVAL_INPUT_TV:
266 case PVR2_CVAL_INPUT_DTV:
267 case PVR2_CVAL_INPUT_RADIO:
268 tmp.type = V4L2_INPUT_TYPE_TUNER;
269 break;
270 case PVR2_CVAL_INPUT_SVIDEO:
271 case PVR2_CVAL_INPUT_COMPOSITE:
272 tmp.type = V4L2_INPUT_TYPE_CAMERA;
273 break;
274 default:
275 ret = -EINVAL;
276 break;
278 if (ret < 0) break;
280 cnt = 0;
281 pvr2_ctrl_get_valname(cptr,val,
282 tmp.name,sizeof(tmp.name)-1,&cnt);
283 tmp.name[cnt] = 0;
285 /* Don't bother with audioset, since this driver currently
286 always switches the audio whenever the video is
287 switched. */
289 /* Handling std is a tougher problem. It doesn't make
290 sense in cases where a device might be multi-standard.
291 We could just copy out the current value for the
292 standard, but it can change over time. For now just
293 leave it zero. */
295 memcpy(vi, &tmp, sizeof(tmp));
297 ret = 0;
298 break;
301 case VIDIOC_G_INPUT:
303 unsigned int idx;
304 struct pvr2_ctrl *cptr;
305 struct v4l2_input *vi = (struct v4l2_input *)arg;
306 int val;
307 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
308 val = 0;
309 ret = pvr2_ctrl_get_value(cptr,&val);
310 vi->index = 0;
311 for (idx = 0; idx < fh->input_cnt; idx++) {
312 if (fh->input_map[idx] == val) {
313 vi->index = idx;
314 break;
317 break;
320 case VIDIOC_S_INPUT:
322 struct v4l2_input *vi = (struct v4l2_input *)arg;
323 if (vi->index >= fh->input_cnt) {
324 ret = -ERANGE;
325 break;
327 ret = pvr2_ctrl_set_value(
328 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
329 fh->input_map[vi->index]);
330 break;
333 case VIDIOC_ENUMAUDIO:
335 /* pkt: FIXME: We are returning one "fake" input here
336 which could very well be called "whatever_we_like".
337 This is for apps that want to see an audio input
338 just to feel comfortable, as well as to test if
339 it can do stereo or sth. There is actually no guarantee
340 that the actual audio input cannot change behind the app's
341 back, but most applications should not mind that either.
343 Hopefully, mplayer people will work with us on this (this
344 whole mess is to support mplayer pvr://), or Hans will come
345 up with a more standard way to say "we have inputs but we
346 don 't want you to change them independent of video" which
347 will sort this mess.
349 struct v4l2_audio *vin = arg;
350 ret = -EINVAL;
351 if (vin->index > 0) break;
352 strncpy(vin->name, "PVRUSB2 Audio",14);
353 vin->capability = V4L2_AUDCAP_STEREO;
354 ret = 0;
355 break;
356 break;
359 case VIDIOC_G_AUDIO:
361 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
362 struct v4l2_audio *vin = arg;
363 memset(vin,0,sizeof(*vin));
364 vin->index = 0;
365 strncpy(vin->name, "PVRUSB2 Audio",14);
366 vin->capability = V4L2_AUDCAP_STEREO;
367 ret = 0;
368 break;
371 case VIDIOC_S_AUDIO:
373 ret = -EINVAL;
374 break;
376 case VIDIOC_G_TUNER:
378 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
380 if (vt->index != 0) break; /* Only answer for the 1st tuner */
382 pvr2_hdw_execute_tuner_poll(hdw);
383 ret = pvr2_hdw_get_tuner_status(hdw,vt);
384 break;
387 case VIDIOC_S_TUNER:
389 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
391 if (vt->index != 0)
392 break;
394 ret = pvr2_ctrl_set_value(
395 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
396 vt->audmode);
397 break;
400 case VIDIOC_S_FREQUENCY:
402 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
403 unsigned long fv;
404 struct v4l2_tuner vt;
405 int cur_input;
406 struct pvr2_ctrl *ctrlp;
407 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
408 if (ret != 0) break;
409 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
410 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
411 if (ret != 0) break;
412 if (vf->type == V4L2_TUNER_RADIO) {
413 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
414 pvr2_ctrl_set_value(ctrlp,
415 PVR2_CVAL_INPUT_RADIO);
417 } else {
418 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
419 pvr2_ctrl_set_value(ctrlp,
420 PVR2_CVAL_INPUT_TV);
423 fv = vf->frequency;
424 if (vt.capability & V4L2_TUNER_CAP_LOW) {
425 fv = (fv * 125) / 2;
426 } else {
427 fv = fv * 62500;
429 ret = pvr2_ctrl_set_value(
430 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
431 break;
434 case VIDIOC_G_FREQUENCY:
436 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
437 int val = 0;
438 int cur_input;
439 struct v4l2_tuner vt;
440 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
441 if (ret != 0) break;
442 ret = pvr2_ctrl_get_value(
443 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
444 &val);
445 if (ret != 0) break;
446 pvr2_ctrl_get_value(
447 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
448 &cur_input);
449 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
450 vf->type = V4L2_TUNER_RADIO;
451 } else {
452 vf->type = V4L2_TUNER_ANALOG_TV;
454 if (vt.capability & V4L2_TUNER_CAP_LOW) {
455 val = (val * 2) / 125;
456 } else {
457 val /= 62500;
459 vf->frequency = val;
460 break;
463 case VIDIOC_ENUM_FMT:
465 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
467 /* Only one format is supported : mpeg.*/
468 if (fd->index != 0)
469 break;
471 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
472 ret = 0;
473 break;
476 case VIDIOC_G_FMT:
478 struct v4l2_format *vf = (struct v4l2_format *)arg;
479 int val;
480 switch(vf->type) {
481 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
482 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
483 sizeof(struct v4l2_format));
484 val = 0;
485 pvr2_ctrl_get_value(
486 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
487 &val);
488 vf->fmt.pix.width = val;
489 val = 0;
490 pvr2_ctrl_get_value(
491 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
492 &val);
493 vf->fmt.pix.height = val;
494 ret = 0;
495 break;
496 case V4L2_BUF_TYPE_VBI_CAPTURE:
497 // ????? Still need to figure out to do VBI correctly
498 ret = -EINVAL;
499 break;
500 default:
501 ret = -EINVAL;
502 break;
504 break;
507 case VIDIOC_TRY_FMT:
508 case VIDIOC_S_FMT:
510 struct v4l2_format *vf = (struct v4l2_format *)arg;
512 ret = 0;
513 switch(vf->type) {
514 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
515 int lmin,lmax,ldef;
516 struct pvr2_ctrl *hcp,*vcp;
517 int h = vf->fmt.pix.height;
518 int w = vf->fmt.pix.width;
519 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
520 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
522 lmin = pvr2_ctrl_get_min(hcp);
523 lmax = pvr2_ctrl_get_max(hcp);
524 pvr2_ctrl_get_def(hcp, &ldef);
525 if (w == -1) {
526 w = ldef;
527 } else if (w < lmin) {
528 w = lmin;
529 } else if (w > lmax) {
530 w = lmax;
532 lmin = pvr2_ctrl_get_min(vcp);
533 lmax = pvr2_ctrl_get_max(vcp);
534 pvr2_ctrl_get_def(vcp, &ldef);
535 if (h == -1) {
536 h = ldef;
537 } else if (h < lmin) {
538 h = lmin;
539 } else if (h > lmax) {
540 h = lmax;
543 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
544 sizeof(struct v4l2_format));
545 vf->fmt.pix.width = w;
546 vf->fmt.pix.height = h;
548 if (cmd == VIDIOC_S_FMT) {
549 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
550 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
552 } break;
553 case V4L2_BUF_TYPE_VBI_CAPTURE:
554 // ????? Still need to figure out to do VBI correctly
555 ret = -EINVAL;
556 break;
557 default:
558 ret = -EINVAL;
559 break;
561 break;
564 case VIDIOC_STREAMON:
566 if (!fh->dev_info->stream) {
567 /* No stream defined for this node. This means
568 that we're not currently allowed to stream from
569 this node. */
570 ret = -EPERM;
571 break;
573 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
574 if (ret < 0) return ret;
575 ret = pvr2_hdw_set_streaming(hdw,!0);
576 break;
579 case VIDIOC_STREAMOFF:
581 if (!fh->dev_info->stream) {
582 /* No stream defined for this node. This means
583 that we're not currently allowed to stream from
584 this node. */
585 ret = -EPERM;
586 break;
588 ret = pvr2_hdw_set_streaming(hdw,0);
589 break;
592 case VIDIOC_QUERYCTRL:
594 struct pvr2_ctrl *cptr;
595 int val;
596 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
597 ret = 0;
598 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
599 cptr = pvr2_hdw_get_ctrl_nextv4l(
600 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
601 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
602 } else {
603 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
605 if (!cptr) {
606 pvr2_trace(PVR2_TRACE_V4LIOCTL,
607 "QUERYCTRL id=0x%x not implemented here",
608 vc->id);
609 ret = -EINVAL;
610 break;
613 pvr2_trace(PVR2_TRACE_V4LIOCTL,
614 "QUERYCTRL id=0x%x mapping name=%s (%s)",
615 vc->id,pvr2_ctrl_get_name(cptr),
616 pvr2_ctrl_get_desc(cptr));
617 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
618 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
619 pvr2_ctrl_get_def(cptr, &val);
620 vc->default_value = val;
621 switch (pvr2_ctrl_get_type(cptr)) {
622 case pvr2_ctl_enum:
623 vc->type = V4L2_CTRL_TYPE_MENU;
624 vc->minimum = 0;
625 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
626 vc->step = 1;
627 break;
628 case pvr2_ctl_bool:
629 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
630 vc->minimum = 0;
631 vc->maximum = 1;
632 vc->step = 1;
633 break;
634 case pvr2_ctl_int:
635 vc->type = V4L2_CTRL_TYPE_INTEGER;
636 vc->minimum = pvr2_ctrl_get_min(cptr);
637 vc->maximum = pvr2_ctrl_get_max(cptr);
638 vc->step = 1;
639 break;
640 default:
641 pvr2_trace(PVR2_TRACE_V4LIOCTL,
642 "QUERYCTRL id=0x%x name=%s not mappable",
643 vc->id,pvr2_ctrl_get_name(cptr));
644 ret = -EINVAL;
645 break;
647 break;
650 case VIDIOC_QUERYMENU:
652 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
653 unsigned int cnt = 0;
654 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
655 vm->index,
656 vm->name,sizeof(vm->name)-1,
657 &cnt);
658 vm->name[cnt] = 0;
659 break;
662 case VIDIOC_G_CTRL:
664 struct v4l2_control *vc = (struct v4l2_control *)arg;
665 int val = 0;
666 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
667 &val);
668 vc->value = val;
669 break;
672 case VIDIOC_S_CTRL:
674 struct v4l2_control *vc = (struct v4l2_control *)arg;
675 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
676 vc->value);
677 break;
680 case VIDIOC_G_EXT_CTRLS:
682 struct v4l2_ext_controls *ctls =
683 (struct v4l2_ext_controls *)arg;
684 struct v4l2_ext_control *ctrl;
685 unsigned int idx;
686 int val;
687 ret = 0;
688 for (idx = 0; idx < ctls->count; idx++) {
689 ctrl = ctls->controls + idx;
690 ret = pvr2_ctrl_get_value(
691 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
692 if (ret) {
693 ctls->error_idx = idx;
694 break;
696 /* Ensure that if read as a 64 bit value, the user
697 will still get a hopefully sane value */
698 ctrl->value64 = 0;
699 ctrl->value = val;
701 break;
704 case VIDIOC_S_EXT_CTRLS:
706 struct v4l2_ext_controls *ctls =
707 (struct v4l2_ext_controls *)arg;
708 struct v4l2_ext_control *ctrl;
709 unsigned int idx;
710 ret = 0;
711 for (idx = 0; idx < ctls->count; idx++) {
712 ctrl = ctls->controls + idx;
713 ret = pvr2_ctrl_set_value(
714 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
715 ctrl->value);
716 if (ret) {
717 ctls->error_idx = idx;
718 break;
721 break;
724 case VIDIOC_TRY_EXT_CTRLS:
726 struct v4l2_ext_controls *ctls =
727 (struct v4l2_ext_controls *)arg;
728 struct v4l2_ext_control *ctrl;
729 struct pvr2_ctrl *pctl;
730 unsigned int idx;
731 /* For the moment just validate that the requested control
732 actually exists. */
733 ret = 0;
734 for (idx = 0; idx < ctls->count; idx++) {
735 ctrl = ctls->controls + idx;
736 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
737 if (!pctl) {
738 ret = -EINVAL;
739 ctls->error_idx = idx;
740 break;
743 break;
746 case VIDIOC_CROPCAP:
748 struct v4l2_cropcap *cap = (struct v4l2_cropcap *)arg;
749 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
750 ret = -EINVAL;
751 break;
753 ret = pvr2_hdw_get_cropcap(hdw, cap);
754 cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
755 break;
757 case VIDIOC_G_CROP:
759 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
760 int val = 0;
761 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
762 ret = -EINVAL;
763 break;
765 ret = pvr2_ctrl_get_value(
766 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
767 if (ret != 0) {
768 ret = -EINVAL;
769 break;
771 crop->c.left = val;
772 ret = pvr2_ctrl_get_value(
773 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
774 if (ret != 0) {
775 ret = -EINVAL;
776 break;
778 crop->c.top = val;
779 ret = pvr2_ctrl_get_value(
780 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
781 if (ret != 0) {
782 ret = -EINVAL;
783 break;
785 crop->c.width = val;
786 ret = pvr2_ctrl_get_value(
787 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
788 if (ret != 0) {
789 ret = -EINVAL;
790 break;
792 crop->c.height = val;
794 case VIDIOC_S_CROP:
796 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
797 struct v4l2_cropcap cap;
798 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
799 ret = -EINVAL;
800 break;
802 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
803 ret = pvr2_ctrl_set_value(
804 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
805 crop->c.left);
806 if (ret != 0) {
807 ret = -EINVAL;
808 break;
810 ret = pvr2_ctrl_set_value(
811 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
812 crop->c.top);
813 if (ret != 0) {
814 ret = -EINVAL;
815 break;
817 ret = pvr2_ctrl_set_value(
818 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
819 crop->c.width);
820 if (ret != 0) {
821 ret = -EINVAL;
822 break;
824 ret = pvr2_ctrl_set_value(
825 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
826 crop->c.height);
827 if (ret != 0) {
828 ret = -EINVAL;
829 break;
832 case VIDIOC_LOG_STATUS:
834 pvr2_hdw_trigger_module_log(hdw);
835 ret = 0;
836 break;
838 #ifdef CONFIG_VIDEO_ADV_DEBUG
839 case VIDIOC_DBG_S_REGISTER:
840 case VIDIOC_DBG_G_REGISTER:
842 u64 val;
843 struct v4l2_dbg_register *req = (struct v4l2_dbg_register *)arg;
844 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
845 ret = pvr2_hdw_register_access(
846 hdw, &req->match, req->reg,
847 cmd == VIDIOC_DBG_S_REGISTER, &val);
848 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
849 break;
851 #endif
853 default :
854 ret = v4l_compat_translate_ioctl(file, cmd,
855 arg, pvr2_v4l2_do_ioctl);
858 pvr2_hdw_commit_ctl(hdw);
860 if (ret < 0) {
861 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
862 pvr2_trace(PVR2_TRACE_V4LIOCTL,
863 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret);
864 } else {
865 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
866 pvr2_trace(PVR2_TRACE_V4LIOCTL,
867 "pvr2_v4l2_do_ioctl failure, ret=%ld"
868 " command was:", ret);
869 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
870 cmd);
873 } else {
874 pvr2_trace(PVR2_TRACE_V4LIOCTL,
875 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
876 ret, ret);
878 return ret;
881 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
883 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
884 enum pvr2_config cfg = dip->config;
886 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
888 /* Paranoia */
889 dip->v4lp = NULL;
890 dip->stream = NULL;
892 /* Actual deallocation happens later when all internal references
893 are gone. */
894 video_unregister_device(&dip->devbase);
896 printk(KERN_INFO "pvrusb2: unregistered device %s [%s]\n",
897 video_device_node_name(&dip->devbase),
898 pvr2_config_get_name(cfg));
903 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
905 if (!dip) return;
906 if (!dip->devbase.parent) return;
907 dip->devbase.parent = NULL;
908 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
912 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
914 if (vp->dev_video) {
915 pvr2_v4l2_dev_destroy(vp->dev_video);
916 vp->dev_video = NULL;
918 if (vp->dev_radio) {
919 pvr2_v4l2_dev_destroy(vp->dev_radio);
920 vp->dev_radio = NULL;
923 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
924 pvr2_channel_done(&vp->channel);
925 kfree(vp);
929 static void pvr2_video_device_release(struct video_device *vdev)
931 struct pvr2_v4l2_dev *dev;
932 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
933 kfree(dev);
937 static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
939 struct pvr2_v4l2 *vp;
940 vp = container_of(chp,struct pvr2_v4l2,channel);
941 if (!vp->channel.mc_head->disconnect_flag) return;
942 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
943 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
944 if (vp->vfirst) return;
945 pvr2_v4l2_destroy_no_lock(vp);
949 static long pvr2_v4l2_ioctl(struct file *file,
950 unsigned int cmd, unsigned long arg)
953 return video_usercopy(file, cmd, arg, pvr2_v4l2_do_ioctl);
957 static int pvr2_v4l2_release(struct file *file)
959 struct pvr2_v4l2_fh *fhp = file->private_data;
960 struct pvr2_v4l2 *vp = fhp->vhead;
961 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
963 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
965 if (fhp->rhp) {
966 struct pvr2_stream *sp;
967 pvr2_hdw_set_streaming(hdw,0);
968 sp = pvr2_ioread_get_stream(fhp->rhp);
969 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
970 pvr2_ioread_destroy(fhp->rhp);
971 fhp->rhp = NULL;
974 v4l2_prio_close(&vp->prio, &fhp->prio);
975 file->private_data = NULL;
977 if (fhp->vnext) {
978 fhp->vnext->vprev = fhp->vprev;
979 } else {
980 vp->vlast = fhp->vprev;
982 if (fhp->vprev) {
983 fhp->vprev->vnext = fhp->vnext;
984 } else {
985 vp->vfirst = fhp->vnext;
987 fhp->vnext = NULL;
988 fhp->vprev = NULL;
989 fhp->vhead = NULL;
990 pvr2_channel_done(&fhp->channel);
991 pvr2_trace(PVR2_TRACE_STRUCT,
992 "Destroying pvr_v4l2_fh id=%p",fhp);
993 if (fhp->input_map) {
994 kfree(fhp->input_map);
995 fhp->input_map = NULL;
997 kfree(fhp);
998 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
999 pvr2_v4l2_destroy_no_lock(vp);
1001 return 0;
1005 static int pvr2_v4l2_open(struct file *file)
1007 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
1008 struct pvr2_v4l2_fh *fhp;
1009 struct pvr2_v4l2 *vp;
1010 struct pvr2_hdw *hdw;
1011 unsigned int input_mask = 0;
1012 unsigned int input_cnt,idx;
1013 int ret = 0;
1015 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
1017 vp = dip->v4lp;
1018 hdw = vp->channel.hdw;
1020 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
1022 if (!pvr2_hdw_dev_ok(hdw)) {
1023 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
1024 "pvr2_v4l2_open: hardware not ready");
1025 return -EIO;
1028 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
1029 if (!fhp) {
1030 return -ENOMEM;
1033 init_waitqueue_head(&fhp->wait_data);
1034 fhp->dev_info = dip;
1036 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1037 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
1039 if (dip->v4l_type == VFL_TYPE_RADIO) {
1040 /* Opening device as a radio, legal input selection subset
1041 is just the radio. */
1042 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1043 } else {
1044 /* Opening the main V4L device, legal input selection
1045 subset includes all analog inputs. */
1046 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1047 (1 << PVR2_CVAL_INPUT_TV) |
1048 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1049 (1 << PVR2_CVAL_INPUT_SVIDEO));
1051 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1052 if (ret) {
1053 pvr2_channel_done(&fhp->channel);
1054 pvr2_trace(PVR2_TRACE_STRUCT,
1055 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1056 fhp);
1058 kfree(fhp);
1059 return ret;
1062 input_mask &= pvr2_hdw_get_input_available(hdw);
1063 input_cnt = 0;
1064 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1065 if (input_mask & (1 << idx)) input_cnt++;
1067 fhp->input_cnt = input_cnt;
1068 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1069 if (!fhp->input_map) {
1070 pvr2_channel_done(&fhp->channel);
1071 pvr2_trace(PVR2_TRACE_STRUCT,
1072 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1073 fhp);
1074 kfree(fhp);
1075 return -ENOMEM;
1077 input_cnt = 0;
1078 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1079 if (!(input_mask & (1 << idx))) continue;
1080 fhp->input_map[input_cnt++] = idx;
1083 fhp->vnext = NULL;
1084 fhp->vprev = vp->vlast;
1085 if (vp->vlast) {
1086 vp->vlast->vnext = fhp;
1087 } else {
1088 vp->vfirst = fhp;
1090 vp->vlast = fhp;
1091 fhp->vhead = vp;
1093 fhp->file = file;
1094 file->private_data = fhp;
1095 v4l2_prio_open(&vp->prio,&fhp->prio);
1097 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1099 return 0;
1103 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1105 wake_up(&fhp->wait_data);
1108 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1110 int ret;
1111 struct pvr2_stream *sp;
1112 struct pvr2_hdw *hdw;
1113 if (fh->rhp) return 0;
1115 if (!fh->dev_info->stream) {
1116 /* No stream defined for this node. This means that we're
1117 not currently allowed to stream from this node. */
1118 return -EPERM;
1121 /* First read() attempt. Try to claim the stream and start
1122 it... */
1123 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1124 fh->dev_info->stream)) != 0) {
1125 /* Someone else must already have it */
1126 return ret;
1129 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
1130 if (!fh->rhp) {
1131 pvr2_channel_claim_stream(&fh->channel,NULL);
1132 return -ENOMEM;
1135 hdw = fh->channel.mc_head->hdw;
1136 sp = fh->dev_info->stream->stream;
1137 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1138 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
1139 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1140 return pvr2_ioread_set_enabled(fh->rhp,!0);
1144 static ssize_t pvr2_v4l2_read(struct file *file,
1145 char __user *buff, size_t count, loff_t *ppos)
1147 struct pvr2_v4l2_fh *fh = file->private_data;
1148 int ret;
1150 if (fh->fw_mode_flag) {
1151 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1152 char *tbuf;
1153 int c1,c2;
1154 int tcnt = 0;
1155 unsigned int offs = *ppos;
1157 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1158 if (!tbuf) return -ENOMEM;
1160 while (count) {
1161 c1 = count;
1162 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1163 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1164 if (c2 < 0) {
1165 tcnt = c2;
1166 break;
1168 if (!c2) break;
1169 if (copy_to_user(buff,tbuf,c2)) {
1170 tcnt = -EFAULT;
1171 break;
1173 offs += c2;
1174 tcnt += c2;
1175 buff += c2;
1176 count -= c2;
1177 *ppos += c2;
1179 kfree(tbuf);
1180 return tcnt;
1183 if (!fh->rhp) {
1184 ret = pvr2_v4l2_iosetup(fh);
1185 if (ret) {
1186 return ret;
1190 for (;;) {
1191 ret = pvr2_ioread_read(fh->rhp,buff,count);
1192 if (ret >= 0) break;
1193 if (ret != -EAGAIN) break;
1194 if (file->f_flags & O_NONBLOCK) break;
1195 /* Doing blocking I/O. Wait here. */
1196 ret = wait_event_interruptible(
1197 fh->wait_data,
1198 pvr2_ioread_avail(fh->rhp) >= 0);
1199 if (ret < 0) break;
1202 return ret;
1206 static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1208 unsigned int mask = 0;
1209 struct pvr2_v4l2_fh *fh = file->private_data;
1210 int ret;
1212 if (fh->fw_mode_flag) {
1213 mask |= POLLIN | POLLRDNORM;
1214 return mask;
1217 if (!fh->rhp) {
1218 ret = pvr2_v4l2_iosetup(fh);
1219 if (ret) return POLLERR;
1222 poll_wait(file,&fh->wait_data,wait);
1224 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1225 mask |= POLLIN | POLLRDNORM;
1228 return mask;
1232 static const struct v4l2_file_operations vdev_fops = {
1233 .owner = THIS_MODULE,
1234 .open = pvr2_v4l2_open,
1235 .release = pvr2_v4l2_release,
1236 .read = pvr2_v4l2_read,
1237 .ioctl = pvr2_v4l2_ioctl,
1238 .poll = pvr2_v4l2_poll,
1242 static struct video_device vdev_template = {
1243 .fops = &vdev_fops,
1247 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1248 struct pvr2_v4l2 *vp,
1249 int v4l_type)
1251 struct usb_device *usbdev;
1252 int mindevnum;
1253 int unit_number;
1254 int *nr_ptr = NULL;
1255 dip->v4lp = vp;
1257 usbdev = pvr2_hdw_get_dev(vp->channel.mc_head->hdw);
1258 dip->v4l_type = v4l_type;
1259 switch (v4l_type) {
1260 case VFL_TYPE_GRABBER:
1261 dip->stream = &vp->channel.mc_head->video_stream;
1262 dip->config = pvr2_config_mpeg;
1263 dip->minor_type = pvr2_v4l_type_video;
1264 nr_ptr = video_nr;
1265 if (!dip->stream) {
1266 pr_err(KBUILD_MODNAME
1267 ": Failed to set up pvrusb2 v4l video dev"
1268 " due to missing stream instance\n");
1269 return;
1271 break;
1272 case VFL_TYPE_VBI:
1273 dip->config = pvr2_config_vbi;
1274 dip->minor_type = pvr2_v4l_type_vbi;
1275 nr_ptr = vbi_nr;
1276 break;
1277 case VFL_TYPE_RADIO:
1278 dip->stream = &vp->channel.mc_head->video_stream;
1279 dip->config = pvr2_config_mpeg;
1280 dip->minor_type = pvr2_v4l_type_radio;
1281 nr_ptr = radio_nr;
1282 break;
1283 default:
1284 /* Bail out (this should be impossible) */
1285 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
1286 " due to unrecognized config\n");
1287 return;
1290 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1291 dip->devbase.release = pvr2_video_device_release;
1293 mindevnum = -1;
1294 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1295 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1296 mindevnum = nr_ptr[unit_number];
1298 dip->devbase.parent = &usbdev->dev;
1299 if ((video_register_device(&dip->devbase,
1300 dip->v4l_type, mindevnum) < 0) &&
1301 (video_register_device(&dip->devbase,
1302 dip->v4l_type, -1) < 0)) {
1303 pr_err(KBUILD_MODNAME
1304 ": Failed to register pvrusb2 v4l device\n");
1307 printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1308 video_device_node_name(&dip->devbase),
1309 pvr2_config_get_name(dip->config));
1311 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1312 dip->minor_type,dip->devbase.minor);
1316 struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1318 struct pvr2_v4l2 *vp;
1320 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1321 if (!vp) return vp;
1322 pvr2_channel_init(&vp->channel,mnp);
1323 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1325 vp->channel.check_func = pvr2_v4l2_internal_check;
1327 /* register streams */
1328 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1329 if (!vp->dev_video) goto fail;
1330 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1331 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1332 (1 << PVR2_CVAL_INPUT_RADIO)) {
1333 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1334 if (!vp->dev_radio) goto fail;
1335 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1338 return vp;
1339 fail:
1340 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1341 pvr2_v4l2_destroy_no_lock(vp);
1342 return NULL;
1346 Stuff for Emacs to see, in order to encourage consistent editing style:
1347 *** Local Variables: ***
1348 *** mode: c ***
1349 *** fill-column: 75 ***
1350 *** tab-width: 8 ***
1351 *** c-basic-offset: 8 ***
1352 *** End: ***