blob: 97e974d9b9c34ba8b215f0a61eb9a04f96e34008 [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <linux/kernel.h>
Mauro Carvalho Chehabce4260c2006-06-26 22:26:08 -030024#include <linux/version.h>
Mike Iselyd8554972006-06-26 20:58:46 -030025#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>
Mike Isely43e06022006-09-23 23:47:50 -030032#include <media/v4l2-dev.h>
Mike Iselyd8554972006-06-26 20:58:46 -030033#include <media/v4l2-common.h>
34
35struct pvr2_v4l2_dev;
36struct pvr2_v4l2_fh;
37struct pvr2_v4l2;
38
Mike Iselyd8554972006-06-26 20:58:46 -030039struct pvr2_v4l2_dev {
Mike Isely75910052006-09-23 22:30:50 -030040 struct video_device devbase; /* MUST be first! */
Mike Iselyd8554972006-06-26 20:58:46 -030041 struct pvr2_v4l2 *v4lp;
Mike Iselyd8554972006-06-26 20:58:46 -030042 struct pvr2_context_stream *stream;
Mike Iselyd8554972006-06-26 20:58:46 -030043 enum pvr2_config config;
44};
45
46struct pvr2_v4l2_fh {
47 struct pvr2_channel channel;
48 struct pvr2_v4l2_dev *dev_info;
49 enum v4l2_priority prio;
50 struct pvr2_ioread *rhp;
51 struct file *file;
52 struct pvr2_v4l2 *vhead;
53 struct pvr2_v4l2_fh *vnext;
54 struct pvr2_v4l2_fh *vprev;
55 wait_queue_head_t wait_data;
56 int fw_mode_flag;
57};
58
59struct pvr2_v4l2 {
60 struct pvr2_channel channel;
61 struct pvr2_v4l2_fh *vfirst;
62 struct pvr2_v4l2_fh *vlast;
63
64 struct v4l2_prio_state prio;
65
66 /* streams */
Mike Isely75910052006-09-23 22:30:50 -030067 struct pvr2_v4l2_dev *vdev;
Mike Iselyd8554972006-06-26 20:58:46 -030068};
69
70static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
71module_param_array(video_nr, int, NULL, 0444);
72MODULE_PARM_DESC(video_nr, "Offset for device's minor");
73
Adrian Bunk07e337e2006-06-30 11:30:20 -030074static struct v4l2_capability pvr_capability ={
Mike Iselyd8554972006-06-26 20:58:46 -030075 .driver = "pvrusb2",
76 .card = "Hauppauge WinTV pvr-usb2",
77 .bus_info = "usb",
78 .version = KERNEL_VERSION(0,8,0),
79 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
80 V4L2_CAP_TUNER | V4L2_CAP_AUDIO |
81 V4L2_CAP_READWRITE),
82 .reserved = {0,0,0,0}
83};
84
85static struct v4l2_tuner pvr_v4l2_tuners[]= {
86 {
87 .index = 0,
88 .name = "TV Tuner",
89 .type = V4L2_TUNER_ANALOG_TV,
90 .capability = (V4L2_TUNER_CAP_NORM |
91 V4L2_TUNER_CAP_STEREO |
92 V4L2_TUNER_CAP_LANG1 |
93 V4L2_TUNER_CAP_LANG2),
94 .rangelow = 0,
95 .rangehigh = 0,
96 .rxsubchans = V4L2_TUNER_SUB_STEREO,
97 .audmode = V4L2_TUNER_MODE_STEREO,
98 .signal = 0,
99 .afc = 0,
100 .reserved = {0,0,0,0}
101 }
102};
103
Adrian Bunk07e337e2006-06-30 11:30:20 -0300104static struct v4l2_fmtdesc pvr_fmtdesc [] = {
Mike Iselyd8554972006-06-26 20:58:46 -0300105 {
106 .index = 0,
107 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
108 .flags = V4L2_FMT_FLAG_COMPRESSED,
109 .description = "MPEG1/2",
110 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
111 // breaks when I do that.
112 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
113 .reserved = { 0, 0, 0, 0 }
114 }
115};
116
117#define PVR_FORMAT_PIX 0
118#define PVR_FORMAT_VBI 1
119
Adrian Bunk07e337e2006-06-30 11:30:20 -0300120static struct v4l2_format pvr_format [] = {
Mike Iselyd8554972006-06-26 20:58:46 -0300121 [PVR_FORMAT_PIX] = {
122 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
123 .fmt = {
124 .pix = {
125 .width = 720,
126 .height = 576,
127 // This should really be V4L2_PIX_FMT_MPEG,
128 // but xawtv breaks when I do that.
129 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
130 .field = V4L2_FIELD_INTERLACED,
131 .bytesperline = 0, // doesn't make sense
132 // here
133 //FIXME : Don't know what to put here...
134 .sizeimage = (32*1024),
135 .colorspace = 0, // doesn't make sense here
136 .priv = 0
137 }
138 }
139 },
140 [PVR_FORMAT_VBI] = {
141 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
142 .fmt = {
143 .vbi = {
144 .sampling_rate = 27000000,
145 .offset = 248,
146 .samples_per_line = 1443,
147 .sample_format = V4L2_PIX_FMT_GREY,
148 .start = { 0, 0 },
149 .count = { 0, 0 },
150 .flags = 0,
151 .reserved = { 0, 0 }
152 }
153 }
154 }
155};
156
157/*
158 * pvr_ioctl()
159 *
160 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
161 *
162 */
163static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
164 unsigned int cmd, void *arg)
165{
166 struct pvr2_v4l2_fh *fh = file->private_data;
167 struct pvr2_v4l2 *vp = fh->vhead;
168 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
169 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
170 int ret = -EINVAL;
171
172 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
173 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
174 }
175
176 if (!pvr2_hdw_dev_ok(hdw)) {
177 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
178 "ioctl failed - bad or no context");
179 return -EFAULT;
180 }
181
182 /* check priority */
183 switch (cmd) {
184 case VIDIOC_S_CTRL:
185 case VIDIOC_S_STD:
186 case VIDIOC_S_INPUT:
187 case VIDIOC_S_TUNER:
188 case VIDIOC_S_FREQUENCY:
189 ret = v4l2_prio_check(&vp->prio, &fh->prio);
190 if (ret)
191 return ret;
192 }
193
194 switch (cmd) {
195 case VIDIOC_QUERYCAP:
196 {
197 struct v4l2_capability *cap = arg;
198
199 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
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;
253
254 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
255
256 memset(&tmp,0,sizeof(tmp));
257 tmp.index = vi->index;
258 ret = 0;
259 switch (vi->index) {
260 case PVR2_CVAL_INPUT_TV:
261 case PVR2_CVAL_INPUT_RADIO:
262 tmp.type = V4L2_INPUT_TYPE_TUNER;
263 break;
264 case PVR2_CVAL_INPUT_SVIDEO:
265 case PVR2_CVAL_INPUT_COMPOSITE:
266 tmp.type = V4L2_INPUT_TYPE_CAMERA;
267 break;
268 default:
269 ret = -EINVAL;
270 break;
271 }
272 if (ret < 0) break;
273
274 cnt = 0;
275 pvr2_ctrl_get_valname(cptr,vi->index,
276 tmp.name,sizeof(tmp.name)-1,&cnt);
277 tmp.name[cnt] = 0;
278
279 /* Don't bother with audioset, since this driver currently
280 always switches the audio whenever the video is
281 switched. */
282
283 /* Handling std is a tougher problem. It doesn't make
284 sense in cases where a device might be multi-standard.
285 We could just copy out the current value for the
286 standard, but it can change over time. For now just
287 leave it zero. */
288
289 memcpy(vi, &tmp, sizeof(tmp));
290
291 ret = 0;
292 break;
293 }
294
295 case VIDIOC_G_INPUT:
296 {
297 struct pvr2_ctrl *cptr;
298 struct v4l2_input *vi = (struct v4l2_input *)arg;
299 int val;
300 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
301 val = 0;
302 ret = pvr2_ctrl_get_value(cptr,&val);
303 vi->index = val;
304 break;
305 }
306
307 case VIDIOC_S_INPUT:
308 {
309 struct v4l2_input *vi = (struct v4l2_input *)arg;
310 ret = pvr2_ctrl_set_value(
311 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
312 vi->index);
313 break;
314 }
315
316 case VIDIOC_ENUMAUDIO:
317 {
318 ret = -EINVAL;
319 break;
320 }
321
322 case VIDIOC_G_AUDIO:
323 {
324 ret = -EINVAL;
325 break;
326 }
327
328 case VIDIOC_S_AUDIO:
329 {
330 ret = -EINVAL;
331 break;
332 }
333 case VIDIOC_G_TUNER:
334 {
335 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
336 unsigned int status_mask;
337 int val;
338 if (vt->index !=0) break;
339
340 status_mask = pvr2_hdw_get_signal_status(hdw);
341
342 memcpy(vt, &pvr_v4l2_tuners[vt->index],
343 sizeof(struct v4l2_tuner));
344
345 vt->signal = 0;
346 if (status_mask & PVR2_SIGNAL_OK) {
347 if (status_mask & PVR2_SIGNAL_STEREO) {
348 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
349 } else {
350 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
351 }
352 if (status_mask & PVR2_SIGNAL_SAP) {
353 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
354 V4L2_TUNER_SUB_LANG2);
355 }
356 vt->signal = 65535;
357 }
358
359 val = 0;
360 ret = pvr2_ctrl_get_value(
361 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
362 &val);
363 vt->audmode = val;
364 break;
365 }
366
367 case VIDIOC_S_TUNER:
368 {
369 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
370
371 if (vt->index != 0)
372 break;
373
374 ret = pvr2_ctrl_set_value(
375 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
376 vt->audmode);
377 }
378
379 case VIDIOC_S_FREQUENCY:
380 {
381 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
382 ret = pvr2_ctrl_set_value(
383 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
384 vf->frequency * 62500);
385 break;
386 }
387
388 case VIDIOC_G_FREQUENCY:
389 {
390 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
391 int val = 0;
392 ret = pvr2_ctrl_get_value(
393 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
394 &val);
395 val /= 62500;
396 vf->frequency = val;
397 break;
398 }
399
400 case VIDIOC_ENUM_FMT:
401 {
402 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
403
404 /* Only one format is supported : mpeg.*/
405 if (fd->index != 0)
406 break;
407
408 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
409 ret = 0;
410 break;
411 }
412
413 case VIDIOC_G_FMT:
414 {
415 struct v4l2_format *vf = (struct v4l2_format *)arg;
416 int val;
417 switch(vf->type) {
418 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
419 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
420 sizeof(struct v4l2_format));
421 val = 0;
422 pvr2_ctrl_get_value(
423 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
424 &val);
425 vf->fmt.pix.width = val;
426 val = 0;
427 pvr2_ctrl_get_value(
Mike Iselyd8554972006-06-26 20:58:46 -0300428 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
429 &val);
430 vf->fmt.pix.height = val;
431 ret = 0;
432 break;
433 case V4L2_BUF_TYPE_VBI_CAPTURE:
434 // ????? Still need to figure out to do VBI correctly
435 ret = -EINVAL;
436 break;
437 default:
438 ret = -EINVAL;
439 break;
440 }
441 break;
442 }
443
444 case VIDIOC_TRY_FMT:
445 case VIDIOC_S_FMT:
446 {
447 struct v4l2_format *vf = (struct v4l2_format *)arg;
448
449 ret = 0;
450 switch(vf->type) {
451 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
Mike Iselye95a1912006-08-08 09:10:07 -0300452 int lmin,lmax;
453 struct pvr2_ctrl *hcp,*vcp;
Mike Iselyd8554972006-06-26 20:58:46 -0300454 int h = vf->fmt.pix.height;
455 int w = vf->fmt.pix.width;
Mike Iselye95a1912006-08-08 09:10:07 -0300456 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
457 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
Mike Iselyd8554972006-06-26 20:58:46 -0300458
Mike Iselye95a1912006-08-08 09:10:07 -0300459 lmin = pvr2_ctrl_get_min(hcp);
460 lmax = pvr2_ctrl_get_max(hcp);
Mike Iselye95a1912006-08-08 09:10:07 -0300461 if (w < lmin) {
462 w = lmin;
463 } else if (w > lmax) {
464 w = lmax;
Mike Isely039c4302006-06-25 20:04:16 -0300465 }
Hans Verkuilb31e3412006-09-01 18:36:10 -0300466 lmin = pvr2_ctrl_get_min(vcp);
467 lmax = pvr2_ctrl_get_max(vcp);
468 if (h < lmin) {
469 h = lmin;
470 } else if (h > lmax) {
471 h = lmax;
472 }
Mike Iselyd8554972006-06-26 20:58:46 -0300473
474 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
475 sizeof(struct v4l2_format));
Mike Isely039c4302006-06-25 20:04:16 -0300476 vf->fmt.pix.width = w;
477 vf->fmt.pix.height = h;
Mike Iselyd8554972006-06-26 20:58:46 -0300478
479 if (cmd == VIDIOC_S_FMT) {
Mike Iselye95a1912006-08-08 09:10:07 -0300480 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
481 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
Mike Iselyd8554972006-06-26 20:58:46 -0300482 }
483 } break;
484 case V4L2_BUF_TYPE_VBI_CAPTURE:
485 // ????? Still need to figure out to do VBI correctly
486 ret = -EINVAL;
487 break;
488 default:
489 ret = -EINVAL;
490 break;
491 }
492 break;
493 }
494
495 case VIDIOC_STREAMON:
496 {
497 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
498 if (ret < 0) return ret;
499 ret = pvr2_hdw_set_streaming(hdw,!0);
500 break;
501 }
502
503 case VIDIOC_STREAMOFF:
504 {
505 ret = pvr2_hdw_set_streaming(hdw,0);
506 break;
507 }
508
509 case VIDIOC_QUERYCTRL:
510 {
511 struct pvr2_ctrl *cptr;
512 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
513 ret = 0;
Mike Isely1d9f8462006-06-25 20:04:58 -0300514 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
515 cptr = pvr2_hdw_get_ctrl_nextv4l(
516 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
517 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
518 } else {
519 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
520 }
Mike Iselyd8554972006-06-26 20:58:46 -0300521 if (!cptr) {
Mike Isely0885ba12006-06-25 21:30:47 -0300522 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselya761f432006-06-25 20:04:44 -0300523 "QUERYCTRL id=0x%x not implemented here",
524 vc->id);
Mike Iselyd8554972006-06-26 20:58:46 -0300525 ret = -EINVAL;
526 break;
527 }
528
Mike Iselya761f432006-06-25 20:04:44 -0300529 pvr2_trace(PVR2_TRACE_V4LIOCTL,
530 "QUERYCTRL id=0x%x mapping name=%s (%s)",
531 vc->id,pvr2_ctrl_get_name(cptr),
532 pvr2_ctrl_get_desc(cptr));
533 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
534 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
Mike Iselyd8554972006-06-26 20:58:46 -0300535 vc->default_value = pvr2_ctrl_get_def(cptr);
536 switch (pvr2_ctrl_get_type(cptr)) {
537 case pvr2_ctl_enum:
538 vc->type = V4L2_CTRL_TYPE_MENU;
539 vc->minimum = 0;
540 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
541 vc->step = 1;
542 break;
Mike Isely33213962006-06-25 20:04:40 -0300543 case pvr2_ctl_bool:
Mike Isely1d9f8462006-06-25 20:04:58 -0300544 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
Mike Isely33213962006-06-25 20:04:40 -0300545 vc->minimum = 0;
546 vc->maximum = 1;
547 vc->step = 1;
548 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300549 case pvr2_ctl_int:
550 vc->type = V4L2_CTRL_TYPE_INTEGER;
551 vc->minimum = pvr2_ctrl_get_min(cptr);
552 vc->maximum = pvr2_ctrl_get_max(cptr);
553 vc->step = 1;
554 break;
555 default:
Mike Isely0885ba12006-06-25 21:30:47 -0300556 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselya761f432006-06-25 20:04:44 -0300557 "QUERYCTRL id=0x%x name=%s not mappable",
558 vc->id,pvr2_ctrl_get_name(cptr));
Mike Iselyd8554972006-06-26 20:58:46 -0300559 ret = -EINVAL;
560 break;
561 }
562 break;
563 }
564
565 case VIDIOC_QUERYMENU:
566 {
567 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
568 unsigned int cnt = 0;
569 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
570 vm->index,
571 vm->name,sizeof(vm->name)-1,
572 &cnt);
573 vm->name[cnt] = 0;
574 break;
575 }
576
577 case VIDIOC_G_CTRL:
578 {
579 struct v4l2_control *vc = (struct v4l2_control *)arg;
580 int val = 0;
581 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
582 &val);
583 vc->value = val;
584 break;
585 }
586
587 case VIDIOC_S_CTRL:
588 {
589 struct v4l2_control *vc = (struct v4l2_control *)arg;
590 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
591 vc->value);
592 break;
593 }
594
Mike Isely1d9f8462006-06-25 20:04:58 -0300595 case VIDIOC_G_EXT_CTRLS:
596 {
597 struct v4l2_ext_controls *ctls =
598 (struct v4l2_ext_controls *)arg;
599 struct v4l2_ext_control *ctrl;
600 unsigned int idx;
601 int val;
602 for (idx = 0; idx < ctls->count; idx++) {
603 ctrl = ctls->controls + idx;
604 ret = pvr2_ctrl_get_value(
605 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
606 if (ret) {
607 ctls->error_idx = idx;
608 break;
609 }
610 /* Ensure that if read as a 64 bit value, the user
611 will still get a hopefully sane value */
612 ctrl->value64 = 0;
613 ctrl->value = val;
614 }
615 break;
616 }
617
618 case VIDIOC_S_EXT_CTRLS:
619 {
620 struct v4l2_ext_controls *ctls =
621 (struct v4l2_ext_controls *)arg;
622 struct v4l2_ext_control *ctrl;
623 unsigned int idx;
624 for (idx = 0; idx < ctls->count; idx++) {
625 ctrl = ctls->controls + idx;
626 ret = pvr2_ctrl_set_value(
627 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
628 ctrl->value);
629 if (ret) {
630 ctls->error_idx = idx;
631 break;
632 }
633 }
634 break;
635 }
636
637 case VIDIOC_TRY_EXT_CTRLS:
638 {
639 struct v4l2_ext_controls *ctls =
640 (struct v4l2_ext_controls *)arg;
641 struct v4l2_ext_control *ctrl;
642 struct pvr2_ctrl *pctl;
643 unsigned int idx;
644 /* For the moment just validate that the requested control
645 actually exists. */
646 for (idx = 0; idx < ctls->count; idx++) {
647 ctrl = ctls->controls + idx;
648 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
649 if (!pctl) {
650 ret = -EINVAL;
651 ctls->error_idx = idx;
652 break;
653 }
654 }
655 break;
656 }
657
Mike Iselyd8554972006-06-26 20:58:46 -0300658 case VIDIOC_LOG_STATUS:
659 {
Mike Iselyd8554972006-06-26 20:58:46 -0300660 pvr2_hdw_trigger_module_log(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300661 ret = 0;
662 break;
663 }
Mike Isely32ffa9a2006-09-23 22:26:52 -0300664#ifdef CONFIG_VIDEO_ADV_DEBUG
665 case VIDIOC_INT_G_REGISTER:
666 case VIDIOC_INT_S_REGISTER:
667 {
668 u32 val;
669 struct v4l2_register *req = (struct v4l2_register *)arg;
670 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
671 ret = pvr2_hdw_register_access(
672 hdw,req->i2c_id,req->reg,
673 cmd == VIDIOC_INT_S_REGISTER,&val);
Mike Isely6d988162006-09-28 17:53:49 -0300674 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
Mike Isely32ffa9a2006-09-23 22:26:52 -0300675 break;
676 }
677#endif
Mike Iselyd8554972006-06-26 20:58:46 -0300678
679 default :
680 ret = v4l_compat_translate_ioctl(inode,file,cmd,
681 arg,pvr2_v4l2_do_ioctl);
682 }
683
684 pvr2_hdw_commit_ctl(hdw);
685
686 if (ret < 0) {
687 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
Mike Isely0885ba12006-06-25 21:30:47 -0300688 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselyd8554972006-06-26 20:58:46 -0300689 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
690 } else {
Mike Isely0885ba12006-06-25 21:30:47 -0300691 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
692 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselyd8554972006-06-26 20:58:46 -0300693 "pvr2_v4l2_do_ioctl failure, ret=%d"
694 " command was:",ret);
695 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
696 cmd);
697 }
698 }
699 } else {
700 pvr2_trace(PVR2_TRACE_V4LIOCTL,
701 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
702 ret,ret);
703 }
704 return ret;
705}
706
707
708static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
709{
Mike Isely47ed3bc2006-06-29 09:26:43 -0300710 printk(KERN_INFO "pvrusb2: unregistering device video%d [%s]\n",
Mike Isely75910052006-09-23 22:30:50 -0300711 dip->devbase.minor,pvr2_config_get_name(dip->config));
712
713 /* Paranoia */
714 dip->v4lp = 0;
715 dip->stream = 0;
716
717 /* Actual deallocation happens later when all internal references
718 are gone. */
719 video_unregister_device(&dip->devbase);
Mike Iselyd8554972006-06-26 20:58:46 -0300720}
721
722
723static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
724{
725 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,-1);
Mike Isely75910052006-09-23 22:30:50 -0300726 pvr2_v4l2_dev_destroy(vp->vdev);
Mike Iselyd8554972006-06-26 20:58:46 -0300727
728 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
729 pvr2_channel_done(&vp->channel);
730 kfree(vp);
731}
732
733
Mike Isely75910052006-09-23 22:30:50 -0300734static void pvr2_video_device_release(struct video_device *vdev)
735{
736 struct pvr2_v4l2_dev *dev;
737 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
738 kfree(dev);
739}
740
741
Adrian Bunk07e337e2006-06-30 11:30:20 -0300742static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
Mike Iselyd8554972006-06-26 20:58:46 -0300743{
744 struct pvr2_v4l2 *vp;
745 vp = container_of(chp,struct pvr2_v4l2,channel);
746 if (!vp->channel.mc_head->disconnect_flag) return;
747 if (vp->vfirst) return;
748 pvr2_v4l2_destroy_no_lock(vp);
749}
750
751
Adrian Bunk07e337e2006-06-30 11:30:20 -0300752static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
753 unsigned int cmd, unsigned long arg)
Mike Iselyd8554972006-06-26 20:58:46 -0300754{
755
756/* Temporary hack : use ivtv api until a v4l2 one is available. */
757#define IVTV_IOC_G_CODEC 0xFFEE7703
758#define IVTV_IOC_S_CODEC 0xFFEE7704
759 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
760 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
761}
762
763
Adrian Bunk07e337e2006-06-30 11:30:20 -0300764static int pvr2_v4l2_release(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300765{
766 struct pvr2_v4l2_fh *fhp = file->private_data;
767 struct pvr2_v4l2 *vp = fhp->vhead;
768 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
769
770 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
771
772 if (fhp->rhp) {
773 struct pvr2_stream *sp;
774 struct pvr2_hdw *hdw;
775 hdw = fhp->channel.mc_head->hdw;
776 pvr2_hdw_set_streaming(hdw,0);
777 sp = pvr2_ioread_get_stream(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300778 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300779 pvr2_ioread_destroy(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300780 fhp->rhp = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300781 }
782 v4l2_prio_close(&vp->prio, &fhp->prio);
783 file->private_data = NULL;
784
785 pvr2_context_enter(mp); do {
786 if (fhp->vnext) {
787 fhp->vnext->vprev = fhp->vprev;
788 } else {
789 vp->vlast = fhp->vprev;
790 }
791 if (fhp->vprev) {
792 fhp->vprev->vnext = fhp->vnext;
793 } else {
794 vp->vfirst = fhp->vnext;
795 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300796 fhp->vnext = NULL;
797 fhp->vprev = NULL;
798 fhp->vhead = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300799 pvr2_channel_done(&fhp->channel);
800 pvr2_trace(PVR2_TRACE_STRUCT,
801 "Destroying pvr_v4l2_fh id=%p",fhp);
802 kfree(fhp);
803 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
804 pvr2_v4l2_destroy_no_lock(vp);
805 }
806 } while (0); pvr2_context_exit(mp);
807 return 0;
808}
809
810
Adrian Bunk07e337e2006-06-30 11:30:20 -0300811static int pvr2_v4l2_open(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300812{
Mike Isely75910052006-09-23 22:30:50 -0300813 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
Mike Iselyd8554972006-06-26 20:58:46 -0300814 struct pvr2_v4l2_fh *fhp;
815 struct pvr2_v4l2 *vp;
816 struct pvr2_hdw *hdw;
817
Mike Isely75910052006-09-23 22:30:50 -0300818 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
Mike Iselyd8554972006-06-26 20:58:46 -0300819
820 vp = dip->v4lp;
821 hdw = vp->channel.hdw;
822
823 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
824
825 if (!pvr2_hdw_dev_ok(hdw)) {
826 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
827 "pvr2_v4l2_open: hardware not ready");
828 return -EIO;
829 }
830
831 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
832 if (!fhp) {
833 return -ENOMEM;
834 }
835 memset(fhp,0,sizeof(*fhp));
836
837 init_waitqueue_head(&fhp->wait_data);
838 fhp->dev_info = dip;
839
840 pvr2_context_enter(vp->channel.mc_head); do {
841 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
842 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300843 fhp->vnext = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300844 fhp->vprev = vp->vlast;
845 if (vp->vlast) {
846 vp->vlast->vnext = fhp;
847 } else {
848 vp->vfirst = fhp;
849 }
850 vp->vlast = fhp;
851 fhp->vhead = vp;
852 } while (0); pvr2_context_exit(vp->channel.mc_head);
853
854 fhp->file = file;
855 file->private_data = fhp;
856 v4l2_prio_open(&vp->prio,&fhp->prio);
857
858 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
859
860 return 0;
861}
862
863
864static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
865{
866 wake_up(&fhp->wait_data);
867}
868
869static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
870{
871 int ret;
872 struct pvr2_stream *sp;
873 struct pvr2_hdw *hdw;
874 if (fh->rhp) return 0;
875
876 /* First read() attempt. Try to claim the stream and start
877 it... */
878 if ((ret = pvr2_channel_claim_stream(&fh->channel,
879 fh->dev_info->stream)) != 0) {
880 /* Someone else must already have it */
881 return ret;
882 }
883
884 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
885 if (!fh->rhp) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300886 pvr2_channel_claim_stream(&fh->channel,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300887 return -ENOMEM;
888 }
889
890 hdw = fh->channel.mc_head->hdw;
891 sp = fh->dev_info->stream->stream;
892 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
893 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
894 pvr2_hdw_set_streaming(hdw,!0);
895 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
896
897 return ret;
898}
899
900
901static ssize_t pvr2_v4l2_read(struct file *file,
902 char __user *buff, size_t count, loff_t *ppos)
903{
904 struct pvr2_v4l2_fh *fh = file->private_data;
905 int ret;
906
907 if (fh->fw_mode_flag) {
908 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
909 char *tbuf;
910 int c1,c2;
911 int tcnt = 0;
912 unsigned int offs = *ppos;
913
914 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
915 if (!tbuf) return -ENOMEM;
916
917 while (count) {
918 c1 = count;
919 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
920 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
921 if (c2 < 0) {
922 tcnt = c2;
923 break;
924 }
925 if (!c2) break;
926 if (copy_to_user(buff,tbuf,c2)) {
927 tcnt = -EFAULT;
928 break;
929 }
930 offs += c2;
931 tcnt += c2;
932 buff += c2;
933 count -= c2;
934 *ppos += c2;
935 }
936 kfree(tbuf);
937 return tcnt;
938 }
939
940 if (!fh->rhp) {
941 ret = pvr2_v4l2_iosetup(fh);
942 if (ret) {
943 return ret;
944 }
945 }
946
947 for (;;) {
948 ret = pvr2_ioread_read(fh->rhp,buff,count);
949 if (ret >= 0) break;
950 if (ret != -EAGAIN) break;
951 if (file->f_flags & O_NONBLOCK) break;
952 /* Doing blocking I/O. Wait here. */
953 ret = wait_event_interruptible(
954 fh->wait_data,
955 pvr2_ioread_avail(fh->rhp) >= 0);
956 if (ret < 0) break;
957 }
958
959 return ret;
960}
961
962
963static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
964{
965 unsigned int mask = 0;
966 struct pvr2_v4l2_fh *fh = file->private_data;
967 int ret;
968
969 if (fh->fw_mode_flag) {
970 mask |= POLLIN | POLLRDNORM;
971 return mask;
972 }
973
974 if (!fh->rhp) {
975 ret = pvr2_v4l2_iosetup(fh);
976 if (ret) return POLLERR;
977 }
978
979 poll_wait(file,&fh->wait_data,wait);
980
981 if (pvr2_ioread_avail(fh->rhp) >= 0) {
982 mask |= POLLIN | POLLRDNORM;
983 }
984
985 return mask;
986}
987
988
989static struct file_operations vdev_fops = {
990 .owner = THIS_MODULE,
991 .open = pvr2_v4l2_open,
992 .release = pvr2_v4l2_release,
993 .read = pvr2_v4l2_read,
994 .ioctl = pvr2_v4l2_ioctl,
995 .llseek = no_llseek,
996 .poll = pvr2_v4l2_poll,
997};
998
999
1000#define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1001
1002static struct video_device vdev_template = {
1003 .owner = THIS_MODULE,
1004 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1005 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1006 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1007 | V4L2_CAP_READWRITE),
1008 .hardware = VID_HARDWARE_PVRUSB2,
1009 .fops = &vdev_fops,
1010};
1011
1012
1013static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1014 struct pvr2_v4l2 *vp,
1015 enum pvr2_config cfg)
1016{
1017 int mindevnum;
1018 int unit_number;
1019 int v4l_type;
1020 dip->v4lp = vp;
1021 dip->config = cfg;
1022
1023
1024 switch (cfg) {
1025 case pvr2_config_mpeg:
1026 v4l_type = VFL_TYPE_GRABBER;
1027 dip->stream = &vp->channel.mc_head->video_stream;
1028 break;
1029 case pvr2_config_vbi:
1030 v4l_type = VFL_TYPE_VBI;
1031 break;
1032 case pvr2_config_radio:
1033 v4l_type = VFL_TYPE_RADIO;
1034 break;
1035 default:
1036 /* Bail out (this should be impossible) */
1037 err("Failed to set up pvrusb2 v4l dev"
1038 " due to unrecognized config");
1039 return;
1040 }
1041
1042 if (!dip->stream) {
1043 err("Failed to set up pvrusb2 v4l dev"
1044 " due to missing stream instance");
1045 return;
1046 }
1047
Mike Isely75910052006-09-23 22:30:50 -03001048 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1049 dip->devbase.release = pvr2_video_device_release;
Mike Iselyd8554972006-06-26 20:58:46 -03001050
1051 mindevnum = -1;
1052 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1053 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1054 mindevnum = video_nr[unit_number];
1055 }
Mike Isely75910052006-09-23 22:30:50 -03001056 if ((video_register_device(&dip->devbase, v4l_type, mindevnum) < 0) &&
1057 (video_register_device(&dip->devbase, v4l_type, -1) < 0)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001058 err("Failed to register pvrusb2 v4l video device");
1059 } else {
Mike Isely47ed3bc2006-06-29 09:26:43 -03001060 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
Mike Isely75910052006-09-23 22:30:50 -03001061 dip->devbase.minor,pvr2_config_get_name(dip->config));
Mike Iselyd8554972006-06-26 20:58:46 -03001062 }
1063
Mike Iselyd8554972006-06-26 20:58:46 -03001064 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
Mike Isely75910052006-09-23 22:30:50 -03001065 dip->devbase.minor);
Mike Iselyd8554972006-06-26 20:58:46 -03001066}
1067
1068
1069struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1070{
1071 struct pvr2_v4l2 *vp;
1072
1073 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1074 if (!vp) return vp;
1075 memset(vp,0,sizeof(*vp));
Mike Isely75910052006-09-23 22:30:50 -03001076 vp->vdev = kmalloc(sizeof(*vp->vdev),GFP_KERNEL);
1077 if (!vp->vdev) {
1078 kfree(vp);
1079 return 0;
1080 }
1081 memset(vp->vdev,0,sizeof(*vp->vdev));
Mike Iselyd8554972006-06-26 20:58:46 -03001082 pvr2_channel_init(&vp->channel,mnp);
1083 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1084
1085 vp->channel.check_func = pvr2_v4l2_internal_check;
1086
1087 /* register streams */
Mike Isely75910052006-09-23 22:30:50 -03001088 pvr2_v4l2_dev_init(vp->vdev,vp,pvr2_config_mpeg);
Mike Iselyd8554972006-06-26 20:58:46 -03001089
1090 return vp;
1091}
1092
1093/*
1094 Stuff for Emacs to see, in order to encourage consistent editing style:
1095 *** Local Variables: ***
1096 *** mode: c ***
1097 *** fill-column: 75 ***
1098 *** tab-width: 8 ***
1099 *** c-basic-offset: 8 ***
1100 *** End: ***
1101 */