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