Merge branch 'omap-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tmlind...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / pvrusb2 / pvrusb2-v4l2.c
CommitLineData
d855497e
MI
1/*
2 *
d855497e
MI
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
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
10 *
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.
15 *
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
19 *
20 */
21
22#include <linux/kernel.h>
5a0e3ad6 23#include <linux/slab.h>
ce4260c3 24#include <linux/version.h>
d855497e
MI
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>
43e06022 32#include <media/v4l2-dev.h>
d855497e 33#include <media/v4l2-common.h>
35ea11ff 34#include <media/v4l2-ioctl.h>
d855497e
MI
35
36struct pvr2_v4l2_dev;
37struct pvr2_v4l2_fh;
38struct pvr2_v4l2;
39
d855497e 40struct pvr2_v4l2_dev {
75910053 41 struct video_device devbase; /* MUST be first! */
d855497e 42 struct pvr2_v4l2 *v4lp;
d855497e 43 struct pvr2_context_stream *stream;
16eb40d3
MI
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 */
d855497e
MI
48};
49
50struct pvr2_v4l2_fh {
51 struct pvr2_channel channel;
108bdd7a 52 struct pvr2_v4l2_dev *pdi;
d855497e
MI
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;
e57b1c80
MI
61 /* Map contiguous ordinal value to input id */
62 unsigned char *input_map;
63 unsigned int input_cnt;
d855497e
MI
64};
65
66struct pvr2_v4l2 {
67 struct pvr2_channel channel;
68 struct pvr2_v4l2_fh *vfirst;
69 struct pvr2_v4l2_fh *vlast;
70
71 struct v4l2_prio_state prio;
72
0f0f257b
MI
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;
d855497e
MI
78};
79
80static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
81module_param_array(video_nr, int, NULL, 0444);
5e6862ce
MI
82MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
83static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
84module_param_array(radio_nr, int, NULL, 0444);
85MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
86static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
87module_param_array(vbi_nr, int, NULL, 0444);
88MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
d855497e 89
07e337ee 90static struct v4l2_capability pvr_capability ={
d855497e
MI
91 .driver = "pvrusb2",
92 .card = "Hauppauge WinTV pvr-usb2",
93 .bus_info = "usb",
48c5b0df 94 .version = KERNEL_VERSION(0, 9, 0),
d166b02e 95 .capabilities = (V4L2_CAP_VIDEO_CAPTURE |
ae2b9e25 96 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
d855497e
MI
97 V4L2_CAP_READWRITE),
98 .reserved = {0,0,0,0}
99};
100
07e337ee 101static struct v4l2_fmtdesc pvr_fmtdesc [] = {
d855497e
MI
102 {
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 }
111 }
112};
113
114#define PVR_FORMAT_PIX 0
115#define PVR_FORMAT_VBI 1
116
07e337ee 117static struct v4l2_format pvr_format [] = {
d855497e
MI
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
134 }
135 }
136 },
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 }
149 }
150 }
151 }
152};
153
16eb40d3 154
d855497e
MI
155/*
156 * pvr_ioctl()
157 *
158 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
159 *
160 */
069b7479 161static long pvr2_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
d855497e
MI
162{
163 struct pvr2_v4l2_fh *fh = file->private_data;
164 struct pvr2_v4l2 *vp = fh->vhead;
108bdd7a 165 struct pvr2_v4l2_dev *pdi = fh->pdi;
d855497e 166 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
069b7479 167 long ret = -EINVAL;
d855497e
MI
168
169 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
170 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
171 }
172
173 if (!pvr2_hdw_dev_ok(hdw)) {
174 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
175 "ioctl failed - bad or no context");
176 return -EFAULT;
177 }
178
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:
ffb4877b 186 ret = v4l2_prio_check(&vp->prio, fh->prio);
d855497e
MI
187 if (ret)
188 return ret;
189 }
190
191 switch (cmd) {
192 case VIDIOC_QUERYCAP:
193 {
194 struct v4l2_capability *cap = arg;
195
196 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
31a18547
MI
197 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
198 sizeof(cap->bus_info));
78a47101 199 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
d855497e
MI
200
201 ret = 0;
202 break;
203 }
204
205 case VIDIOC_G_PRIORITY:
206 {
207 enum v4l2_priority *p = arg;
208
209 *p = v4l2_prio_max(&vp->prio);
210 ret = 0;
211 break;
212 }
213
214 case VIDIOC_S_PRIORITY:
215 {
216 enum v4l2_priority *prio = arg;
217
218 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
219 break;
220 }
221
222 case VIDIOC_ENUMSTD:
223 {
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;
228 }
229
230 case VIDIOC_G_STD:
231 {
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;
237 }
238
239 case VIDIOC_S_STD:
240 {
241 ret = pvr2_ctrl_set_value(
242 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
243 *(v4l2_std_id *)arg);
244 break;
245 }
246
247 case VIDIOC_ENUMINPUT:
248 {
249 struct pvr2_ctrl *cptr;
250 struct v4l2_input *vi = (struct v4l2_input *)arg;
251 struct v4l2_input tmp;
252 unsigned int cnt;
beb0ecd7 253 int val;
d855497e
MI
254
255 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
256
257 memset(&tmp,0,sizeof(tmp));
258 tmp.index = vi->index;
259 ret = 0;
223ffe5f 260 if (vi->index >= fh->input_cnt) {
beb0ecd7
MI
261 ret = -EINVAL;
262 break;
263 }
e57b1c80 264 val = fh->input_map[vi->index];
beb0ecd7 265 switch (val) {
d855497e 266 case PVR2_CVAL_INPUT_TV:
895c3e8b 267 case PVR2_CVAL_INPUT_DTV:
d855497e
MI
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;
278 }
279 if (ret < 0) break;
280
281 cnt = 0;
beb0ecd7 282 pvr2_ctrl_get_valname(cptr,val,
d855497e
MI
283 tmp.name,sizeof(tmp.name)-1,&cnt);
284 tmp.name[cnt] = 0;
285
286 /* Don't bother with audioset, since this driver currently
287 always switches the audio whenever the video is
288 switched. */
289
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. */
295
296 memcpy(vi, &tmp, sizeof(tmp));
297
298 ret = 0;
299 break;
300 }
301
302 case VIDIOC_G_INPUT:
303 {
beb0ecd7 304 unsigned int idx;
d855497e
MI
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);
beb0ecd7 311 vi->index = 0;
e57b1c80
MI
312 for (idx = 0; idx < fh->input_cnt; idx++) {
313 if (fh->input_map[idx] == val) {
beb0ecd7
MI
314 vi->index = idx;
315 break;
316 }
317 }
d855497e
MI
318 break;
319 }
320
321 case VIDIOC_S_INPUT:
322 {
323 struct v4l2_input *vi = (struct v4l2_input *)arg;
223ffe5f 324 if (vi->index >= fh->input_cnt) {
beb0ecd7
MI
325 ret = -ERANGE;
326 break;
327 }
d855497e
MI
328 ret = pvr2_ctrl_set_value(
329 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
e57b1c80 330 fh->input_map[vi->index]);
d855497e
MI
331 break;
332 }
333
334 case VIDIOC_ENUMAUDIO:
335 {
848ed3ca
PK
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.
343
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.
349 */
350 struct v4l2_audio *vin = arg;
d855497e 351 ret = -EINVAL;
848ed3ca
PK
352 if (vin->index > 0) break;
353 strncpy(vin->name, "PVRUSB2 Audio",14);
354 vin->capability = V4L2_AUDCAP_STEREO;
355 ret = 0;
356 break;
d855497e
MI
357 break;
358 }
359
360 case VIDIOC_G_AUDIO:
361 {
848ed3ca
PK
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;
d855497e
MI
369 break;
370 }
371
372 case VIDIOC_S_AUDIO:
373 {
374 ret = -EINVAL;
375 break;
376 }
377 case VIDIOC_G_TUNER:
378 {
379 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
5cc1dd8c 380
8d364363 381 if (vt->index != 0) break; /* Only answer for the 1st tuner */
5cc1dd8c 382
18103c57
MI
383 pvr2_hdw_execute_tuner_poll(hdw);
384 ret = pvr2_hdw_get_tuner_status(hdw,vt);
d855497e
MI
385 break;
386 }
387
388 case VIDIOC_S_TUNER:
389 {
390 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
391
392 if (vt->index != 0)
393 break;
394
395 ret = pvr2_ctrl_set_value(
396 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
397 vt->audmode);
11fc76c9 398 break;
d855497e
MI
399 }
400
401 case VIDIOC_S_FREQUENCY:
402 {
403 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
c0e69315 404 unsigned long fv;
18103c57
MI
405 struct v4l2_tuner vt;
406 int cur_input;
407 struct pvr2_ctrl *ctrlp;
408 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
409 if (ret != 0) break;
410 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
411 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
412 if (ret != 0) break;
c0e69315 413 if (vf->type == V4L2_TUNER_RADIO) {
18103c57
MI
414 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
415 pvr2_ctrl_set_value(ctrlp,
416 PVR2_CVAL_INPUT_RADIO);
417 }
418 } else {
419 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
420 pvr2_ctrl_set_value(ctrlp,
421 PVR2_CVAL_INPUT_TV);
422 }
423 }
424 fv = vf->frequency;
425 if (vt.capability & V4L2_TUNER_CAP_LOW) {
c0e69315
MI
426 fv = (fv * 125) / 2;
427 } else {
428 fv = fv * 62500;
429 }
d855497e 430 ret = pvr2_ctrl_set_value(
c0e69315 431 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
d855497e
MI
432 break;
433 }
434
435 case VIDIOC_G_FREQUENCY:
436 {
437 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
438 int val = 0;
18103c57
MI
439 int cur_input;
440 struct v4l2_tuner vt;
441 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
442 if (ret != 0) break;
d855497e
MI
443 ret = pvr2_ctrl_get_value(
444 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
445 &val);
c0e69315
MI
446 if (ret != 0) break;
447 pvr2_ctrl_get_value(
448 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
449 &cur_input);
450 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
c0e69315
MI
451 vf->type = V4L2_TUNER_RADIO;
452 } else {
c0e69315
MI
453 vf->type = V4L2_TUNER_ANALOG_TV;
454 }
18103c57
MI
455 if (vt.capability & V4L2_TUNER_CAP_LOW) {
456 val = (val * 2) / 125;
457 } else {
458 val /= 62500;
459 }
460 vf->frequency = val;
d855497e
MI
461 break;
462 }
463
464 case VIDIOC_ENUM_FMT:
465 {
466 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
467
468 /* Only one format is supported : mpeg.*/
469 if (fd->index != 0)
470 break;
471
472 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
473 ret = 0;
474 break;
475 }
476
477 case VIDIOC_G_FMT:
478 {
479 struct v4l2_format *vf = (struct v4l2_format *)arg;
480 int val;
481 switch(vf->type) {
482 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
483 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
484 sizeof(struct v4l2_format));
485 val = 0;
486 pvr2_ctrl_get_value(
487 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
488 &val);
489 vf->fmt.pix.width = val;
490 val = 0;
d855497e
MI
491 pvr2_ctrl_get_value(
492 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
493 &val);
494 vf->fmt.pix.height = val;
495 ret = 0;
496 break;
497 case V4L2_BUF_TYPE_VBI_CAPTURE:
498 // ????? Still need to figure out to do VBI correctly
499 ret = -EINVAL;
500 break;
501 default:
502 ret = -EINVAL;
503 break;
504 }
505 break;
506 }
507
508 case VIDIOC_TRY_FMT:
509 case VIDIOC_S_FMT:
510 {
511 struct v4l2_format *vf = (struct v4l2_format *)arg;
512
513 ret = 0;
514 switch(vf->type) {
515 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
fd694964 516 int lmin,lmax,ldef;
e95a1915 517 struct pvr2_ctrl *hcp,*vcp;
d855497e
MI
518 int h = vf->fmt.pix.height;
519 int w = vf->fmt.pix.width;
e95a1915
MI
520 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
521 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
522
523 lmin = pvr2_ctrl_get_min(hcp);
524 lmax = pvr2_ctrl_get_max(hcp);
26dd1c57 525 pvr2_ctrl_get_def(hcp, &ldef);
fd694964
PK
526 if (w == -1) {
527 w = ldef;
528 } else if (w < lmin) {
e95a1915
MI
529 w = lmin;
530 } else if (w > lmax) {
531 w = lmax;
d855497e 532 }
b31e341b
HV
533 lmin = pvr2_ctrl_get_min(vcp);
534 lmax = pvr2_ctrl_get_max(vcp);
26dd1c57 535 pvr2_ctrl_get_def(vcp, &ldef);
fd694964
PK
536 if (h == -1) {
537 h = ldef;
538 } else if (h < lmin) {
b31e341b
HV
539 h = lmin;
540 } else if (h > lmax) {
541 h = lmax;
542 }
d855497e
MI
543
544 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
545 sizeof(struct v4l2_format));
039c430f
MI
546 vf->fmt.pix.width = w;
547 vf->fmt.pix.height = h;
d855497e
MI
548
549 if (cmd == VIDIOC_S_FMT) {
e95a1915
MI
550 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
551 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
d855497e
MI
552 }
553 } break;
554 case V4L2_BUF_TYPE_VBI_CAPTURE:
555 // ????? Still need to figure out to do VBI correctly
556 ret = -EINVAL;
557 break;
558 default:
559 ret = -EINVAL;
560 break;
561 }
562 break;
563 }
564
565 case VIDIOC_STREAMON:
566 {
108bdd7a 567 if (!fh->pdi->stream) {
16eb40d3
MI
568 /* No stream defined for this node. This means
569 that we're not currently allowed to stream from
570 this node. */
571 ret = -EPERM;
572 break;
573 }
108bdd7a 574 ret = pvr2_hdw_set_stream_type(hdw,pdi->config);
d855497e
MI
575 if (ret < 0) return ret;
576 ret = pvr2_hdw_set_streaming(hdw,!0);
577 break;
578 }
579
580 case VIDIOC_STREAMOFF:
581 {
108bdd7a 582 if (!fh->pdi->stream) {
16eb40d3
MI
583 /* No stream defined for this node. This means
584 that we're not currently allowed to stream from
585 this node. */
586 ret = -EPERM;
587 break;
588 }
d855497e
MI
589 ret = pvr2_hdw_set_streaming(hdw,0);
590 break;
591 }
592
593 case VIDIOC_QUERYCTRL:
594 {
595 struct pvr2_ctrl *cptr;
26dd1c57 596 int val;
d855497e
MI
597 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
598 ret = 0;
1d9f8461
MI
599 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
600 cptr = pvr2_hdw_get_ctrl_nextv4l(
601 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
602 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
603 } else {
604 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
605 }
d855497e 606 if (!cptr) {
0885ba1d 607 pvr2_trace(PVR2_TRACE_V4LIOCTL,
a761f431
MI
608 "QUERYCTRL id=0x%x not implemented here",
609 vc->id);
d855497e
MI
610 ret = -EINVAL;
611 break;
612 }
613
a761f431
MI
614 pvr2_trace(PVR2_TRACE_V4LIOCTL,
615 "QUERYCTRL id=0x%x mapping name=%s (%s)",
616 vc->id,pvr2_ctrl_get_name(cptr),
617 pvr2_ctrl_get_desc(cptr));
618 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
619 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
26dd1c57
MI
620 pvr2_ctrl_get_def(cptr, &val);
621 vc->default_value = val;
d855497e
MI
622 switch (pvr2_ctrl_get_type(cptr)) {
623 case pvr2_ctl_enum:
624 vc->type = V4L2_CTRL_TYPE_MENU;
625 vc->minimum = 0;
626 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
627 vc->step = 1;
628 break;
33213963 629 case pvr2_ctl_bool:
1d9f8461 630 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
33213963
MI
631 vc->minimum = 0;
632 vc->maximum = 1;
633 vc->step = 1;
634 break;
d855497e
MI
635 case pvr2_ctl_int:
636 vc->type = V4L2_CTRL_TYPE_INTEGER;
637 vc->minimum = pvr2_ctrl_get_min(cptr);
638 vc->maximum = pvr2_ctrl_get_max(cptr);
639 vc->step = 1;
640 break;
641 default:
0885ba1d 642 pvr2_trace(PVR2_TRACE_V4LIOCTL,
a761f431
MI
643 "QUERYCTRL id=0x%x name=%s not mappable",
644 vc->id,pvr2_ctrl_get_name(cptr));
d855497e
MI
645 ret = -EINVAL;
646 break;
647 }
648 break;
649 }
650
651 case VIDIOC_QUERYMENU:
652 {
653 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
654 unsigned int cnt = 0;
655 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
656 vm->index,
657 vm->name,sizeof(vm->name)-1,
658 &cnt);
659 vm->name[cnt] = 0;
660 break;
661 }
662
663 case VIDIOC_G_CTRL:
664 {
665 struct v4l2_control *vc = (struct v4l2_control *)arg;
666 int val = 0;
667 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
668 &val);
669 vc->value = val;
670 break;
671 }
672
673 case VIDIOC_S_CTRL:
674 {
675 struct v4l2_control *vc = (struct v4l2_control *)arg;
676 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
677 vc->value);
678 break;
679 }
680
1d9f8461
MI
681 case VIDIOC_G_EXT_CTRLS:
682 {
683 struct v4l2_ext_controls *ctls =
684 (struct v4l2_ext_controls *)arg;
685 struct v4l2_ext_control *ctrl;
686 unsigned int idx;
687 int val;
c1c2680d 688 ret = 0;
1d9f8461
MI
689 for (idx = 0; idx < ctls->count; idx++) {
690 ctrl = ctls->controls + idx;
691 ret = pvr2_ctrl_get_value(
692 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
693 if (ret) {
694 ctls->error_idx = idx;
695 break;
696 }
697 /* Ensure that if read as a 64 bit value, the user
698 will still get a hopefully sane value */
699 ctrl->value64 = 0;
700 ctrl->value = val;
701 }
702 break;
703 }
704
705 case VIDIOC_S_EXT_CTRLS:
706 {
707 struct v4l2_ext_controls *ctls =
708 (struct v4l2_ext_controls *)arg;
709 struct v4l2_ext_control *ctrl;
710 unsigned int idx;
c1c2680d 711 ret = 0;
1d9f8461
MI
712 for (idx = 0; idx < ctls->count; idx++) {
713 ctrl = ctls->controls + idx;
714 ret = pvr2_ctrl_set_value(
715 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
716 ctrl->value);
717 if (ret) {
718 ctls->error_idx = idx;
719 break;
720 }
721 }
722 break;
723 }
724
725 case VIDIOC_TRY_EXT_CTRLS:
726 {
727 struct v4l2_ext_controls *ctls =
728 (struct v4l2_ext_controls *)arg;
729 struct v4l2_ext_control *ctrl;
730 struct pvr2_ctrl *pctl;
731 unsigned int idx;
732 /* For the moment just validate that the requested control
733 actually exists. */
c1c2680d 734 ret = 0;
1d9f8461
MI
735 for (idx = 0; idx < ctls->count; idx++) {
736 ctrl = ctls->controls + idx;
737 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
738 if (!pctl) {
739 ret = -EINVAL;
740 ctls->error_idx = idx;
741 break;
742 }
743 }
744 break;
745 }
746
432907f7
MI
747 case VIDIOC_CROPCAP:
748 {
749 struct v4l2_cropcap *cap = (struct v4l2_cropcap *)arg;
750 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
751 ret = -EINVAL;
752 break;
753 }
754 ret = pvr2_hdw_get_cropcap(hdw, cap);
755 cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
756 break;
757 }
758 case VIDIOC_G_CROP:
759 {
760 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
761 int val = 0;
762 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
763 ret = -EINVAL;
764 break;
765 }
766 ret = pvr2_ctrl_get_value(
767 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
768 if (ret != 0) {
769 ret = -EINVAL;
770 break;
771 }
772 crop->c.left = val;
773 ret = pvr2_ctrl_get_value(
774 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
775 if (ret != 0) {
776 ret = -EINVAL;
777 break;
778 }
779 crop->c.top = val;
780 ret = pvr2_ctrl_get_value(
781 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
782 if (ret != 0) {
783 ret = -EINVAL;
784 break;
785 }
786 crop->c.width = val;
787 ret = pvr2_ctrl_get_value(
788 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
789 if (ret != 0) {
790 ret = -EINVAL;
791 break;
792 }
793 crop->c.height = val;
794 }
795 case VIDIOC_S_CROP:
796 {
797 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
432907f7
MI
798 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
799 ret = -EINVAL;
800 break;
801 }
432907f7
MI
802 ret = pvr2_ctrl_set_value(
803 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
804 crop->c.left);
805 if (ret != 0) {
806 ret = -EINVAL;
807 break;
808 }
809 ret = pvr2_ctrl_set_value(
810 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
811 crop->c.top);
812 if (ret != 0) {
813 ret = -EINVAL;
814 break;
815 }
816 ret = pvr2_ctrl_set_value(
817 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
818 crop->c.width);
819 if (ret != 0) {
820 ret = -EINVAL;
821 break;
822 }
823 ret = pvr2_ctrl_set_value(
824 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
825 crop->c.height);
826 if (ret != 0) {
827 ret = -EINVAL;
828 break;
829 }
830 }
d855497e
MI
831 case VIDIOC_LOG_STATUS:
832 {
d855497e 833 pvr2_hdw_trigger_module_log(hdw);
d855497e
MI
834 ret = 0;
835 break;
836 }
32ffa9ae 837#ifdef CONFIG_VIDEO_ADV_DEBUG
52ebc763 838 case VIDIOC_DBG_S_REGISTER:
52ebc763 839 case VIDIOC_DBG_G_REGISTER:
32ffa9ae 840 {
f3d092b8 841 u64 val;
aecde8b5 842 struct v4l2_dbg_register *req = (struct v4l2_dbg_register *)arg;
52ebc763 843 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
32ffa9ae 844 ret = pvr2_hdw_register_access(
aecde8b5
HV
845 hdw, &req->match, req->reg,
846 cmd == VIDIOC_DBG_S_REGISTER, &val);
52ebc763 847 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
32ffa9ae
MI
848 break;
849 }
850#endif
d855497e
MI
851
852 default :
08af245d
HV
853 ret = -EINVAL;
854 break;
d855497e
MI
855 }
856
857 pvr2_hdw_commit_ctl(hdw);
858
859 if (ret < 0) {
860 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
0885ba1d 861 pvr2_trace(PVR2_TRACE_V4LIOCTL,
069b7479 862 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret);
d855497e 863 } else {
0885ba1d
MI
864 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
865 pvr2_trace(PVR2_TRACE_V4LIOCTL,
069b7479
HV
866 "pvr2_v4l2_do_ioctl failure, ret=%ld"
867 " command was:", ret);
d855497e
MI
868 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
869 cmd);
870 }
871 }
872 } else {
873 pvr2_trace(PVR2_TRACE_V4LIOCTL,
069b7479
HV
874 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
875 ret, ret);
d855497e
MI
876 }
877 return ret;
878}
879
d855497e
MI
880static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
881{
0f0f257b 882 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
16eb40d3 883 enum pvr2_config cfg = dip->config;
d72baad3
MI
884 char msg[80];
885 unsigned int mcnt;
886
887 /* Construct the unregistration message *before* we actually
888 perform the unregistration step. By doing it this way we don't
889 have to worry about potentially touching deleted resources. */
890 mcnt = scnprintf(msg, sizeof(msg) - 1,
891 "pvrusb2: unregistered device %s [%s]",
892 video_device_node_name(&dip->devbase),
893 pvr2_config_get_name(cfg));
894 msg[mcnt] = 0;
0f0f257b 895
16eb40d3 896 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
75910053
MI
897
898 /* Paranoia */
c2625bff
RD
899 dip->v4lp = NULL;
900 dip->stream = NULL;
75910053
MI
901
902 /* Actual deallocation happens later when all internal references
903 are gone. */
904 video_unregister_device(&dip->devbase);
0f0f257b 905
d72baad3 906 printk(KERN_INFO "%s\n", msg);
0f0f257b 907
d855497e
MI
908}
909
910
4a89baa2
MI
911static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
912{
913 if (!dip) return;
914 if (!dip->devbase.parent) return;
915 dip->devbase.parent = NULL;
916 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
917}
918
919
d855497e
MI
920static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
921{
0f0f257b
MI
922 if (vp->dev_video) {
923 pvr2_v4l2_dev_destroy(vp->dev_video);
89952d13 924 vp->dev_video = NULL;
0f0f257b
MI
925 }
926 if (vp->dev_radio) {
927 pvr2_v4l2_dev_destroy(vp->dev_radio);
89952d13 928 vp->dev_radio = NULL;
0f0f257b 929 }
d855497e
MI
930
931 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
932 pvr2_channel_done(&vp->channel);
933 kfree(vp);
934}
935
936
75910053
MI
937static void pvr2_video_device_release(struct video_device *vdev)
938{
939 struct pvr2_v4l2_dev *dev;
940 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
941 kfree(dev);
942}
943
944
07e337ee 945static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
d855497e
MI
946{
947 struct pvr2_v4l2 *vp;
948 vp = container_of(chp,struct pvr2_v4l2,channel);
949 if (!vp->channel.mc_head->disconnect_flag) return;
4a89baa2
MI
950 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
951 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
d855497e
MI
952 if (vp->vfirst) return;
953 pvr2_v4l2_destroy_no_lock(vp);
954}
955
956
069b7479 957static long pvr2_v4l2_ioctl(struct file *file,
07e337ee 958 unsigned int cmd, unsigned long arg)
d855497e
MI
959{
960
f473bf76 961 return video_usercopy(file, cmd, arg, pvr2_v4l2_do_ioctl);
d855497e
MI
962}
963
964
bec43661 965static int pvr2_v4l2_release(struct file *file)
d855497e
MI
966{
967 struct pvr2_v4l2_fh *fhp = file->private_data;
968 struct pvr2_v4l2 *vp = fhp->vhead;
c74e0062 969 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
d855497e
MI
970
971 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
972
973 if (fhp->rhp) {
974 struct pvr2_stream *sp;
d855497e
MI
975 pvr2_hdw_set_streaming(hdw,0);
976 sp = pvr2_ioread_get_stream(fhp->rhp);
a0fd1cb1 977 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
d855497e 978 pvr2_ioread_destroy(fhp->rhp);
a0fd1cb1 979 fhp->rhp = NULL;
d855497e 980 }
ae2b9e25 981
ffb4877b 982 v4l2_prio_close(&vp->prio, fhp->prio);
d855497e
MI
983 file->private_data = NULL;
984
794b1607
MI
985 if (fhp->vnext) {
986 fhp->vnext->vprev = fhp->vprev;
987 } else {
988 vp->vlast = fhp->vprev;
989 }
990 if (fhp->vprev) {
991 fhp->vprev->vnext = fhp->vnext;
992 } else {
993 vp->vfirst = fhp->vnext;
994 }
995 fhp->vnext = NULL;
996 fhp->vprev = NULL;
997 fhp->vhead = NULL;
998 pvr2_channel_done(&fhp->channel);
999 pvr2_trace(PVR2_TRACE_STRUCT,
1000 "Destroying pvr_v4l2_fh id=%p",fhp);
e57b1c80
MI
1001 if (fhp->input_map) {
1002 kfree(fhp->input_map);
1003 fhp->input_map = NULL;
1004 }
794b1607
MI
1005 kfree(fhp);
1006 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
1007 pvr2_v4l2_destroy_no_lock(vp);
1008 }
d855497e
MI
1009 return 0;
1010}
1011
1012
bec43661 1013static int pvr2_v4l2_open(struct file *file)
d855497e 1014{
75910053 1015 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
d855497e
MI
1016 struct pvr2_v4l2_fh *fhp;
1017 struct pvr2_v4l2 *vp;
1018 struct pvr2_hdw *hdw;
1cb03b76 1019 unsigned int input_mask = 0;
e57b1c80 1020 unsigned int input_cnt,idx;
1cb03b76 1021 int ret = 0;
d855497e 1022
75910053 1023 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
d855497e
MI
1024
1025 vp = dip->v4lp;
1026 hdw = vp->channel.hdw;
1027
1028 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
1029
1030 if (!pvr2_hdw_dev_ok(hdw)) {
1031 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
1032 "pvr2_v4l2_open: hardware not ready");
1033 return -EIO;
1034 }
1035
4b85dee6 1036 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
d855497e
MI
1037 if (!fhp) {
1038 return -ENOMEM;
1039 }
d855497e
MI
1040
1041 init_waitqueue_head(&fhp->wait_data);
108bdd7a 1042 fhp->pdi = dip;
d855497e 1043
794b1607
MI
1044 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1045 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
ae2b9e25 1046
1cb03b76
MI
1047 if (dip->v4l_type == VFL_TYPE_RADIO) {
1048 /* Opening device as a radio, legal input selection subset
1049 is just the radio. */
1050 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1051 } else {
1052 /* Opening the main V4L device, legal input selection
1053 subset includes all analog inputs. */
1054 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1055 (1 << PVR2_CVAL_INPUT_TV) |
1056 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1057 (1 << PVR2_CVAL_INPUT_SVIDEO));
1058 }
1059 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1060 if (ret) {
1061 pvr2_channel_done(&fhp->channel);
1062 pvr2_trace(PVR2_TRACE_STRUCT,
1063 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1064 fhp);
1065
1066 kfree(fhp);
1067 return ret;
1068 }
1069
e57b1c80
MI
1070 input_mask &= pvr2_hdw_get_input_available(hdw);
1071 input_cnt = 0;
1072 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1073 if (input_mask & (1 << idx)) input_cnt++;
1074 }
1075 fhp->input_cnt = input_cnt;
1076 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1077 if (!fhp->input_map) {
1078 pvr2_channel_done(&fhp->channel);
1079 pvr2_trace(PVR2_TRACE_STRUCT,
1080 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1081 fhp);
1082 kfree(fhp);
1083 return -ENOMEM;
1084 }
1085 input_cnt = 0;
1086 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1087 if (!(input_mask & (1 << idx))) continue;
1088 fhp->input_map[input_cnt++] = idx;
1089 }
1090
794b1607
MI
1091 fhp->vnext = NULL;
1092 fhp->vprev = vp->vlast;
1093 if (vp->vlast) {
1094 vp->vlast->vnext = fhp;
1095 } else {
1096 vp->vfirst = fhp;
1097 }
1098 vp->vlast = fhp;
1099 fhp->vhead = vp;
1100
d855497e
MI
1101 fhp->file = file;
1102 file->private_data = fhp;
ffb4877b 1103 v4l2_prio_open(&vp->prio, &fhp->prio);
d855497e
MI
1104
1105 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1106
1107 return 0;
1108}
1109
1110
1111static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1112{
1113 wake_up(&fhp->wait_data);
1114}
1115
1116static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1117{
1118 int ret;
1119 struct pvr2_stream *sp;
1120 struct pvr2_hdw *hdw;
1121 if (fh->rhp) return 0;
1122
108bdd7a 1123 if (!fh->pdi->stream) {
16eb40d3
MI
1124 /* No stream defined for this node. This means that we're
1125 not currently allowed to stream from this node. */
1126 return -EPERM;
1127 }
1128
d855497e
MI
1129 /* First read() attempt. Try to claim the stream and start
1130 it... */
1131 if ((ret = pvr2_channel_claim_stream(&fh->channel,
108bdd7a 1132 fh->pdi->stream)) != 0) {
d855497e
MI
1133 /* Someone else must already have it */
1134 return ret;
1135 }
1136
108bdd7a 1137 fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
d855497e 1138 if (!fh->rhp) {
a0fd1cb1 1139 pvr2_channel_claim_stream(&fh->channel,NULL);
d855497e
MI
1140 return -ENOMEM;
1141 }
1142
1143 hdw = fh->channel.mc_head->hdw;
108bdd7a 1144 sp = fh->pdi->stream->stream;
d855497e 1145 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
108bdd7a 1146 pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
681c7399
MI
1147 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1148 return pvr2_ioread_set_enabled(fh->rhp,!0);
d855497e
MI
1149}
1150
1151
1152static ssize_t pvr2_v4l2_read(struct file *file,
1153 char __user *buff, size_t count, loff_t *ppos)
1154{
1155 struct pvr2_v4l2_fh *fh = file->private_data;
1156 int ret;
1157
1158 if (fh->fw_mode_flag) {
1159 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1160 char *tbuf;
1161 int c1,c2;
1162 int tcnt = 0;
1163 unsigned int offs = *ppos;
1164
1165 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1166 if (!tbuf) return -ENOMEM;
1167
1168 while (count) {
1169 c1 = count;
1170 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1171 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1172 if (c2 < 0) {
1173 tcnt = c2;
1174 break;
1175 }
1176 if (!c2) break;
1177 if (copy_to_user(buff,tbuf,c2)) {
1178 tcnt = -EFAULT;
1179 break;
1180 }
1181 offs += c2;
1182 tcnt += c2;
1183 buff += c2;
1184 count -= c2;
1185 *ppos += c2;
1186 }
1187 kfree(tbuf);
1188 return tcnt;
1189 }
1190
1191 if (!fh->rhp) {
1192 ret = pvr2_v4l2_iosetup(fh);
1193 if (ret) {
1194 return ret;
1195 }
1196 }
1197
1198 for (;;) {
1199 ret = pvr2_ioread_read(fh->rhp,buff,count);
1200 if (ret >= 0) break;
1201 if (ret != -EAGAIN) break;
1202 if (file->f_flags & O_NONBLOCK) break;
1203 /* Doing blocking I/O. Wait here. */
1204 ret = wait_event_interruptible(
1205 fh->wait_data,
1206 pvr2_ioread_avail(fh->rhp) >= 0);
1207 if (ret < 0) break;
1208 }
1209
1210 return ret;
1211}
1212
1213
1214static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1215{
1216 unsigned int mask = 0;
1217 struct pvr2_v4l2_fh *fh = file->private_data;
1218 int ret;
1219
1220 if (fh->fw_mode_flag) {
1221 mask |= POLLIN | POLLRDNORM;
1222 return mask;
1223 }
1224
1225 if (!fh->rhp) {
1226 ret = pvr2_v4l2_iosetup(fh);
1227 if (ret) return POLLERR;
1228 }
1229
1230 poll_wait(file,&fh->wait_data,wait);
1231
1232 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1233 mask |= POLLIN | POLLRDNORM;
1234 }
1235
1236 return mask;
1237}
1238
1239
bec43661 1240static const struct v4l2_file_operations vdev_fops = {
d855497e
MI
1241 .owner = THIS_MODULE,
1242 .open = pvr2_v4l2_open,
1243 .release = pvr2_v4l2_release,
1244 .read = pvr2_v4l2_read,
1245 .ioctl = pvr2_v4l2_ioctl,
d855497e
MI
1246 .poll = pvr2_v4l2_poll,
1247};
1248
1249
d855497e 1250static struct video_device vdev_template = {
d855497e
MI
1251 .fops = &vdev_fops,
1252};
1253
1254
1255static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1256 struct pvr2_v4l2 *vp,
16eb40d3 1257 int v4l_type)
d855497e 1258{
4a89baa2 1259 struct usb_device *usbdev;
d855497e
MI
1260 int mindevnum;
1261 int unit_number;
89952d13 1262 int *nr_ptr = NULL;
d855497e 1263 dip->v4lp = vp;
d855497e 1264
4a89baa2 1265 usbdev = pvr2_hdw_get_dev(vp->channel.mc_head->hdw);
16eb40d3
MI
1266 dip->v4l_type = v4l_type;
1267 switch (v4l_type) {
1268 case VFL_TYPE_GRABBER:
d855497e 1269 dip->stream = &vp->channel.mc_head->video_stream;
16eb40d3
MI
1270 dip->config = pvr2_config_mpeg;
1271 dip->minor_type = pvr2_v4l_type_video;
1272 nr_ptr = video_nr;
c74e0062 1273 if (!dip->stream) {
be9ed511
MCC
1274 pr_err(KBUILD_MODNAME
1275 ": Failed to set up pvrusb2 v4l video dev"
1276 " due to missing stream instance\n");
c74e0062
MI
1277 return;
1278 }
d855497e 1279 break;
16eb40d3
MI
1280 case VFL_TYPE_VBI:
1281 dip->config = pvr2_config_vbi;
1282 dip->minor_type = pvr2_v4l_type_vbi;
1283 nr_ptr = vbi_nr;
d855497e 1284 break;
16eb40d3 1285 case VFL_TYPE_RADIO:
af78a48b
MI
1286 dip->stream = &vp->channel.mc_head->video_stream;
1287 dip->config = pvr2_config_mpeg;
16eb40d3
MI
1288 dip->minor_type = pvr2_v4l_type_radio;
1289 nr_ptr = radio_nr;
d855497e
MI
1290 break;
1291 default:
1292 /* Bail out (this should be impossible) */
be9ed511
MCC
1293 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
1294 " due to unrecognized config\n");
d855497e
MI
1295 return;
1296 }
1297
75910053
MI
1298 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1299 dip->devbase.release = pvr2_video_device_release;
d855497e
MI
1300
1301 mindevnum = -1;
1302 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
16eb40d3
MI
1303 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1304 mindevnum = nr_ptr[unit_number];
d855497e 1305 }
4a89baa2 1306 dip->devbase.parent = &usbdev->dev;
16eb40d3
MI
1307 if ((video_register_device(&dip->devbase,
1308 dip->v4l_type, mindevnum) < 0) &&
1309 (video_register_device(&dip->devbase,
1310 dip->v4l_type, -1) < 0)) {
be9ed511
MCC
1311 pr_err(KBUILD_MODNAME
1312 ": Failed to register pvrusb2 v4l device\n");
ae2b9e25 1313 }
16eb40d3 1314
38c7c036
LP
1315 printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1316 video_device_node_name(&dip->devbase),
16eb40d3 1317 pvr2_config_get_name(dip->config));
d855497e 1318
d855497e 1319 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
16eb40d3 1320 dip->minor_type,dip->devbase.minor);
d855497e
MI
1321}
1322
1323
1324struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1325{
1326 struct pvr2_v4l2 *vp;
1327
4b85dee6 1328 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
d855497e 1329 if (!vp) return vp;
d855497e
MI
1330 pvr2_channel_init(&vp->channel,mnp);
1331 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1332
1333 vp->channel.check_func = pvr2_v4l2_internal_check;
1334
1335 /* register streams */
beb0ecd7
MI
1336 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1337 if (!vp->dev_video) goto fail;
16eb40d3 1338 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
e57b1c80
MI
1339 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1340 (1 << PVR2_CVAL_INPUT_RADIO)) {
beb0ecd7
MI
1341 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1342 if (!vp->dev_radio) goto fail;
1343 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1344 }
d855497e
MI
1345
1346 return vp;
beb0ecd7
MI
1347 fail:
1348 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1349 pvr2_v4l2_destroy_no_lock(vp);
a6a3a17b 1350 return NULL;
d855497e
MI
1351}
1352
1353/*
1354 Stuff for Emacs to see, in order to encourage consistent editing style:
1355 *** Local Variables: ***
1356 *** mode: c ***
1357 *** fill-column: 75 ***
1358 *** tab-width: 8 ***
1359 *** c-basic-offset: 8 ***
1360 *** End: ***
1361 */