blob: 02a541fbeff9e32c118be57fc424b5831614ddc1 [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 */
Randy Dunlapc2625bf2006-10-29 11:12:27 -0300714 dip->v4lp = NULL;
715 dip->stream = NULL;
Mike Isely75910052006-09-23 22:30:50 -0300716
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{
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300725 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
726 pvr2_config_mpeg-1,-1);
727 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
728 pvr2_config_vbi-1,-1);
729 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
730 pvr2_config_radio-1,-1);
Mike Isely75910052006-09-23 22:30:50 -0300731 pvr2_v4l2_dev_destroy(vp->vdev);
Mike Iselyd8554972006-06-26 20:58:46 -0300732
733 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
734 pvr2_channel_done(&vp->channel);
735 kfree(vp);
736}
737
738
Mike Isely75910052006-09-23 22:30:50 -0300739static void pvr2_video_device_release(struct video_device *vdev)
740{
741 struct pvr2_v4l2_dev *dev;
742 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
743 kfree(dev);
744}
745
746
Adrian Bunk07e337e2006-06-30 11:30:20 -0300747static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
Mike Iselyd8554972006-06-26 20:58:46 -0300748{
749 struct pvr2_v4l2 *vp;
750 vp = container_of(chp,struct pvr2_v4l2,channel);
751 if (!vp->channel.mc_head->disconnect_flag) return;
752 if (vp->vfirst) return;
753 pvr2_v4l2_destroy_no_lock(vp);
754}
755
756
Adrian Bunk07e337e2006-06-30 11:30:20 -0300757static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
758 unsigned int cmd, unsigned long arg)
Mike Iselyd8554972006-06-26 20:58:46 -0300759{
760
761/* Temporary hack : use ivtv api until a v4l2 one is available. */
762#define IVTV_IOC_G_CODEC 0xFFEE7703
763#define IVTV_IOC_S_CODEC 0xFFEE7704
764 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
765 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
766}
767
768
Adrian Bunk07e337e2006-06-30 11:30:20 -0300769static int pvr2_v4l2_release(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300770{
771 struct pvr2_v4l2_fh *fhp = file->private_data;
772 struct pvr2_v4l2 *vp = fhp->vhead;
773 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
774
775 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
776
777 if (fhp->rhp) {
778 struct pvr2_stream *sp;
779 struct pvr2_hdw *hdw;
780 hdw = fhp->channel.mc_head->hdw;
781 pvr2_hdw_set_streaming(hdw,0);
782 sp = pvr2_ioread_get_stream(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300783 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300784 pvr2_ioread_destroy(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300785 fhp->rhp = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300786 }
787 v4l2_prio_close(&vp->prio, &fhp->prio);
788 file->private_data = NULL;
789
790 pvr2_context_enter(mp); do {
791 if (fhp->vnext) {
792 fhp->vnext->vprev = fhp->vprev;
793 } else {
794 vp->vlast = fhp->vprev;
795 }
796 if (fhp->vprev) {
797 fhp->vprev->vnext = fhp->vnext;
798 } else {
799 vp->vfirst = fhp->vnext;
800 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300801 fhp->vnext = NULL;
802 fhp->vprev = NULL;
803 fhp->vhead = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300804 pvr2_channel_done(&fhp->channel);
805 pvr2_trace(PVR2_TRACE_STRUCT,
806 "Destroying pvr_v4l2_fh id=%p",fhp);
807 kfree(fhp);
808 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
809 pvr2_v4l2_destroy_no_lock(vp);
810 }
811 } while (0); pvr2_context_exit(mp);
812 return 0;
813}
814
815
Adrian Bunk07e337e2006-06-30 11:30:20 -0300816static int pvr2_v4l2_open(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300817{
Mike Isely75910052006-09-23 22:30:50 -0300818 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
Mike Iselyd8554972006-06-26 20:58:46 -0300819 struct pvr2_v4l2_fh *fhp;
820 struct pvr2_v4l2 *vp;
821 struct pvr2_hdw *hdw;
822
Mike Isely75910052006-09-23 22:30:50 -0300823 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
Mike Iselyd8554972006-06-26 20:58:46 -0300824
825 vp = dip->v4lp;
826 hdw = vp->channel.hdw;
827
828 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
829
830 if (!pvr2_hdw_dev_ok(hdw)) {
831 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
832 "pvr2_v4l2_open: hardware not ready");
833 return -EIO;
834 }
835
836 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
837 if (!fhp) {
838 return -ENOMEM;
839 }
840 memset(fhp,0,sizeof(*fhp));
841
842 init_waitqueue_head(&fhp->wait_data);
843 fhp->dev_info = dip;
844
845 pvr2_context_enter(vp->channel.mc_head); do {
846 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
847 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300848 fhp->vnext = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300849 fhp->vprev = vp->vlast;
850 if (vp->vlast) {
851 vp->vlast->vnext = fhp;
852 } else {
853 vp->vfirst = fhp;
854 }
855 vp->vlast = fhp;
856 fhp->vhead = vp;
857 } while (0); pvr2_context_exit(vp->channel.mc_head);
858
859 fhp->file = file;
860 file->private_data = fhp;
861 v4l2_prio_open(&vp->prio,&fhp->prio);
862
863 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
864
865 return 0;
866}
867
868
869static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
870{
871 wake_up(&fhp->wait_data);
872}
873
874static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
875{
876 int ret;
877 struct pvr2_stream *sp;
878 struct pvr2_hdw *hdw;
879 if (fh->rhp) return 0;
880
881 /* First read() attempt. Try to claim the stream and start
882 it... */
883 if ((ret = pvr2_channel_claim_stream(&fh->channel,
884 fh->dev_info->stream)) != 0) {
885 /* Someone else must already have it */
886 return ret;
887 }
888
889 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
890 if (!fh->rhp) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300891 pvr2_channel_claim_stream(&fh->channel,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300892 return -ENOMEM;
893 }
894
895 hdw = fh->channel.mc_head->hdw;
896 sp = fh->dev_info->stream->stream;
897 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
898 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
899 pvr2_hdw_set_streaming(hdw,!0);
900 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
901
902 return ret;
903}
904
905
906static ssize_t pvr2_v4l2_read(struct file *file,
907 char __user *buff, size_t count, loff_t *ppos)
908{
909 struct pvr2_v4l2_fh *fh = file->private_data;
910 int ret;
911
912 if (fh->fw_mode_flag) {
913 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
914 char *tbuf;
915 int c1,c2;
916 int tcnt = 0;
917 unsigned int offs = *ppos;
918
919 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
920 if (!tbuf) return -ENOMEM;
921
922 while (count) {
923 c1 = count;
924 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
925 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
926 if (c2 < 0) {
927 tcnt = c2;
928 break;
929 }
930 if (!c2) break;
931 if (copy_to_user(buff,tbuf,c2)) {
932 tcnt = -EFAULT;
933 break;
934 }
935 offs += c2;
936 tcnt += c2;
937 buff += c2;
938 count -= c2;
939 *ppos += c2;
940 }
941 kfree(tbuf);
942 return tcnt;
943 }
944
945 if (!fh->rhp) {
946 ret = pvr2_v4l2_iosetup(fh);
947 if (ret) {
948 return ret;
949 }
950 }
951
952 for (;;) {
953 ret = pvr2_ioread_read(fh->rhp,buff,count);
954 if (ret >= 0) break;
955 if (ret != -EAGAIN) break;
956 if (file->f_flags & O_NONBLOCK) break;
957 /* Doing blocking I/O. Wait here. */
958 ret = wait_event_interruptible(
959 fh->wait_data,
960 pvr2_ioread_avail(fh->rhp) >= 0);
961 if (ret < 0) break;
962 }
963
964 return ret;
965}
966
967
968static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
969{
970 unsigned int mask = 0;
971 struct pvr2_v4l2_fh *fh = file->private_data;
972 int ret;
973
974 if (fh->fw_mode_flag) {
975 mask |= POLLIN | POLLRDNORM;
976 return mask;
977 }
978
979 if (!fh->rhp) {
980 ret = pvr2_v4l2_iosetup(fh);
981 if (ret) return POLLERR;
982 }
983
984 poll_wait(file,&fh->wait_data,wait);
985
986 if (pvr2_ioread_avail(fh->rhp) >= 0) {
987 mask |= POLLIN | POLLRDNORM;
988 }
989
990 return mask;
991}
992
993
Arjan van de Venfa027c22007-02-12 00:55:33 -0800994static const struct file_operations vdev_fops = {
Mike Iselyd8554972006-06-26 20:58:46 -0300995 .owner = THIS_MODULE,
996 .open = pvr2_v4l2_open,
997 .release = pvr2_v4l2_release,
998 .read = pvr2_v4l2_read,
999 .ioctl = pvr2_v4l2_ioctl,
1000 .llseek = no_llseek,
1001 .poll = pvr2_v4l2_poll,
1002};
1003
1004
1005#define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1006
1007static struct video_device vdev_template = {
1008 .owner = THIS_MODULE,
1009 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1010 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1011 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1012 | V4L2_CAP_READWRITE),
1013 .hardware = VID_HARDWARE_PVRUSB2,
1014 .fops = &vdev_fops,
1015};
1016
1017
1018static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1019 struct pvr2_v4l2 *vp,
1020 enum pvr2_config cfg)
1021{
1022 int mindevnum;
1023 int unit_number;
1024 int v4l_type;
1025 dip->v4lp = vp;
1026 dip->config = cfg;
1027
1028
1029 switch (cfg) {
1030 case pvr2_config_mpeg:
1031 v4l_type = VFL_TYPE_GRABBER;
1032 dip->stream = &vp->channel.mc_head->video_stream;
1033 break;
1034 case pvr2_config_vbi:
1035 v4l_type = VFL_TYPE_VBI;
1036 break;
1037 case pvr2_config_radio:
1038 v4l_type = VFL_TYPE_RADIO;
1039 break;
1040 default:
1041 /* Bail out (this should be impossible) */
1042 err("Failed to set up pvrusb2 v4l dev"
1043 " due to unrecognized config");
1044 return;
1045 }
1046
1047 if (!dip->stream) {
1048 err("Failed to set up pvrusb2 v4l dev"
1049 " due to missing stream instance");
1050 return;
1051 }
1052
Mike Isely75910052006-09-23 22:30:50 -03001053 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1054 dip->devbase.release = pvr2_video_device_release;
Mike Iselyd8554972006-06-26 20:58:46 -03001055
1056 mindevnum = -1;
1057 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1058 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1059 mindevnum = video_nr[unit_number];
1060 }
Mike Isely75910052006-09-23 22:30:50 -03001061 if ((video_register_device(&dip->devbase, v4l_type, mindevnum) < 0) &&
1062 (video_register_device(&dip->devbase, v4l_type, -1) < 0)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001063 err("Failed to register pvrusb2 v4l video device");
1064 } else {
Mike Isely47ed3bc2006-06-29 09:26:43 -03001065 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
Mike Isely75910052006-09-23 22:30:50 -03001066 dip->devbase.minor,pvr2_config_get_name(dip->config));
Mike Iselyd8554972006-06-26 20:58:46 -03001067 }
1068
Mike Iselyd8554972006-06-26 20:58:46 -03001069 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03001070 cfg-1,dip->devbase.minor);
Mike Iselyd8554972006-06-26 20:58:46 -03001071}
1072
1073
1074struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1075{
1076 struct pvr2_v4l2 *vp;
1077
1078 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1079 if (!vp) return vp;
1080 memset(vp,0,sizeof(*vp));
Mike Isely75910052006-09-23 22:30:50 -03001081 vp->vdev = kmalloc(sizeof(*vp->vdev),GFP_KERNEL);
1082 if (!vp->vdev) {
1083 kfree(vp);
Randy Dunlapc2625bf2006-10-29 11:12:27 -03001084 return NULL;
Mike Isely75910052006-09-23 22:30:50 -03001085 }
1086 memset(vp->vdev,0,sizeof(*vp->vdev));
Mike Iselyd8554972006-06-26 20:58:46 -03001087 pvr2_channel_init(&vp->channel,mnp);
1088 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1089
1090 vp->channel.check_func = pvr2_v4l2_internal_check;
1091
1092 /* register streams */
Mike Isely75910052006-09-23 22:30:50 -03001093 pvr2_v4l2_dev_init(vp->vdev,vp,pvr2_config_mpeg);
Mike Iselyd8554972006-06-26 20:58:46 -03001094
1095 return vp;
1096}
1097
1098/*
1099 Stuff for Emacs to see, in order to encourage consistent editing style:
1100 *** Local Variables: ***
1101 *** mode: c ***
1102 *** fill-column: 75 ***
1103 *** tab-width: 8 ***
1104 *** c-basic-offset: 8 ***
1105 *** End: ***
1106 */