aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/saa7164
diff options
context:
space:
mode:
authorSteven Toth <stoth@kernellabs.com>2010-07-31 15:34:19 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2010-10-21 05:55:05 -0400
commit11bd27b2e7ab8125b0f8d763b8b61d07a5a38acf (patch)
treeeb7d7157931efe031e6b8869d5285b2660e7d37e /drivers/media/video/saa7164
parent96d8420d17809f0b090da3dddb26a3625b1b6e7a (diff)
[media] saa7164: Add missing saa7164-vbi.c file
Signed-off-by: Steven Toth <stoth@kernellabs.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/video/saa7164')
-rw-r--r--drivers/media/video/saa7164/saa7164-vbi.c1411
1 files changed, 1411 insertions, 0 deletions
diff --git a/drivers/media/video/saa7164/saa7164-vbi.c b/drivers/media/video/saa7164/saa7164-vbi.c
new file mode 100644
index 00000000000..36ef93fb040
--- /dev/null
+++ b/drivers/media/video/saa7164/saa7164-vbi.c
@@ -0,0 +1,1411 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "saa7164.h"
23
24static struct saa7164_tvnorm saa7164_tvnorms[] = {
25 {
26 .name = "NTSC-M",
27 .id = V4L2_STD_NTSC_M,
28 }, {
29 .name = "NTSC-JP",
30 .id = V4L2_STD_NTSC_M_JP,
31 }
32};
33
34static const u32 saa7164_v4l2_ctrls[] = {
35 0
36};
37
38/* Take the encoder configuration from the port struct and
39 * flush it to the hardware.
40 */
41static void saa7164_vbi_configure(struct saa7164_port *port)
42{
43 struct saa7164_dev *dev = port->dev;
44 dprintk(DBGLVL_VBI, "%s()\n", __func__);
45
46 port->vbi_params.width = port->width;
47 port->vbi_params.height = port->height;
48 port->vbi_params.is_50hz =
49 (port->encodernorm.id & V4L2_STD_625_50) != 0;
50
51 /* Set up the DIF (enable it) for analog mode by default */
52 saa7164_api_initialize_dif(port);
53
54// /* Configure the correct video standard */
55// saa7164_api_configure_dif(port, port->encodernorm.id);
56
57// /* Ensure the audio decoder is correct configured */
58// saa7164_api_set_audio_std(port);
59 dprintk(DBGLVL_VBI, "%s() ends\n", __func__);
60}
61
62static int saa7164_vbi_buffers_dealloc(struct saa7164_port *port)
63{
64 struct list_head *c, *n, *p, *q, *l, *v;
65 struct saa7164_dev *dev = port->dev;
66 struct saa7164_buffer *buf;
67 struct saa7164_user_buffer *ubuf;
68
69 /* Remove any allocated buffers */
70 mutex_lock(&port->dmaqueue_lock);
71
72 dprintk(DBGLVL_VBI, "%s(port=%d) dmaqueue\n", __func__, port->nr);
73 list_for_each_safe(c, n, &port->dmaqueue.list) {
74 buf = list_entry(c, struct saa7164_buffer, list);
75 list_del(c);
76 saa7164_buffer_dealloc(buf);
77 }
78
79 dprintk(DBGLVL_VBI, "%s(port=%d) used\n", __func__, port->nr);
80 list_for_each_safe(p, q, &port->list_buf_used.list) {
81 ubuf = list_entry(p, struct saa7164_user_buffer, list);
82 list_del(p);
83 saa7164_buffer_dealloc_user(ubuf);
84 }
85
86 dprintk(DBGLVL_VBI, "%s(port=%d) free\n", __func__, port->nr);
87 list_for_each_safe(l, v, &port->list_buf_free.list) {
88 ubuf = list_entry(l, struct saa7164_user_buffer, list);
89 list_del(l);
90 saa7164_buffer_dealloc_user(ubuf);
91 }
92
93 mutex_unlock(&port->dmaqueue_lock);
94 dprintk(DBGLVL_VBI, "%s(port=%d) done\n", __func__, port->nr);
95
96 return 0;
97}
98
99/* Dynamic buffer switch at vbi start time */
100static int saa7164_vbi_buffers_alloc(struct saa7164_port *port)
101{
102 struct saa7164_dev *dev = port->dev;
103 struct saa7164_buffer *buf;
104 struct saa7164_user_buffer *ubuf;
105 tmHWStreamParameters_t *params = &port->hw_streamingparams;
106 int result = -ENODEV, i;
107 int len = 0;
108
109 dprintk(DBGLVL_VBI, "%s()\n", __func__);
110
111 /* TODO: NTSC SPECIFIC */
112 /* Init and establish defaults */
113 params->samplesperline = 1440;
114 params->numberoflines = 12;
115 params->numberoflines = 18;
116 params->pitch = 1600;
117 params->pitch = 1440;
118 params->numpagetables = 2 +
119 ((params->numberoflines * params->pitch) / PAGE_SIZE);
120 params->bitspersample = 8;
121 params->linethreshold = 0;
122 params->pagetablelistvirt = 0;
123 params->pagetablelistphys = 0;
124 params->numpagetableentries = port->hwcfg.buffercount;
125
126 /* Allocate the PCI resources, buffers (hard) */
127 for (i = 0; i < port->hwcfg.buffercount; i++) {
128 buf = saa7164_buffer_alloc(port,
129 params->numberoflines *
130 params->pitch);
131
132 if (!buf) {
133 printk(KERN_ERR "%s() failed "
134 "(errno = %d), unable to allocate buffer\n",
135 __func__, result);
136 result = -ENOMEM;
137 goto failed;
138 } else {
139
140 mutex_lock(&port->dmaqueue_lock);
141 list_add_tail(&buf->list, &port->dmaqueue.list);
142 mutex_unlock(&port->dmaqueue_lock);
143
144 }
145 }
146
147 /* Allocate some kenrel kernel buffers for copying
148 * to userpsace.
149 */
150 len = params->numberoflines * params->pitch;
151
152 if (vbi_buffers < 16)
153 vbi_buffers = 16;
154 if (vbi_buffers > 512)
155 vbi_buffers = 512;
156
157 for (i = 0; i < vbi_buffers; i++) {
158
159 ubuf = saa7164_buffer_alloc_user(dev, len);
160 if (ubuf) {
161 mutex_lock(&port->dmaqueue_lock);
162 list_add_tail(&ubuf->list, &port->list_buf_free.list);
163 mutex_unlock(&port->dmaqueue_lock);
164 }
165
166 }
167
168 result = 0;
169
170failed:
171 return result;
172}
173
174
175static int saa7164_vbi_initialize(struct saa7164_port *port)
176{
177 saa7164_vbi_configure(port);
178 return 0;
179}
180
181/* -- V4L2 --------------------------------------------------------- */
182static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
183{
184 struct saa7164_vbi_fh *fh = file->private_data;
185 struct saa7164_port *port = fh->port;
186 struct saa7164_dev *dev = port->dev;
187 unsigned int i;
188
189 dprintk(DBGLVL_VBI, "%s(id=0x%x)\n", __func__, (u32)*id);
190
191 for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
192 if (*id & saa7164_tvnorms[i].id)
193 break;
194 }
195 if (i == ARRAY_SIZE(saa7164_tvnorms))
196 return -EINVAL;
197
198 port->encodernorm = saa7164_tvnorms[i];
199
200 /* Update the audio decoder while is not running in
201 * auto detect mode.
202 */
203 saa7164_api_set_audio_std(port);
204
205 dprintk(DBGLVL_VBI, "%s(id=0x%x) OK\n", __func__, (u32)*id);
206
207 return 0;
208}
209
210static int vidioc_enum_input(struct file *file, void *priv,
211 struct v4l2_input *i)
212{
213 int n;
214
215 char *inputs[] = { "tuner", "composite", "svideo", "aux",
216 "composite", "svideo", "aux" };
217
218 if (i->index >= 7)
219 return -EINVAL;
220
221 strcpy(i->name, inputs[ i->index ]);
222
223 if (i->index == 0)
224 i->type = V4L2_INPUT_TYPE_TUNER;
225 else
226 i->type = V4L2_INPUT_TYPE_CAMERA;
227
228 for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
229 i->std |= saa7164_tvnorms[n].id;
230
231 return 0;
232}
233
234static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
235{
236 struct saa7164_vbi_fh *fh = file->private_data;
237 struct saa7164_port *port = fh->port;
238 struct saa7164_dev *dev = port->dev;
239
240 if (saa7164_api_get_videomux(port) != SAA_OK)
241 return -EIO;
242
243 *i = (port->mux_input - 1);
244
245 dprintk(DBGLVL_VBI, "%s() input=%d\n", __func__, *i);
246
247 return 0;
248}
249
250static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
251{
252 struct saa7164_vbi_fh *fh = file->private_data;
253 struct saa7164_port *port = fh->port;
254 struct saa7164_dev *dev = port->dev;
255
256 dprintk(DBGLVL_VBI, "%s() input=%d\n", __func__, i);
257
258 if (i >= 7)
259 return -EINVAL;
260
261 port->mux_input = i + 1;
262
263 if (saa7164_api_set_videomux(port) != SAA_OK)
264 return -EIO;
265
266 return 0;
267}
268
269static int vidioc_g_tuner(struct file *file, void *priv,
270 struct v4l2_tuner *t)
271{
272 struct saa7164_vbi_fh *fh = file->private_data;
273 struct saa7164_port *port = fh->port;
274 struct saa7164_dev *dev = port->dev;
275
276 if (0 != t->index)
277 return -EINVAL;
278
279 strcpy(t->name, "tuner");
280 t->type = V4L2_TUNER_ANALOG_TV;
281 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
282
283 dprintk(DBGLVL_VBI, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
284
285 return 0;
286}
287
288static int vidioc_s_tuner(struct file *file, void *priv,
289 struct v4l2_tuner *t)
290{
291
292 /* Update the A/V core */
293
294 return 0;
295}
296
297static int vidioc_g_frequency(struct file *file, void *priv,
298 struct v4l2_frequency *f)
299{
300 struct saa7164_vbi_fh *fh = file->private_data;
301 struct saa7164_port *port = fh->port;
302
303 f->type = V4L2_TUNER_ANALOG_TV;
304 f->frequency = port->freq;
305
306 return 0;
307}
308
309static int vidioc_s_frequency(struct file *file, void *priv,
310 struct v4l2_frequency *f)
311{
312 struct saa7164_vbi_fh *fh = file->private_data;
313 struct saa7164_port *port = fh->port;
314 struct saa7164_dev *dev = port->dev;
315 struct saa7164_port *tsport;
316 struct dvb_frontend *fe;
317
318 /* TODO: Pull this for the std */
319 struct analog_parameters params = {
320 .mode = V4L2_TUNER_ANALOG_TV,
321 .audmode = V4L2_TUNER_MODE_STEREO,
322 .std = port->encodernorm.id,
323 .frequency = f->frequency
324 };
325
326 /* Stop the encoder */
327 dprintk(DBGLVL_VBI, "%s() frequency=%d tuner=%d\n", __func__,
328 f->frequency, f->tuner);
329
330 if (f->tuner != 0)
331 return -EINVAL;
332
333 if (f->type != V4L2_TUNER_ANALOG_TV)
334 return -EINVAL;
335
336 port->freq = f->frequency;
337
338 /* Update the hardware */
339 if (port->nr == SAA7164_PORT_VBI1)
340 tsport = &dev->ports[ SAA7164_PORT_TS1 ];
341 else
342 if (port->nr == SAA7164_PORT_VBI2)
343 tsport = &dev->ports[ SAA7164_PORT_TS2 ];
344 else
345 BUG();
346
347 fe = tsport->dvb.frontend;
348
349 if (fe && fe->ops.tuner_ops.set_analog_params)
350 fe->ops.tuner_ops.set_analog_params(fe, &params);
351 else
352 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
353
354 saa7164_vbi_initialize(port);
355
356 return 0;
357}
358
359static int vidioc_g_ctrl(struct file *file, void *priv,
360 struct v4l2_control *ctl)
361{
362 struct saa7164_vbi_fh *fh = file->private_data;
363 struct saa7164_port *port = fh->port;
364 struct saa7164_dev *dev = port->dev;
365
366 dprintk(DBGLVL_VBI, "%s(id=%d, value=%d)\n", __func__,
367 ctl->id, ctl->value);
368
369 switch (ctl->id) {
370 case V4L2_CID_BRIGHTNESS:
371 ctl->value = port->ctl_brightness;
372 break;
373 case V4L2_CID_CONTRAST:
374 ctl->value = port->ctl_contrast;
375 break;
376 case V4L2_CID_SATURATION:
377 ctl->value = port->ctl_saturation;
378 break;
379 case V4L2_CID_HUE:
380 ctl->value = port->ctl_hue;
381 break;
382 case V4L2_CID_SHARPNESS:
383 ctl->value = port->ctl_sharpness;
384 break;
385 case V4L2_CID_AUDIO_VOLUME:
386 ctl->value = port->ctl_volume;
387 break;
388 default:
389 return -EINVAL;
390 }
391
392 return 0;
393}
394
395static int vidioc_s_ctrl(struct file *file, void *priv,
396 struct v4l2_control *ctl)
397{
398 struct saa7164_vbi_fh *fh = file->private_data;
399 struct saa7164_port *port = fh->port;
400 struct saa7164_dev *dev = port->dev;
401 int ret = 0;
402
403 dprintk(DBGLVL_VBI, "%s(id=%d, value=%d)\n", __func__,
404 ctl->id, ctl->value);
405
406 switch (ctl->id) {
407 case V4L2_CID_BRIGHTNESS:
408 if ((ctl->value >= 0) && (ctl->value <= 255)) {
409 port->ctl_brightness = ctl->value;
410 saa7164_api_set_usercontrol(port,
411 PU_BRIGHTNESS_CONTROL);
412 } else
413 ret = -EINVAL;
414 break;
415 case V4L2_CID_CONTRAST:
416 if ((ctl->value >= 0) && (ctl->value <= 255)) {
417 port->ctl_contrast = ctl->value;
418 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
419 } else
420 ret = -EINVAL;
421 break;
422 case V4L2_CID_SATURATION:
423 if ((ctl->value >= 0) && (ctl->value <= 255)) {
424 port->ctl_saturation = ctl->value;
425 saa7164_api_set_usercontrol(port,
426 PU_SATURATION_CONTROL);
427 } else
428 ret = -EINVAL;
429 break;
430 case V4L2_CID_HUE:
431 if ((ctl->value >= 0) && (ctl->value <= 255)) {
432 port->ctl_hue = ctl->value;
433 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
434 } else
435 ret = -EINVAL;
436 break;
437 case V4L2_CID_SHARPNESS:
438 if ((ctl->value >= 0) && (ctl->value <= 255)) {
439 port->ctl_sharpness = ctl->value;
440 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
441 } else
442 ret = -EINVAL;
443 break;
444 case V4L2_CID_AUDIO_VOLUME:
445 if ((ctl->value >= -83) && (ctl->value <= 24)) {
446 port->ctl_volume = ctl->value;
447 saa7164_api_set_audio_volume(port, port->ctl_volume);
448 } else
449 ret = -EINVAL;
450 break;
451 default:
452 ret = -EINVAL;
453 }
454
455 return ret;
456}
457
458static int saa7164_get_ctrl(struct saa7164_port *port,
459 struct v4l2_ext_control *ctrl)
460{
461 struct saa7164_vbi_params *params = &port->vbi_params;
462
463 switch (ctrl->id) {
464 case V4L2_CID_MPEG_STREAM_TYPE:
465 ctrl->value = params->stream_type;
466 break;
467 case V4L2_CID_MPEG_AUDIO_MUTE:
468 ctrl->value = params->ctl_mute;
469 break;
470 case V4L2_CID_MPEG_VIDEO_ASPECT:
471 ctrl->value = params->ctl_aspect;
472 break;
473 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
474 ctrl->value = params->refdist;
475 break;
476 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
477 ctrl->value = params->gop_size;
478 break;
479 default:
480 return -EINVAL;
481 }
482 return 0;
483}
484
485static int vidioc_g_ext_ctrls(struct file *file, void *priv,
486 struct v4l2_ext_controls *ctrls)
487{
488 struct saa7164_vbi_fh *fh = file->private_data;
489 struct saa7164_port *port = fh->port;
490 int i, err = 0;
491
492 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
493 for (i = 0; i < ctrls->count; i++) {
494 struct v4l2_ext_control *ctrl = ctrls->controls + i;
495
496 err = saa7164_get_ctrl(port, ctrl);
497 if (err) {
498 ctrls->error_idx = i;
499 break;
500 }
501 }
502 return err;
503
504 }
505
506 return -EINVAL;
507}
508
509static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
510{
511 int ret = -EINVAL;
512
513 switch (ctrl->id) {
514 case V4L2_CID_MPEG_STREAM_TYPE:
515 if ((ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) ||
516 (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS))
517 ret = 0;
518 break;
519 case V4L2_CID_MPEG_AUDIO_MUTE:
520 if ((ctrl->value >= 0) &&
521 (ctrl->value <= 1))
522 ret = 0;
523 break;
524 case V4L2_CID_MPEG_VIDEO_ASPECT:
525 if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
526 (ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
527 ret = 0;
528 break;
529 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
530 if ((ctrl->value >= 0) &&
531 (ctrl->value <= 255))
532 ret = 0;
533 break;
534 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
535 if ((ctrl->value >= 1) &&
536 (ctrl->value <= 3))
537 ret = 0;
538 break;
539 default:
540 ret = -EINVAL;
541 }
542
543 return ret;
544}
545
546static int vidioc_try_ext_ctrls(struct file *file, void *priv,
547 struct v4l2_ext_controls *ctrls)
548{
549 int i, err = 0;
550
551 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
552 for (i = 0; i < ctrls->count; i++) {
553 struct v4l2_ext_control *ctrl = ctrls->controls + i;
554
555 err = saa7164_try_ctrl(ctrl, 0);
556 if (err) {
557 ctrls->error_idx = i;
558 break;
559 }
560 }
561 return err;
562 }
563
564 return -EINVAL;
565}
566
567static int saa7164_set_ctrl(struct saa7164_port *port,
568 struct v4l2_ext_control *ctrl)
569{
570 struct saa7164_vbi_params *params = &port->vbi_params;
571 int ret = 0;
572
573 switch (ctrl->id) {
574 case V4L2_CID_MPEG_STREAM_TYPE:
575 params->stream_type = ctrl->value;
576 break;
577 case V4L2_CID_MPEG_AUDIO_MUTE:
578 params->ctl_mute = ctrl->value;
579 ret = saa7164_api_audio_mute(port, params->ctl_mute);
580 if (ret != SAA_OK) {
581 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
582 ret);
583 ret = -EIO;
584 }
585 break;
586 case V4L2_CID_MPEG_VIDEO_ASPECT:
587 params->ctl_aspect = ctrl->value;
588 ret = saa7164_api_set_aspect_ratio(port);
589 if (ret != SAA_OK) {
590 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
591 ret);
592 ret = -EIO;
593 }
594 break;
595 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
596 params->refdist = ctrl->value;
597 break;
598 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
599 params->gop_size = ctrl->value;
600 break;
601 default:
602 return -EINVAL;
603 }
604
605 /* TODO: Update the hardware */
606
607 return ret;
608}
609
610static int vidioc_s_ext_ctrls(struct file *file, void *priv,
611 struct v4l2_ext_controls *ctrls)
612{
613 struct saa7164_vbi_fh *fh = file->private_data;
614 struct saa7164_port *port = fh->port;
615 int i, err = 0;
616
617 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
618 for (i = 0; i < ctrls->count; i++) {
619 struct v4l2_ext_control *ctrl = ctrls->controls + i;
620
621 err = saa7164_try_ctrl(ctrl, 0);
622 if (err) {
623 ctrls->error_idx = i;
624 break;
625 }
626 err = saa7164_set_ctrl(port, ctrl);
627 if (err) {
628 ctrls->error_idx = i;
629 break;
630 }
631 }
632 return err;
633
634 }
635
636 return -EINVAL;
637}
638
639static int vidioc_querycap(struct file *file, void *priv,
640 struct v4l2_capability *cap)
641{
642 struct saa7164_vbi_fh *fh = file->private_data;
643 struct saa7164_port *port = fh->port;
644 struct saa7164_dev *dev = port->dev;
645
646 strcpy(cap->driver, dev->name);
647 strlcpy(cap->card, saa7164_boards[dev->board].name,
648 sizeof(cap->card));
649 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
650
651 cap->capabilities =
652 V4L2_CAP_VBI_CAPTURE |
653 V4L2_CAP_READWRITE |
654 V4L2_CAP_STREAMING |
655 0;
656
657 cap->capabilities |= V4L2_CAP_TUNER;
658 cap->version = 0;
659
660 return 0;
661}
662
663static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
664 struct v4l2_fmtdesc *f)
665{
666 if (f->index != 0)
667 return -EINVAL;
668
669 strlcpy(f->description, "VBI", sizeof(f->description));
670 f->pixelformat = V4L2_PIX_FMT_MPEG;
671
672 return 0;
673}
674
675static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
676 struct v4l2_format *f)
677{
678 struct saa7164_vbi_fh *fh = file->private_data;
679 struct saa7164_port *port = fh->port;
680 struct saa7164_dev *dev = port->dev;
681
682 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
683 f->fmt.pix.bytesperline = 0;
684 f->fmt.pix.sizeimage =
685 port->ts_packet_size * port->ts_packet_count;
686 f->fmt.pix.colorspace = 0;
687 f->fmt.pix.width = port->width;
688 f->fmt.pix.height = port->height;
689
690 dprintk(DBGLVL_VBI, "VIDIOC_G_FMT: w: %d, h: %d\n",
691 port->width, port->height);
692
693 return 0;
694}
695
696static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
697 struct v4l2_format *f)
698{
699 struct saa7164_vbi_fh *fh = file->private_data;
700 struct saa7164_port *port = fh->port;
701 struct saa7164_dev *dev = port->dev;
702
703 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
704 f->fmt.pix.bytesperline = 0;
705 f->fmt.pix.sizeimage =
706 port->ts_packet_size * port->ts_packet_count;
707 f->fmt.pix.colorspace = 0;
708 dprintk(DBGLVL_VBI, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
709 port->width, port->height);
710 return 0;
711}
712
713static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
714 struct v4l2_format *f)
715{
716 struct saa7164_vbi_fh *fh = file->private_data;
717 struct saa7164_port *port = fh->port;
718 struct saa7164_dev *dev = port->dev;
719
720 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
721 f->fmt.pix.bytesperline = 0;
722 f->fmt.pix.sizeimage =
723 port->ts_packet_size * port->ts_packet_count;
724 f->fmt.pix.colorspace = 0;
725
726 dprintk(DBGLVL_VBI, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
727 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
728
729 return 0;
730}
731
732static int vidioc_log_status(struct file *file, void *priv)
733{
734 return 0;
735}
736
737static int fill_queryctrl(struct saa7164_vbi_params *params,
738 struct v4l2_queryctrl *c)
739{
740 switch (c->id) {
741 case V4L2_CID_BRIGHTNESS:
742 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
743 case V4L2_CID_CONTRAST:
744 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
745 case V4L2_CID_SATURATION:
746 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
747 case V4L2_CID_HUE:
748 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
749 case V4L2_CID_SHARPNESS:
750 return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
751 case V4L2_CID_MPEG_AUDIO_MUTE:
752 return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
753 case V4L2_CID_AUDIO_VOLUME:
754 return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
755 case V4L2_CID_MPEG_STREAM_TYPE:
756 return v4l2_ctrl_query_fill(c,
757 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
758 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
759 1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
760 case V4L2_CID_MPEG_VIDEO_ASPECT:
761 return v4l2_ctrl_query_fill(c,
762 V4L2_MPEG_VIDEO_ASPECT_1x1,
763 V4L2_MPEG_VIDEO_ASPECT_221x100,
764 1, V4L2_MPEG_VIDEO_ASPECT_4x3);
765 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
766 return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
767 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
768 return v4l2_ctrl_query_fill(c,
769 1, 3, 1, 1);
770 default:
771 return -EINVAL;
772 }
773}
774
775static int vidioc_queryctrl(struct file *file, void *priv,
776 struct v4l2_queryctrl *c)
777{
778 struct saa7164_vbi_fh *fh = priv;
779 struct saa7164_port *port = fh->port;
780 int i, next;
781 u32 id = c->id;
782
783 memset(c, 0, sizeof(*c));
784
785 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
786 c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
787
788 for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
789 if (next) {
790 if (c->id < saa7164_v4l2_ctrls[i])
791 c->id = saa7164_v4l2_ctrls[i];
792 else
793 continue;
794 }
795
796 if (c->id == saa7164_v4l2_ctrls[i])
797 return fill_queryctrl(&port->vbi_params, c);
798
799 if (c->id < saa7164_v4l2_ctrls[i])
800 break;
801 }
802
803 return -EINVAL;
804}
805
806static int saa7164_vbi_stop_port(struct saa7164_port *port)
807{
808 struct saa7164_dev *dev = port->dev;
809 int ret;
810
811 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
812 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
813 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
814 __func__, ret);
815 ret = -EIO;
816 } else {
817 dprintk(DBGLVL_VBI, "%s() Stopped\n", __func__);
818 ret = 0;
819 }
820
821 return ret;
822}
823
824static int saa7164_vbi_acquire_port(struct saa7164_port *port)
825{
826 struct saa7164_dev *dev = port->dev;
827 int ret;
828
829 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
830 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
831 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
832 __func__, ret);
833 ret = -EIO;
834 } else {
835 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
836 ret = 0;
837 }
838
839 return ret;
840}
841
842static int saa7164_vbi_pause_port(struct saa7164_port *port)
843{
844 struct saa7164_dev *dev = port->dev;
845 int ret;
846
847 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
848 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
849 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
850 __func__, ret);
851 ret = -EIO;
852 } else {
853 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
854 ret = 0;
855 }
856
857 return ret;
858}
859
860/* Firmware is very windows centric, meaning you have to transition
861 * the part through AVStream / KS Windows stages, forwards or backwards.
862 * States are: stopped, acquired (h/w), paused, started.
863 * We have to leave here will all of the soft buffers on the free list,
864 * else the cfg_post() func won't have soft buffers to correctly configure.
865 */
866static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
867{
868 struct saa7164_dev *dev = port->dev;
869 struct saa7164_buffer *buf;
870 struct saa7164_user_buffer *ubuf;
871 struct list_head *c, *n;
872 int ret;
873
874 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
875
876 ret = saa7164_vbi_pause_port(port);
877 ret = saa7164_vbi_acquire_port(port);
878 ret = saa7164_vbi_stop_port(port);
879
880 dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
881 port->nr);
882
883 /* Reset the state of any allocated buffer resources */
884 mutex_lock(&port->dmaqueue_lock);
885
886 /* Reset the hard and soft buffer state */
887 list_for_each_safe(c, n, &port->dmaqueue.list) {
888 buf = list_entry(c, struct saa7164_buffer, list);
889 buf->flags = SAA7164_BUFFER_FREE;
890 buf->pos = 0;
891 }
892
893 list_for_each_safe(c, n, &port->list_buf_used.list) {
894 ubuf = list_entry(c, struct saa7164_user_buffer, list);
895 ubuf->pos = 0;
896 list_move_tail(&ubuf->list, &port->list_buf_free.list);
897 }
898
899 mutex_unlock(&port->dmaqueue_lock);
900
901 /* Free any allocated resources */
902 saa7164_vbi_buffers_dealloc(port);
903
904 dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
905
906 return ret;
907}
908
909static int saa7164_vbi_start_streaming(struct saa7164_port *port)
910{
911 struct saa7164_dev *dev = port->dev;
912 int result, ret = 0;
913
914 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
915
916 port->done_first_interrupt = 0;
917
918 /* allocate all of the PCIe DMA buffer resources on the fly,
919 * allowing switching between TS and PS payloads without
920 * requiring a complete driver reload.
921 */
922 saa7164_vbi_buffers_alloc(port);
923
924 /* Configure the encoder with any cache values */
925// saa7164_api_set_encoder(port);
926// saa7164_api_get_encoder(port);
927
928 /* Place the empty buffers on the hardware */
929 saa7164_buffer_cfg_port(port);
930
931 /* Negotiate format */
932 if (saa7164_api_set_vbi_format(port) != SAA_OK) {
933 printk(KERN_ERR "%s() No supported VBI format\n", __func__);
934 ret = -EIO;
935 goto out;
936 }
937
938 /* Acquire the hardware */
939 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
940 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
941 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
942 __func__, result);
943
944 ret = -EIO;
945 goto out;
946 } else
947 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
948
949 /* Pause the hardware */
950 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
951 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
952 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
953 __func__, result);
954
955 /* Stop the hardware, regardless */
956 result = saa7164_vbi_stop_port(port);
957 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
958 printk(KERN_ERR "%s() pause/forced stop transition "
959 "failed, res = 0x%x\n", __func__, result);
960 }
961
962 ret = -EIO;
963 goto out;
964 } else
965 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
966
967 /* Start the hardware */
968 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
969 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
970 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
971 __func__, result);
972
973 /* Stop the hardware, regardless */
974 result = saa7164_vbi_acquire_port(port);
975 result = saa7164_vbi_stop_port(port);
976 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
977 printk(KERN_ERR "%s() run/forced stop transition "
978 "failed, res = 0x%x\n", __func__, result);
979 }
980
981 ret = -EIO;
982 } else
983 dprintk(DBGLVL_VBI, "%s() Running\n", __func__);
984
985out:
986 return ret;
987}
988
989int saa7164_vbi_fmt(struct file *file, void *priv, struct v4l2_format *f)
990{
991 struct saa7164_vbi_fh *fh = priv;
992 struct saa7164_port *port = fh->port;
993
994 /* ntsc */
995 f->fmt.vbi.samples_per_line = 1600;
996 f->fmt.vbi.samples_per_line = 1440;
997 f->fmt.vbi.sampling_rate = 27000000;
998 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
999 f->fmt.vbi.offset = 0;
1000 f->fmt.vbi.flags = 0;
1001 f->fmt.vbi.start[0] = 10;
1002 f->fmt.vbi.count[0] = 18;
1003 f->fmt.vbi.start[1] = 263 + 10 + 1;
1004 f->fmt.vbi.count[1] = 18;
1005 return 0;
1006}
1007
1008static int fops_open(struct file *file)
1009{
1010 struct saa7164_dev *h, *dev = NULL;
1011 struct saa7164_port *port = NULL;
1012 struct saa7164_port *porte = NULL;
1013 struct saa7164_port *portf = NULL;
1014 struct saa7164_vbi_fh *fh;
1015 struct list_head *list;
1016 int minor = video_devdata(file)->minor;
1017
1018 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1019
1020 /* TODO: Really, the BKL? - remove this */
1021 lock_kernel();
1022 list_for_each(list, &saa7164_devlist) {
1023 h = list_entry(list, struct saa7164_dev, devlist);
1024
1025 porte = &h->ports[ SAA7164_PORT_VBI1 ];
1026 portf = &h->ports[ SAA7164_PORT_VBI2 ];
1027
1028 if (porte->v4l_device &&
1029 porte->v4l_device->minor == minor) {
1030 dev = h;
1031 port = porte;
1032 break;
1033 }
1034
1035 if (portf->v4l_device &&
1036 portf->v4l_device->minor == minor) {
1037 dev = h;
1038 port = portf;
1039 break;
1040 }
1041
1042 }
1043
1044 if (port == NULL) {
1045 unlock_kernel();
1046 return -ENODEV;
1047 }
1048
1049 /* allocate + initialize per filehandle data */
1050 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1051 if (NULL == fh) {
1052 unlock_kernel();
1053 return -ENOMEM;
1054 }
1055
1056 file->private_data = fh;
1057 fh->port = port;
1058
1059 unlock_kernel();
1060
1061 return 0;
1062}
1063
1064static int fops_release(struct file *file)
1065{
1066 struct saa7164_vbi_fh *fh = file->private_data;
1067 struct saa7164_port *port = fh->port;
1068 struct saa7164_dev *dev = port->dev;
1069
1070 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1071
1072 /* Shut device down on last close */
1073 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1074 if (atomic_dec_return(&port->v4l_reader_count) == 0) {
1075 /* stop vbi capture then cancel buffers */
1076 saa7164_vbi_stop_streaming(port);
1077 }
1078 }
1079
1080 file->private_data = NULL;
1081 kfree(fh);
1082
1083 return 0;
1084}
1085
1086struct saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
1087{
1088 struct saa7164_user_buffer *ubuf = 0;
1089 struct saa7164_dev *dev = port->dev;
1090 u32 crc;
1091
1092 mutex_lock(&port->dmaqueue_lock);
1093 if (!list_empty(&port->list_buf_used.list)) {
1094 ubuf = list_first_entry(&port->list_buf_used.list,
1095 struct saa7164_user_buffer, list);
1096
1097 if (crc_checking) {
1098 crc = crc32(0, ubuf->data, ubuf->actual_size);
1099 if (crc != ubuf->crc) {
1100 printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n", __func__,
1101 ubuf, ubuf->crc, crc);
1102 }
1103 }
1104
1105 }
1106 mutex_unlock(&port->dmaqueue_lock);
1107
1108 dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
1109
1110 return ubuf;
1111}
1112
1113static ssize_t fops_read(struct file *file, char __user *buffer,
1114 size_t count, loff_t *pos)
1115{
1116 struct saa7164_vbi_fh *fh = file->private_data;
1117 struct saa7164_port *port = fh->port;
1118 struct saa7164_user_buffer *ubuf = NULL;
1119 struct saa7164_dev *dev = port->dev;
1120 unsigned int ret = 0;
1121 int rem, cnt;
1122 u8 *p;
1123
1124 port->last_read_msecs_diff = port->last_read_msecs;
1125 port->last_read_msecs = jiffies_to_msecs(jiffies);
1126 port->last_read_msecs_diff = port->last_read_msecs -
1127 port->last_read_msecs_diff;
1128
1129 saa7164_histogram_update(&port->read_interval,
1130 port->last_read_msecs_diff);
1131
1132 if (*pos) {
1133 printk(KERN_ERR "%s() ESPIPE\n", __func__);
1134 return -ESPIPE;
1135 }
1136
1137 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1138 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1139
1140 if (saa7164_vbi_initialize(port) < 0) {
1141 printk(KERN_ERR "%s() EINVAL\n", __func__);
1142 return -EINVAL;
1143 }
1144
1145 saa7164_vbi_start_streaming(port);
1146 msleep(200);
1147 }
1148 }
1149
1150 /* blocking wait for buffer */
1151 if ((file->f_flags & O_NONBLOCK) == 0) {
1152 if (wait_event_interruptible(port->wait_read,
1153 saa7164_vbi_next_buf(port))) {
1154 printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
1155 return -ERESTARTSYS;
1156 }
1157 }
1158
1159 /* Pull the first buffer from the used list */
1160 ubuf = saa7164_vbi_next_buf(port);
1161
1162 while ((count > 0) && ubuf) {
1163
1164 /* set remaining bytes to copy */
1165 rem = ubuf->actual_size - ubuf->pos;
1166 cnt = rem > count ? count : rem;
1167
1168 p = ubuf->data + ubuf->pos;
1169
1170 dprintk(DBGLVL_VBI,
1171 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1172 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1173
1174 if (copy_to_user(buffer, p, cnt)) {
1175 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1176 if (!ret) {
1177 printk(KERN_ERR "%s() EFAULT\n", __func__);
1178 ret = -EFAULT;
1179 }
1180 goto err;
1181 }
1182
1183 ubuf->pos += cnt;
1184 count -= cnt;
1185 buffer += cnt;
1186 ret += cnt;
1187
1188 if (ubuf->pos > ubuf->actual_size) {
1189 printk(KERN_ERR "read() pos > actual, huh?\n");
1190 }
1191
1192 if (ubuf->pos == ubuf->actual_size) {
1193
1194 /* finished with current buffer, take next buffer */
1195
1196 /* Requeue the buffer on the free list */
1197 ubuf->pos = 0;
1198
1199 mutex_lock(&port->dmaqueue_lock);
1200 list_move_tail(&ubuf->list, &port->list_buf_free.list);
1201 mutex_unlock(&port->dmaqueue_lock);
1202
1203 /* Dequeue next */
1204 if ((file->f_flags & O_NONBLOCK) == 0) {
1205 if (wait_event_interruptible(port->wait_read,
1206 saa7164_vbi_next_buf(port))) {
1207 break;
1208 }
1209 }
1210 ubuf = saa7164_vbi_next_buf(port);
1211 }
1212 }
1213err:
1214 if (!ret && !ubuf) {
1215 printk(KERN_ERR "%s() EAGAIN\n", __func__);
1216 ret = -EAGAIN;
1217 }
1218
1219 return ret;
1220}
1221
1222static unsigned int fops_poll(struct file *file, poll_table *wait)
1223{
1224 struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
1225 struct saa7164_port *port = fh->port;
1226 struct saa7164_user_buffer *ubuf;
1227 unsigned int mask = 0;
1228
1229 port->last_poll_msecs_diff = port->last_poll_msecs;
1230 port->last_poll_msecs = jiffies_to_msecs(jiffies);
1231 port->last_poll_msecs_diff = port->last_poll_msecs -
1232 port->last_poll_msecs_diff;
1233
1234 saa7164_histogram_update(&port->poll_interval,
1235 port->last_poll_msecs_diff);
1236
1237 if (!video_is_registered(port->v4l_device)) {
1238 return -EIO;
1239 }
1240
1241 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1242 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1243 if (saa7164_vbi_initialize(port) < 0)
1244 return -EINVAL;
1245 saa7164_vbi_start_streaming(port);
1246 msleep(200);
1247 }
1248 }
1249
1250 /* blocking wait for buffer */
1251 if ((file->f_flags & O_NONBLOCK) == 0) {
1252 if (wait_event_interruptible(port->wait_read,
1253 saa7164_vbi_next_buf(port))) {
1254 return -ERESTARTSYS;
1255 }
1256 }
1257
1258 /* Pull the first buffer from the used list */
1259 ubuf = list_first_entry(&port->list_buf_used.list,
1260 struct saa7164_user_buffer, list);
1261
1262 if (ubuf)
1263 mask |= POLLIN | POLLRDNORM;
1264
1265 return mask;
1266}
1267static const struct v4l2_file_operations vbi_fops = {
1268 .owner = THIS_MODULE,
1269 .open = fops_open,
1270 .release = fops_release,
1271 .read = fops_read,
1272 .poll = fops_poll,
1273 .unlocked_ioctl = video_ioctl2,
1274};
1275
1276static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1277 .vidioc_s_std = vidioc_s_std,
1278 .vidioc_enum_input = vidioc_enum_input,
1279 .vidioc_g_input = vidioc_g_input,
1280 .vidioc_s_input = vidioc_s_input,
1281 .vidioc_g_tuner = vidioc_g_tuner,
1282 .vidioc_s_tuner = vidioc_s_tuner,
1283 .vidioc_g_frequency = vidioc_g_frequency,
1284 .vidioc_s_frequency = vidioc_s_frequency,
1285 .vidioc_s_ctrl = vidioc_s_ctrl,
1286 .vidioc_g_ctrl = vidioc_g_ctrl,
1287 .vidioc_querycap = vidioc_querycap,
1288 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1289 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1290 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1291 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1292 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1293 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1294 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1295 .vidioc_log_status = vidioc_log_status,
1296 .vidioc_queryctrl = vidioc_queryctrl,
1297// .vidioc_g_chip_ident = saa7164_g_chip_ident,
1298#ifdef CONFIG_VIDEO_ADV_DEBUG
1299// .vidioc_g_register = saa7164_g_register,
1300// .vidioc_s_register = saa7164_s_register,
1301#endif
1302 .vidioc_g_fmt_vbi_cap = saa7164_vbi_fmt,
1303 .vidioc_try_fmt_vbi_cap = saa7164_vbi_fmt,
1304 .vidioc_s_fmt_vbi_cap = saa7164_vbi_fmt,
1305};
1306
1307static struct video_device saa7164_vbi_template = {
1308 .name = "saa7164",
1309 .fops = &vbi_fops,
1310 .ioctl_ops = &vbi_ioctl_ops,
1311 .minor = -1,
1312 .tvnorms = SAA7164_NORMS,
1313 .current_norm = V4L2_STD_NTSC_M,
1314};
1315
1316static struct video_device *saa7164_vbi_alloc(
1317 struct saa7164_port *port,
1318 struct pci_dev *pci,
1319 struct video_device *template,
1320 char *type)
1321{
1322 struct video_device *vfd;
1323 struct saa7164_dev *dev = port->dev;
1324
1325 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1326
1327 vfd = video_device_alloc();
1328 if (NULL == vfd)
1329 return NULL;
1330
1331 *vfd = *template;
1332 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1333 type, saa7164_boards[dev->board].name);
1334
1335 vfd->parent = &pci->dev;
1336 vfd->release = video_device_release;
1337 return vfd;
1338}
1339
1340int saa7164_vbi_register(struct saa7164_port *port)
1341{
1342 struct saa7164_dev *dev = port->dev;
1343 int result = -ENODEV;
1344
1345 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1346
1347 if (port->type != SAA7164_MPEG_VBI)
1348 BUG();
1349
1350 /* Sanity check that the PCI configuration space is active */
1351 if (port->hwcfg.BARLocation == 0) {
1352 printk(KERN_ERR "%s() failed "
1353 "(errno = %d), NO PCI configuration\n",
1354 __func__, result);
1355 result = -ENOMEM;
1356 goto failed;
1357 }
1358
1359 /* Establish VBI defaults here */
1360
1361 /* Allocate and register the video device node */
1362 port->v4l_device = saa7164_vbi_alloc(port,
1363 dev->pci, &saa7164_vbi_template, "vbi");
1364
1365 if (port->v4l_device == NULL) {
1366 printk(KERN_INFO "%s: can't allocate vbi device\n",
1367 dev->name);
1368 result = -ENOMEM;
1369 goto failed;
1370 }
1371
1372 result = video_register_device(port->v4l_device,
1373 VFL_TYPE_VBI, -1);
1374 if (result < 0) {
1375 printk(KERN_INFO "%s: can't register vbi device\n",
1376 dev->name);
1377 /* TODO: We're going to leak here if we don't dealloc
1378 The buffers above. The unreg function can't deal wit it.
1379 */
1380 goto failed;
1381 }
1382
1383 printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
1384 dev->name, port->v4l_device->num);
1385
1386 /* Configure the hardware defaults */
1387
1388 result = 0;
1389failed:
1390 return result;
1391}
1392
1393void saa7164_vbi_unregister(struct saa7164_port *port)
1394{
1395 struct saa7164_dev *dev = port->dev;
1396
1397 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
1398
1399 if (port->type != SAA7164_MPEG_VBI)
1400 BUG();
1401
1402 if (port->v4l_device) {
1403 if (port->v4l_device->minor != -1)
1404 video_unregister_device(port->v4l_device);
1405 else
1406 video_device_release(port->v4l_device);
1407
1408 port->v4l_device = NULL;
1409 }
1410
1411}