blob: 9255d7d23947252299c12a5bf041e1ea2eda4203 [file] [log] [blame]
Steven Toth11bd27b22010-07-31 16:34:19 -03001/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
Steven Toth63a412e2015-03-23 16:08:15 -03004 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
Steven Toth11bd27b22010-07-31 16:34:19 -03005 *
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.
Steven Toth11bd27b22010-07-31 16:34:19 -030016 */
17
18#include "saa7164.h"
19
Steven Toth11bd27b22010-07-31 16:34:19 -030020/* Take the encoder configuration from the port struct and
21 * flush it to the hardware.
22 */
23static void saa7164_vbi_configure(struct saa7164_port *port)
24{
25 struct saa7164_dev *dev = port->dev;
26 dprintk(DBGLVL_VBI, "%s()\n", __func__);
27
Hans Verkuil225b7832015-08-28 08:48:33 -030028 port->vbi_params.width = port->enc_port->width;
29 port->vbi_params.height = port->enc_port->height;
Steven Toth11bd27b22010-07-31 16:34:19 -030030 port->vbi_params.is_50hz =
Hans Verkuil225b7832015-08-28 08:48:33 -030031 (port->enc_port->encodernorm.id & V4L2_STD_625_50) != 0;
Steven Toth11bd27b22010-07-31 16:34:19 -030032
33 /* Set up the DIF (enable it) for analog mode by default */
34 saa7164_api_initialize_dif(port);
Steven Toth11bd27b22010-07-31 16:34:19 -030035 dprintk(DBGLVL_VBI, "%s() ends\n", __func__);
36}
37
38static int saa7164_vbi_buffers_dealloc(struct saa7164_port *port)
39{
40 struct list_head *c, *n, *p, *q, *l, *v;
41 struct saa7164_dev *dev = port->dev;
42 struct saa7164_buffer *buf;
43 struct saa7164_user_buffer *ubuf;
44
45 /* Remove any allocated buffers */
46 mutex_lock(&port->dmaqueue_lock);
47
48 dprintk(DBGLVL_VBI, "%s(port=%d) dmaqueue\n", __func__, port->nr);
49 list_for_each_safe(c, n, &port->dmaqueue.list) {
50 buf = list_entry(c, struct saa7164_buffer, list);
51 list_del(c);
52 saa7164_buffer_dealloc(buf);
53 }
54
55 dprintk(DBGLVL_VBI, "%s(port=%d) used\n", __func__, port->nr);
56 list_for_each_safe(p, q, &port->list_buf_used.list) {
57 ubuf = list_entry(p, struct saa7164_user_buffer, list);
58 list_del(p);
59 saa7164_buffer_dealloc_user(ubuf);
60 }
61
62 dprintk(DBGLVL_VBI, "%s(port=%d) free\n", __func__, port->nr);
63 list_for_each_safe(l, v, &port->list_buf_free.list) {
64 ubuf = list_entry(l, struct saa7164_user_buffer, list);
65 list_del(l);
66 saa7164_buffer_dealloc_user(ubuf);
67 }
68
69 mutex_unlock(&port->dmaqueue_lock);
70 dprintk(DBGLVL_VBI, "%s(port=%d) done\n", __func__, port->nr);
71
72 return 0;
73}
74
75/* Dynamic buffer switch at vbi start time */
76static int saa7164_vbi_buffers_alloc(struct saa7164_port *port)
77{
78 struct saa7164_dev *dev = port->dev;
79 struct saa7164_buffer *buf;
80 struct saa7164_user_buffer *ubuf;
Mauro Carvalho Chehab4d270cf2010-10-11 17:17:45 -030081 struct tmHWStreamParameters *params = &port->hw_streamingparams;
Steven Toth11bd27b22010-07-31 16:34:19 -030082 int result = -ENODEV, i;
83 int len = 0;
84
85 dprintk(DBGLVL_VBI, "%s()\n", __func__);
86
87 /* TODO: NTSC SPECIFIC */
88 /* Init and establish defaults */
89 params->samplesperline = 1440;
90 params->numberoflines = 12;
91 params->numberoflines = 18;
92 params->pitch = 1600;
93 params->pitch = 1440;
94 params->numpagetables = 2 +
95 ((params->numberoflines * params->pitch) / PAGE_SIZE);
96 params->bitspersample = 8;
97 params->linethreshold = 0;
Peter Huewe61ca15002011-01-30 16:33:01 -030098 params->pagetablelistvirt = NULL;
99 params->pagetablelistphys = NULL;
Steven Toth11bd27b22010-07-31 16:34:19 -0300100 params->numpagetableentries = port->hwcfg.buffercount;
101
102 /* Allocate the PCI resources, buffers (hard) */
103 for (i = 0; i < port->hwcfg.buffercount; i++) {
104 buf = saa7164_buffer_alloc(port,
105 params->numberoflines *
106 params->pitch);
107
108 if (!buf) {
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -0200109 printk(KERN_ERR "%s() failed (errno = %d), unable to allocate buffer\n",
Steven Toth11bd27b22010-07-31 16:34:19 -0300110 __func__, result);
111 result = -ENOMEM;
112 goto failed;
113 } else {
114
115 mutex_lock(&port->dmaqueue_lock);
116 list_add_tail(&buf->list, &port->dmaqueue.list);
117 mutex_unlock(&port->dmaqueue_lock);
118
119 }
120 }
121
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200122 /* Allocate some kernel buffers for copying
Steven Toth11bd27b22010-07-31 16:34:19 -0300123 * to userpsace.
124 */
125 len = params->numberoflines * params->pitch;
126
127 if (vbi_buffers < 16)
128 vbi_buffers = 16;
129 if (vbi_buffers > 512)
130 vbi_buffers = 512;
131
132 for (i = 0; i < vbi_buffers; i++) {
133
134 ubuf = saa7164_buffer_alloc_user(dev, len);
135 if (ubuf) {
136 mutex_lock(&port->dmaqueue_lock);
137 list_add_tail(&ubuf->list, &port->list_buf_free.list);
138 mutex_unlock(&port->dmaqueue_lock);
139 }
140
141 }
142
143 result = 0;
144
145failed:
146 return result;
147}
148
149
150static int saa7164_vbi_initialize(struct saa7164_port *port)
151{
152 saa7164_vbi_configure(port);
153 return 0;
154}
155
156/* -- V4L2 --------------------------------------------------------- */
Hans Verkuil314527a2013-03-15 06:10:40 -0300157static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
Steven Toth11bd27b22010-07-31 16:34:19 -0300158{
159 struct saa7164_vbi_fh *fh = file->private_data;
Steven Toth11bd27b22010-07-31 16:34:19 -0300160
Hans Verkuil225b7832015-08-28 08:48:33 -0300161 return saa7164_s_std(fh->port->enc_port, id);
Steven Toth11bd27b22010-07-31 16:34:19 -0300162}
163
Hans Verkuil8d2d41e2013-06-03 05:36:45 -0300164static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
165{
166 struct saa7164_encoder_fh *fh = file->private_data;
Hans Verkuil8d2d41e2013-06-03 05:36:45 -0300167
Hans Verkuil225b7832015-08-28 08:48:33 -0300168 return saa7164_g_std(fh->port->enc_port, id);
Steven Toth11bd27b22010-07-31 16:34:19 -0300169}
170
171static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
172{
173 struct saa7164_vbi_fh *fh = file->private_data;
Steven Toth11bd27b22010-07-31 16:34:19 -0300174
Hans Verkuil225b7832015-08-28 08:48:33 -0300175 return saa7164_g_input(fh->port->enc_port, i);
Steven Toth11bd27b22010-07-31 16:34:19 -0300176}
177
178static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
179{
180 struct saa7164_vbi_fh *fh = file->private_data;
Steven Toth11bd27b22010-07-31 16:34:19 -0300181
Hans Verkuil225b7832015-08-28 08:48:33 -0300182 return saa7164_s_input(fh->port->enc_port, i);
Steven Toth11bd27b22010-07-31 16:34:19 -0300183}
184
185static int vidioc_g_frequency(struct file *file, void *priv,
186 struct v4l2_frequency *f)
187{
188 struct saa7164_vbi_fh *fh = file->private_data;
Steven Toth11bd27b22010-07-31 16:34:19 -0300189
Hans Verkuil225b7832015-08-28 08:48:33 -0300190 return saa7164_g_frequency(fh->port->enc_port, f);
Steven Toth11bd27b22010-07-31 16:34:19 -0300191}
192
193static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -0300194 const struct v4l2_frequency *f)
Steven Toth11bd27b22010-07-31 16:34:19 -0300195{
196 struct saa7164_vbi_fh *fh = file->private_data;
Hans Verkuil225b7832015-08-28 08:48:33 -0300197 int ret = saa7164_s_frequency(fh->port->enc_port, f);
Steven Toth11bd27b22010-07-31 16:34:19 -0300198
Hans Verkuil225b7832015-08-28 08:48:33 -0300199 if (ret == 0)
200 saa7164_vbi_initialize(fh->port);
201 return ret;
Steven Toth11bd27b22010-07-31 16:34:19 -0300202}
203
Steven Toth11bd27b22010-07-31 16:34:19 -0300204static int vidioc_querycap(struct file *file, void *priv,
205 struct v4l2_capability *cap)
206{
207 struct saa7164_vbi_fh *fh = file->private_data;
208 struct saa7164_port *port = fh->port;
209 struct saa7164_dev *dev = port->dev;
210
211 strcpy(cap->driver, dev->name);
212 strlcpy(cap->card, saa7164_boards[dev->board].name,
213 sizeof(cap->card));
214 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
215
Hans Verkuil534bc3e2015-03-27 15:17:56 -0300216 cap->device_caps =
Steven Toth11bd27b22010-07-31 16:34:19 -0300217 V4L2_CAP_VBI_CAPTURE |
Hans Verkuil534bc3e2015-03-27 15:17:56 -0300218 V4L2_CAP_READWRITE |
219 V4L2_CAP_TUNER;
Steven Toth11bd27b22010-07-31 16:34:19 -0300220
Hans Verkuil534bc3e2015-03-27 15:17:56 -0300221 cap->capabilities = cap->device_caps |
222 V4L2_CAP_VIDEO_CAPTURE |
223 V4L2_CAP_DEVICE_CAPS;
Steven Toth11bd27b22010-07-31 16:34:19 -0300224
225 return 0;
226}
227
Steven Toth11bd27b22010-07-31 16:34:19 -0300228static int saa7164_vbi_stop_port(struct saa7164_port *port)
229{
230 struct saa7164_dev *dev = port->dev;
231 int ret;
232
233 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
234 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
235 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
236 __func__, ret);
237 ret = -EIO;
238 } else {
239 dprintk(DBGLVL_VBI, "%s() Stopped\n", __func__);
240 ret = 0;
241 }
242
243 return ret;
244}
245
246static int saa7164_vbi_acquire_port(struct saa7164_port *port)
247{
248 struct saa7164_dev *dev = port->dev;
249 int ret;
250
251 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
252 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
253 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
254 __func__, ret);
255 ret = -EIO;
256 } else {
257 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
258 ret = 0;
259 }
260
261 return ret;
262}
263
264static int saa7164_vbi_pause_port(struct saa7164_port *port)
265{
266 struct saa7164_dev *dev = port->dev;
267 int ret;
268
269 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
270 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
271 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
272 __func__, ret);
273 ret = -EIO;
274 } else {
275 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
276 ret = 0;
277 }
278
279 return ret;
280}
281
282/* Firmware is very windows centric, meaning you have to transition
283 * the part through AVStream / KS Windows stages, forwards or backwards.
284 * States are: stopped, acquired (h/w), paused, started.
285 * We have to leave here will all of the soft buffers on the free list,
286 * else the cfg_post() func won't have soft buffers to correctly configure.
287 */
288static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
289{
290 struct saa7164_dev *dev = port->dev;
291 struct saa7164_buffer *buf;
292 struct saa7164_user_buffer *ubuf;
293 struct list_head *c, *n;
294 int ret;
295
296 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
297
298 ret = saa7164_vbi_pause_port(port);
299 ret = saa7164_vbi_acquire_port(port);
300 ret = saa7164_vbi_stop_port(port);
301
302 dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
303 port->nr);
304
305 /* Reset the state of any allocated buffer resources */
306 mutex_lock(&port->dmaqueue_lock);
307
308 /* Reset the hard and soft buffer state */
309 list_for_each_safe(c, n, &port->dmaqueue.list) {
310 buf = list_entry(c, struct saa7164_buffer, list);
311 buf->flags = SAA7164_BUFFER_FREE;
312 buf->pos = 0;
313 }
314
315 list_for_each_safe(c, n, &port->list_buf_used.list) {
316 ubuf = list_entry(c, struct saa7164_user_buffer, list);
317 ubuf->pos = 0;
318 list_move_tail(&ubuf->list, &port->list_buf_free.list);
319 }
320
321 mutex_unlock(&port->dmaqueue_lock);
322
323 /* Free any allocated resources */
324 saa7164_vbi_buffers_dealloc(port);
325
326 dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
327
328 return ret;
329}
330
331static int saa7164_vbi_start_streaming(struct saa7164_port *port)
332{
333 struct saa7164_dev *dev = port->dev;
334 int result, ret = 0;
335
336 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
337
338 port->done_first_interrupt = 0;
339
340 /* allocate all of the PCIe DMA buffer resources on the fly,
341 * allowing switching between TS and PS payloads without
342 * requiring a complete driver reload.
343 */
344 saa7164_vbi_buffers_alloc(port);
345
346 /* Configure the encoder with any cache values */
Steven Tothbc250682010-11-12 18:32:36 -0300347#if 0
348 saa7164_api_set_encoder(port);
349 saa7164_api_get_encoder(port);
350#endif
Steven Toth11bd27b22010-07-31 16:34:19 -0300351
352 /* Place the empty buffers on the hardware */
353 saa7164_buffer_cfg_port(port);
354
355 /* Negotiate format */
356 if (saa7164_api_set_vbi_format(port) != SAA_OK) {
357 printk(KERN_ERR "%s() No supported VBI format\n", __func__);
358 ret = -EIO;
359 goto out;
360 }
361
362 /* Acquire the hardware */
363 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
364 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
365 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
366 __func__, result);
367
368 ret = -EIO;
369 goto out;
370 } else
371 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
372
373 /* Pause the hardware */
374 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
375 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
376 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
377 __func__, result);
378
379 /* Stop the hardware, regardless */
380 result = saa7164_vbi_stop_port(port);
Dan Carpenter2e710642012-04-20 02:50:45 -0300381 if (result != SAA_OK) {
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -0200382 printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n",
383 __func__, result);
Steven Toth11bd27b22010-07-31 16:34:19 -0300384 }
385
386 ret = -EIO;
387 goto out;
388 } else
389 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
390
391 /* Start the hardware */
392 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
393 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
394 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
395 __func__, result);
396
397 /* Stop the hardware, regardless */
398 result = saa7164_vbi_acquire_port(port);
399 result = saa7164_vbi_stop_port(port);
Dan Carpenter2e710642012-04-20 02:50:45 -0300400 if (result != SAA_OK) {
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -0200401 printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n",
402 __func__, result);
Steven Toth11bd27b22010-07-31 16:34:19 -0300403 }
404
405 ret = -EIO;
406 } else
407 dprintk(DBGLVL_VBI, "%s() Running\n", __func__);
408
409out:
410 return ret;
411}
412
Mauro Carvalho Chehab5faf7db2012-10-27 13:11:11 -0300413static int saa7164_vbi_fmt(struct file *file, void *priv,
414 struct v4l2_format *f)
Steven Toth11bd27b22010-07-31 16:34:19 -0300415{
Steven Toth11bd27b22010-07-31 16:34:19 -0300416 /* ntsc */
Steven Toth11bd27b22010-07-31 16:34:19 -0300417 f->fmt.vbi.samples_per_line = 1440;
418 f->fmt.vbi.sampling_rate = 27000000;
419 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
420 f->fmt.vbi.offset = 0;
421 f->fmt.vbi.flags = 0;
422 f->fmt.vbi.start[0] = 10;
423 f->fmt.vbi.count[0] = 18;
424 f->fmt.vbi.start[1] = 263 + 10 + 1;
425 f->fmt.vbi.count[1] = 18;
Hans Verkuil031d2292015-08-28 08:48:30 -0300426 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Steven Toth11bd27b22010-07-31 16:34:19 -0300427 return 0;
428}
429
430static int fops_open(struct file *file)
431{
Steven Toth214ce3f2010-10-06 21:52:22 -0300432 struct saa7164_dev *dev;
433 struct saa7164_port *port;
Steven Toth11bd27b22010-07-31 16:34:19 -0300434 struct saa7164_vbi_fh *fh;
Steven Toth214ce3f2010-10-06 21:52:22 -0300435
436 port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
437 if (!port)
438 return -ENODEV;
439
440 dev = port->dev;
Steven Toth11bd27b22010-07-31 16:34:19 -0300441
442 dprintk(DBGLVL_VBI, "%s()\n", __func__);
443
Steven Toth11bd27b22010-07-31 16:34:19 -0300444 /* allocate + initialize per filehandle data */
445 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Steven Toth214ce3f2010-10-06 21:52:22 -0300446 if (NULL == fh)
Steven Toth11bd27b22010-07-31 16:34:19 -0300447 return -ENOMEM;
Steven Toth11bd27b22010-07-31 16:34:19 -0300448
Steven Toth11bd27b22010-07-31 16:34:19 -0300449 fh->port = port;
Hans Verkuild6d3fe22015-08-28 08:48:27 -0300450 v4l2_fh_init(&fh->fh, video_devdata(file));
451 v4l2_fh_add(&fh->fh);
452 file->private_data = fh;
Steven Toth11bd27b22010-07-31 16:34:19 -0300453
Steven Toth11bd27b22010-07-31 16:34:19 -0300454 return 0;
455}
456
457static int fops_release(struct file *file)
458{
459 struct saa7164_vbi_fh *fh = file->private_data;
460 struct saa7164_port *port = fh->port;
461 struct saa7164_dev *dev = port->dev;
462
463 dprintk(DBGLVL_VBI, "%s()\n", __func__);
464
465 /* Shut device down on last close */
466 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
467 if (atomic_dec_return(&port->v4l_reader_count) == 0) {
468 /* stop vbi capture then cancel buffers */
469 saa7164_vbi_stop_streaming(port);
470 }
471 }
472
Hans Verkuild6d3fe22015-08-28 08:48:27 -0300473 v4l2_fh_del(&fh->fh);
474 v4l2_fh_exit(&fh->fh);
Steven Toth11bd27b22010-07-31 16:34:19 -0300475 kfree(fh);
476
477 return 0;
478}
479
Mauro Carvalho Chehab5faf7db2012-10-27 13:11:11 -0300480static struct
481saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
Steven Toth11bd27b22010-07-31 16:34:19 -0300482{
Peter Huewe61ca15002011-01-30 16:33:01 -0300483 struct saa7164_user_buffer *ubuf = NULL;
Steven Toth11bd27b22010-07-31 16:34:19 -0300484 struct saa7164_dev *dev = port->dev;
485 u32 crc;
486
487 mutex_lock(&port->dmaqueue_lock);
488 if (!list_empty(&port->list_buf_used.list)) {
489 ubuf = list_first_entry(&port->list_buf_used.list,
490 struct saa7164_user_buffer, list);
491
492 if (crc_checking) {
493 crc = crc32(0, ubuf->data, ubuf->actual_size);
494 if (crc != ubuf->crc) {
Steven Tothbc250682010-11-12 18:32:36 -0300495 printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
496 __func__,
Steven Toth11bd27b22010-07-31 16:34:19 -0300497 ubuf, ubuf->crc, crc);
498 }
499 }
500
501 }
502 mutex_unlock(&port->dmaqueue_lock);
503
504 dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
505
506 return ubuf;
507}
508
509static ssize_t fops_read(struct file *file, char __user *buffer,
510 size_t count, loff_t *pos)
511{
512 struct saa7164_vbi_fh *fh = file->private_data;
513 struct saa7164_port *port = fh->port;
514 struct saa7164_user_buffer *ubuf = NULL;
515 struct saa7164_dev *dev = port->dev;
Gavin Hurlbut106d7e32010-09-30 18:21:20 -0300516 int ret = 0;
Steven Toth11bd27b22010-07-31 16:34:19 -0300517 int rem, cnt;
518 u8 *p;
519
520 port->last_read_msecs_diff = port->last_read_msecs;
521 port->last_read_msecs = jiffies_to_msecs(jiffies);
522 port->last_read_msecs_diff = port->last_read_msecs -
523 port->last_read_msecs_diff;
524
525 saa7164_histogram_update(&port->read_interval,
526 port->last_read_msecs_diff);
527
528 if (*pos) {
529 printk(KERN_ERR "%s() ESPIPE\n", __func__);
530 return -ESPIPE;
531 }
532
533 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
534 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
535
536 if (saa7164_vbi_initialize(port) < 0) {
537 printk(KERN_ERR "%s() EINVAL\n", __func__);
538 return -EINVAL;
539 }
540
541 saa7164_vbi_start_streaming(port);
542 msleep(200);
543 }
544 }
545
546 /* blocking wait for buffer */
547 if ((file->f_flags & O_NONBLOCK) == 0) {
548 if (wait_event_interruptible(port->wait_read,
549 saa7164_vbi_next_buf(port))) {
550 printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
551 return -ERESTARTSYS;
552 }
553 }
554
555 /* Pull the first buffer from the used list */
556 ubuf = saa7164_vbi_next_buf(port);
557
558 while ((count > 0) && ubuf) {
559
560 /* set remaining bytes to copy */
561 rem = ubuf->actual_size - ubuf->pos;
562 cnt = rem > count ? count : rem;
563
564 p = ubuf->data + ubuf->pos;
565
566 dprintk(DBGLVL_VBI,
567 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
568 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
569
570 if (copy_to_user(buffer, p, cnt)) {
571 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
572 if (!ret) {
573 printk(KERN_ERR "%s() EFAULT\n", __func__);
574 ret = -EFAULT;
575 }
576 goto err;
577 }
578
579 ubuf->pos += cnt;
580 count -= cnt;
581 buffer += cnt;
582 ret += cnt;
583
Steven Tothbc250682010-11-12 18:32:36 -0300584 if (ubuf->pos > ubuf->actual_size)
Steven Toth11bd27b22010-07-31 16:34:19 -0300585 printk(KERN_ERR "read() pos > actual, huh?\n");
Steven Toth11bd27b22010-07-31 16:34:19 -0300586
587 if (ubuf->pos == ubuf->actual_size) {
588
589 /* finished with current buffer, take next buffer */
590
591 /* Requeue the buffer on the free list */
592 ubuf->pos = 0;
593
594 mutex_lock(&port->dmaqueue_lock);
595 list_move_tail(&ubuf->list, &port->list_buf_free.list);
596 mutex_unlock(&port->dmaqueue_lock);
597
598 /* Dequeue next */
599 if ((file->f_flags & O_NONBLOCK) == 0) {
600 if (wait_event_interruptible(port->wait_read,
601 saa7164_vbi_next_buf(port))) {
602 break;
603 }
604 }
605 ubuf = saa7164_vbi_next_buf(port);
606 }
607 }
608err:
609 if (!ret && !ubuf) {
610 printk(KERN_ERR "%s() EAGAIN\n", __func__);
611 ret = -EAGAIN;
612 }
613
614 return ret;
615}
616
617static unsigned int fops_poll(struct file *file, poll_table *wait)
618{
619 struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
620 struct saa7164_port *port = fh->port;
Steven Toth11bd27b22010-07-31 16:34:19 -0300621 unsigned int mask = 0;
622
623 port->last_poll_msecs_diff = port->last_poll_msecs;
624 port->last_poll_msecs = jiffies_to_msecs(jiffies);
625 port->last_poll_msecs_diff = port->last_poll_msecs -
626 port->last_poll_msecs_diff;
627
628 saa7164_histogram_update(&port->poll_interval,
629 port->last_poll_msecs_diff);
630
Steven Tothbc250682010-11-12 18:32:36 -0300631 if (!video_is_registered(port->v4l_device))
Steven Toth11bd27b22010-07-31 16:34:19 -0300632 return -EIO;
Steven Toth11bd27b22010-07-31 16:34:19 -0300633
634 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
635 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
636 if (saa7164_vbi_initialize(port) < 0)
637 return -EINVAL;
638 saa7164_vbi_start_streaming(port);
639 msleep(200);
640 }
641 }
642
643 /* blocking wait for buffer */
644 if ((file->f_flags & O_NONBLOCK) == 0) {
645 if (wait_event_interruptible(port->wait_read,
646 saa7164_vbi_next_buf(port))) {
647 return -ERESTARTSYS;
648 }
649 }
650
651 /* Pull the first buffer from the used list */
Dan Carpenter061d55e2010-11-24 02:36:57 -0300652 if (!list_empty(&port->list_buf_used.list))
Steven Toth11bd27b22010-07-31 16:34:19 -0300653 mask |= POLLIN | POLLRDNORM;
654
655 return mask;
656}
657static const struct v4l2_file_operations vbi_fops = {
658 .owner = THIS_MODULE,
659 .open = fops_open,
660 .release = fops_release,
661 .read = fops_read,
662 .poll = fops_poll,
663 .unlocked_ioctl = video_ioctl2,
664};
665
666static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
667 .vidioc_s_std = vidioc_s_std,
Hans Verkuil8d2d41e2013-06-03 05:36:45 -0300668 .vidioc_g_std = vidioc_g_std,
Hans Verkuil225b7832015-08-28 08:48:33 -0300669 .vidioc_enum_input = saa7164_enum_input,
Steven Toth11bd27b22010-07-31 16:34:19 -0300670 .vidioc_g_input = vidioc_g_input,
671 .vidioc_s_input = vidioc_s_input,
Hans Verkuil225b7832015-08-28 08:48:33 -0300672 .vidioc_g_tuner = saa7164_g_tuner,
673 .vidioc_s_tuner = saa7164_s_tuner,
Steven Toth11bd27b22010-07-31 16:34:19 -0300674 .vidioc_g_frequency = vidioc_g_frequency,
675 .vidioc_s_frequency = vidioc_s_frequency,
Steven Toth11bd27b22010-07-31 16:34:19 -0300676 .vidioc_querycap = vidioc_querycap,
Steven Toth11bd27b22010-07-31 16:34:19 -0300677 .vidioc_g_fmt_vbi_cap = saa7164_vbi_fmt,
678 .vidioc_try_fmt_vbi_cap = saa7164_vbi_fmt,
679 .vidioc_s_fmt_vbi_cap = saa7164_vbi_fmt,
680};
681
682static struct video_device saa7164_vbi_template = {
683 .name = "saa7164",
684 .fops = &vbi_fops,
685 .ioctl_ops = &vbi_ioctl_ops,
686 .minor = -1,
687 .tvnorms = SAA7164_NORMS,
Steven Toth11bd27b22010-07-31 16:34:19 -0300688};
689
690static struct video_device *saa7164_vbi_alloc(
691 struct saa7164_port *port,
692 struct pci_dev *pci,
693 struct video_device *template,
694 char *type)
695{
696 struct video_device *vfd;
697 struct saa7164_dev *dev = port->dev;
698
699 dprintk(DBGLVL_VBI, "%s()\n", __func__);
700
701 vfd = video_device_alloc();
702 if (NULL == vfd)
703 return NULL;
704
705 *vfd = *template;
706 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
707 type, saa7164_boards[dev->board].name);
708
Hans Verkuild66de792013-06-12 05:49:50 -0300709 vfd->v4l2_dev = &dev->v4l2_dev;
Steven Toth11bd27b22010-07-31 16:34:19 -0300710 vfd->release = video_device_release;
711 return vfd;
712}
713
714int saa7164_vbi_register(struct saa7164_port *port)
715{
716 struct saa7164_dev *dev = port->dev;
717 int result = -ENODEV;
718
719 dprintk(DBGLVL_VBI, "%s()\n", __func__);
720
721 if (port->type != SAA7164_MPEG_VBI)
722 BUG();
723
724 /* Sanity check that the PCI configuration space is active */
725 if (port->hwcfg.BARLocation == 0) {
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -0200726 printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n",
Steven Toth11bd27b22010-07-31 16:34:19 -0300727 __func__, result);
728 result = -ENOMEM;
729 goto failed;
730 }
731
732 /* Establish VBI defaults here */
733
734 /* Allocate and register the video device node */
735 port->v4l_device = saa7164_vbi_alloc(port,
736 dev->pci, &saa7164_vbi_template, "vbi");
737
Peter Huewe61ca15002011-01-30 16:33:01 -0300738 if (!port->v4l_device) {
Steven Toth11bd27b22010-07-31 16:34:19 -0300739 printk(KERN_INFO "%s: can't allocate vbi device\n",
740 dev->name);
741 result = -ENOMEM;
742 goto failed;
743 }
744
Hans Verkuil225b7832015-08-28 08:48:33 -0300745 port->enc_port = &dev->ports[port->nr - 2];
Steven Toth214ce3f2010-10-06 21:52:22 -0300746 video_set_drvdata(port->v4l_device, port);
Steven Toth11bd27b22010-07-31 16:34:19 -0300747 result = video_register_device(port->v4l_device,
748 VFL_TYPE_VBI, -1);
749 if (result < 0) {
750 printk(KERN_INFO "%s: can't register vbi device\n",
751 dev->name);
752 /* TODO: We're going to leak here if we don't dealloc
753 The buffers above. The unreg function can't deal wit it.
754 */
755 goto failed;
756 }
757
758 printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
759 dev->name, port->v4l_device->num);
760
761 /* Configure the hardware defaults */
762
763 result = 0;
764failed:
765 return result;
766}
767
768void saa7164_vbi_unregister(struct saa7164_port *port)
769{
770 struct saa7164_dev *dev = port->dev;
771
772 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
773
774 if (port->type != SAA7164_MPEG_VBI)
775 BUG();
776
777 if (port->v4l_device) {
778 if (port->v4l_device->minor != -1)
779 video_unregister_device(port->v4l_device);
780 else
781 video_device_release(port->v4l_device);
782
783 port->v4l_device = NULL;
784 }
785
786}