blob: dcfabad8b284ac3d0e5f765486deb3ffd999edae [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Steven Toth9b8b0192010-07-31 14:39:44 -03002/*
3 * Driver for the NXP SAA7164 PCIe bridge
4 *
Steven Toth63a412e2015-03-23 16:08:15 -03005 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
Steven Toth9b8b0192010-07-31 14:39:44 -03006 */
7
8#include "saa7164.h"
9
Steven Toth7615e432010-07-31 14:44:53 -030010#define ENCODER_MAX_BITRATE 6500000
11#define ENCODER_MIN_BITRATE 1000000
12#define ENCODER_DEF_BITRATE 5000000
13
Hans Verkuil031d2292015-08-28 08:48:30 -030014/*
15 * This is a dummy non-zero value for the sizeimage field of v4l2_pix_format.
16 * It is not actually used for anything since this driver does not support
17 * stream I/O, only read(), and because this driver produces an MPEG stream
18 * and not discrete frames. But the V4L2 spec doesn't allow for this value
19 * to be 0, so set it to 0x10000 instead.
20 *
21 * If we ever change this driver to support stream I/O, then this field
22 * will be the size of the streaming buffers.
23 */
24#define SAA7164_SIZEIMAGE (0x10000)
25
Steven Toth7615e432010-07-31 14:44:53 -030026static struct saa7164_tvnorm saa7164_tvnorms[] = {
27 {
28 .name = "NTSC-M",
29 .id = V4L2_STD_NTSC_M,
30 }, {
31 .name = "NTSC-JP",
32 .id = V4L2_STD_NTSC_M_JP,
33 }
34};
35
Steven Toth7615e432010-07-31 14:44:53 -030036/* Take the encoder configuration form the port struct and
37 * flush it to the hardware.
38 */
39static void saa7164_encoder_configure(struct saa7164_port *port)
40{
41 struct saa7164_dev *dev = port->dev;
42 dprintk(DBGLVL_ENC, "%s()\n", __func__);
43
44 port->encoder_params.width = port->width;
45 port->encoder_params.height = port->height;
46 port->encoder_params.is_50hz =
47 (port->encodernorm.id & V4L2_STD_625_50) != 0;
48
49 /* Set up the DIF (enable it) for analog mode by default */
50 saa7164_api_initialize_dif(port);
51
52 /* Configure the correct video standard */
53 saa7164_api_configure_dif(port, port->encodernorm.id);
54
55 /* Ensure the audio decoder is correct configured */
56 saa7164_api_set_audio_std(port);
57}
58
Steven Toth1b0e8e42010-07-31 16:01:00 -030059static int saa7164_encoder_buffers_dealloc(struct saa7164_port *port)
60{
61 struct list_head *c, *n, *p, *q, *l, *v;
62 struct saa7164_dev *dev = port->dev;
63 struct saa7164_buffer *buf;
64 struct saa7164_user_buffer *ubuf;
65
66 /* Remove any allocated buffers */
67 mutex_lock(&port->dmaqueue_lock);
68
69 dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
70 list_for_each_safe(c, n, &port->dmaqueue.list) {
71 buf = list_entry(c, struct saa7164_buffer, list);
72 list_del(c);
73 saa7164_buffer_dealloc(buf);
74 }
75
76 dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
77 list_for_each_safe(p, q, &port->list_buf_used.list) {
78 ubuf = list_entry(p, struct saa7164_user_buffer, list);
79 list_del(p);
80 saa7164_buffer_dealloc_user(ubuf);
81 }
82
83 dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
84 list_for_each_safe(l, v, &port->list_buf_free.list) {
85 ubuf = list_entry(l, struct saa7164_user_buffer, list);
86 list_del(l);
87 saa7164_buffer_dealloc_user(ubuf);
88 }
89
90 mutex_unlock(&port->dmaqueue_lock);
91 dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
92
93 return 0;
94}
95
96/* Dynamic buffer switch at encoder start time */
97static int saa7164_encoder_buffers_alloc(struct saa7164_port *port)
Steven Toth7615e432010-07-31 14:44:53 -030098{
99 struct saa7164_dev *dev = port->dev;
Steven Toth1b0e8e42010-07-31 16:01:00 -0300100 struct saa7164_buffer *buf;
101 struct saa7164_user_buffer *ubuf;
Mauro Carvalho Chehab4d270cf2010-10-11 17:17:45 -0300102 struct tmHWStreamParameters *params = &port->hw_streamingparams;
Steven Toth1b0e8e42010-07-31 16:01:00 -0300103 int result = -ENODEV, i;
104 int len = 0;
Steven Toth7615e432010-07-31 14:44:53 -0300105
106 dprintk(DBGLVL_ENC, "%s()\n", __func__);
107
Steven Tothbc250682010-11-12 18:32:36 -0300108 if (port->encoder_params.stream_type ==
109 V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
110 dprintk(DBGLVL_ENC,
111 "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n",
112 __func__);
Steven Toth1b0e8e42010-07-31 16:01:00 -0300113 params->samplesperline = 128;
114 params->numberoflines = 256;
115 params->pitch = 128;
116 params->numpagetables = 2 +
117 ((SAA7164_PS_NUMBER_OF_LINES * 128) / PAGE_SIZE);
118 } else
Steven Tothbc250682010-11-12 18:32:36 -0300119 if (port->encoder_params.stream_type ==
120 V4L2_MPEG_STREAM_TYPE_MPEG2_TS) {
121 dprintk(DBGLVL_ENC,
122 "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n",
123 __func__);
Steven Toth1b0e8e42010-07-31 16:01:00 -0300124 params->samplesperline = 188;
125 params->numberoflines = 312;
126 params->pitch = 188;
127 params->numpagetables = 2 +
128 ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
129 } else
130 BUG();
Steven Toth7615e432010-07-31 14:44:53 -0300131
Steven Toth1b0e8e42010-07-31 16:01:00 -0300132 /* Init and establish defaults */
133 params->bitspersample = 8;
134 params->linethreshold = 0;
Peter Huewe61ca15002011-01-30 16:33:01 -0300135 params->pagetablelistvirt = NULL;
136 params->pagetablelistphys = NULL;
Steven Toth1b0e8e42010-07-31 16:01:00 -0300137 params->numpagetableentries = port->hwcfg.buffercount;
138
139 /* Allocate the PCI resources, buffers (hard) */
140 for (i = 0; i < port->hwcfg.buffercount; i++) {
141 buf = saa7164_buffer_alloc(port,
142 params->numberoflines *
143 params->pitch);
144
145 if (!buf) {
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -0200146 printk(KERN_ERR "%s() failed (errno = %d), unable to allocate buffer\n",
Steven Toth1b0e8e42010-07-31 16:01:00 -0300147 __func__, result);
148 result = -ENOMEM;
149 goto failed;
150 } else {
151
152 mutex_lock(&port->dmaqueue_lock);
153 list_add_tail(&buf->list, &port->dmaqueue.list);
154 mutex_unlock(&port->dmaqueue_lock);
155
156 }
157 }
158
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200159 /* Allocate some kernel buffers for copying
Steven Toth1b0e8e42010-07-31 16:01:00 -0300160 * to userpsace.
161 */
162 len = params->numberoflines * params->pitch;
163
164 if (encoder_buffers < 16)
165 encoder_buffers = 16;
166 if (encoder_buffers > 512)
167 encoder_buffers = 512;
168
169 for (i = 0; i < encoder_buffers; i++) {
170
171 ubuf = saa7164_buffer_alloc_user(dev, len);
172 if (ubuf) {
173 mutex_lock(&port->dmaqueue_lock);
174 list_add_tail(&ubuf->list, &port->list_buf_free.list);
175 mutex_unlock(&port->dmaqueue_lock);
176 }
177
178 }
179
180 result = 0;
181
182failed:
183 return result;
184}
185
186static int saa7164_encoder_initialize(struct saa7164_port *port)
187{
188 saa7164_encoder_configure(port);
Steven Toth7615e432010-07-31 14:44:53 -0300189 return 0;
190}
191
192/* -- V4L2 --------------------------------------------------------- */
Hans Verkuil225b7832015-08-28 08:48:33 -0300193int saa7164_s_std(struct saa7164_port *port, v4l2_std_id id)
Steven Toth7615e432010-07-31 14:44:53 -0300194{
Steven Toth7615e432010-07-31 14:44:53 -0300195 struct saa7164_dev *dev = port->dev;
196 unsigned int i;
197
Hans Verkuil314527a2013-03-15 06:10:40 -0300198 dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)id);
Steven Toth7615e432010-07-31 14:44:53 -0300199
200 for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
Hans Verkuil314527a2013-03-15 06:10:40 -0300201 if (id & saa7164_tvnorms[i].id)
Steven Toth7615e432010-07-31 14:44:53 -0300202 break;
203 }
204 if (i == ARRAY_SIZE(saa7164_tvnorms))
205 return -EINVAL;
206
207 port->encodernorm = saa7164_tvnorms[i];
Hans Verkuil8d2d41e2013-06-03 05:36:45 -0300208 port->std = id;
Steven Toth7615e432010-07-31 14:44:53 -0300209
210 /* Update the audio decoder while is not running in
211 * auto detect mode.
212 */
213 saa7164_api_set_audio_std(port);
214
Hans Verkuil314527a2013-03-15 06:10:40 -0300215 dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)id);
Steven Toth7615e432010-07-31 14:44:53 -0300216
217 return 0;
218}
219
Hans Verkuil225b7832015-08-28 08:48:33 -0300220static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
Hans Verkuil8d2d41e2013-06-03 05:36:45 -0300221{
222 struct saa7164_encoder_fh *fh = file->private_data;
Hans Verkuil8d2d41e2013-06-03 05:36:45 -0300223
Hans Verkuil225b7832015-08-28 08:48:33 -0300224 return saa7164_s_std(fh->port, id);
225}
226
227int saa7164_g_std(struct saa7164_port *port, v4l2_std_id *id)
228{
Hans Verkuil8d2d41e2013-06-03 05:36:45 -0300229 *id = port->std;
230 return 0;
231}
232
Hans Verkuil225b7832015-08-28 08:48:33 -0300233static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
234{
235 struct saa7164_encoder_fh *fh = file->private_data;
236
237 return saa7164_g_std(fh->port, id);
238}
239
240int saa7164_enum_input(struct file *file, void *priv, struct v4l2_input *i)
Steven Toth7615e432010-07-31 14:44:53 -0300241{
Hans Verkuil6b996122015-08-28 08:48:32 -0300242 static const char * const inputs[] = {
243 "tuner", "composite", "svideo", "aux",
244 "composite 2", "svideo 2", "aux 2"
245 };
Steven Toth7615e432010-07-31 14:44:53 -0300246 int n;
247
Steven Toth7615e432010-07-31 14:44:53 -0300248 if (i->index >= 7)
249 return -EINVAL;
250
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400251 strscpy(i->name, inputs[i->index], sizeof(i->name));
Steven Toth7615e432010-07-31 14:44:53 -0300252
253 if (i->index == 0)
254 i->type = V4L2_INPUT_TYPE_TUNER;
255 else
256 i->type = V4L2_INPUT_TYPE_CAMERA;
257
258 for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
259 i->std |= saa7164_tvnorms[n].id;
260
261 return 0;
262}
263
Hans Verkuil225b7832015-08-28 08:48:33 -0300264int saa7164_g_input(struct saa7164_port *port, unsigned int *i)
Steven Toth7615e432010-07-31 14:44:53 -0300265{
Steven Toth7615e432010-07-31 14:44:53 -0300266 struct saa7164_dev *dev = port->dev;
267
268 if (saa7164_api_get_videomux(port) != SAA_OK)
269 return -EIO;
270
271 *i = (port->mux_input - 1);
272
273 dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);
274
275 return 0;
276}
277
Hans Verkuil225b7832015-08-28 08:48:33 -0300278static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Steven Toth7615e432010-07-31 14:44:53 -0300279{
Steven Toth96d84202010-07-31 16:04:59 -0300280 struct saa7164_encoder_fh *fh = file->private_data;
Hans Verkuil225b7832015-08-28 08:48:33 -0300281
282 return saa7164_g_input(fh->port, i);
283}
284
285int saa7164_s_input(struct saa7164_port *port, unsigned int i)
286{
Steven Toth7615e432010-07-31 14:44:53 -0300287 struct saa7164_dev *dev = port->dev;
288
289 dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);
290
291 if (i >= 7)
292 return -EINVAL;
293
294 port->mux_input = i + 1;
295
296 if (saa7164_api_set_videomux(port) != SAA_OK)
297 return -EIO;
298
299 return 0;
300}
301
Hans Verkuil225b7832015-08-28 08:48:33 -0300302static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
303{
304 struct saa7164_encoder_fh *fh = file->private_data;
305
306 return saa7164_s_input(fh->port, i);
307}
308
309int saa7164_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Steven Toth7615e432010-07-31 14:44:53 -0300310{
Steven Toth96d84202010-07-31 16:04:59 -0300311 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300312 struct saa7164_port *port = fh->port;
313 struct saa7164_dev *dev = port->dev;
314
315 if (0 != t->index)
316 return -EINVAL;
317
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400318 strscpy(t->name, "tuner", sizeof(t->name));
Steven Toth7615e432010-07-31 14:44:53 -0300319 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
Hans Verkuil6b996122015-08-28 08:48:32 -0300320 t->rangelow = SAA7164_TV_MIN_FREQ;
321 t->rangehigh = SAA7164_TV_MAX_FREQ;
Steven Toth7615e432010-07-31 14:44:53 -0300322
323 dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
324
325 return 0;
326}
327
Hans Verkuil225b7832015-08-28 08:48:33 -0300328int saa7164_s_tuner(struct file *file, void *priv,
329 const struct v4l2_tuner *t)
Steven Toth7615e432010-07-31 14:44:53 -0300330{
Hans Verkuil6b996122015-08-28 08:48:32 -0300331 if (0 != t->index)
332 return -EINVAL;
333
Steven Toth7615e432010-07-31 14:44:53 -0300334 /* Update the A/V core */
Steven Toth7615e432010-07-31 14:44:53 -0300335 return 0;
336}
337
Hans Verkuil225b7832015-08-28 08:48:33 -0300338int saa7164_g_frequency(struct saa7164_port *port, struct v4l2_frequency *f)
Steven Toth7615e432010-07-31 14:44:53 -0300339{
Hans Verkuil6b996122015-08-28 08:48:32 -0300340 if (f->tuner)
341 return -EINVAL;
342
Steven Toth7615e432010-07-31 14:44:53 -0300343 f->frequency = port->freq;
Steven Toth7615e432010-07-31 14:44:53 -0300344 return 0;
345}
346
Hans Verkuil225b7832015-08-28 08:48:33 -0300347static int vidioc_g_frequency(struct file *file, void *priv,
348 struct v4l2_frequency *f)
Steven Toth7615e432010-07-31 14:44:53 -0300349{
Steven Toth96d84202010-07-31 16:04:59 -0300350 struct saa7164_encoder_fh *fh = file->private_data;
Hans Verkuil225b7832015-08-28 08:48:33 -0300351
352 return saa7164_g_frequency(fh->port, f);
353}
354
355int saa7164_s_frequency(struct saa7164_port *port,
356 const struct v4l2_frequency *f)
357{
Steven Toth7615e432010-07-31 14:44:53 -0300358 struct saa7164_dev *dev = port->dev;
359 struct saa7164_port *tsport;
360 struct dvb_frontend *fe;
361
362 /* TODO: Pull this for the std */
363 struct analog_parameters params = {
364 .mode = V4L2_TUNER_ANALOG_TV,
365 .audmode = V4L2_TUNER_MODE_STEREO,
366 .std = port->encodernorm.id,
367 .frequency = f->frequency
368 };
369
370 /* Stop the encoder */
371 dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
372 f->frequency, f->tuner);
373
374 if (f->tuner != 0)
375 return -EINVAL;
376
Hans Verkuil6b996122015-08-28 08:48:32 -0300377 port->freq = clamp(f->frequency,
378 SAA7164_TV_MIN_FREQ, SAA7164_TV_MAX_FREQ);
Steven Toth7615e432010-07-31 14:44:53 -0300379
380 /* Update the hardware */
381 if (port->nr == SAA7164_PORT_ENC1)
Mauro Carvalho Chehabc7e242ba2010-10-11 17:39:06 -0300382 tsport = &dev->ports[SAA7164_PORT_TS1];
Hans Verkuil225b7832015-08-28 08:48:33 -0300383 else if (port->nr == SAA7164_PORT_ENC2)
Mauro Carvalho Chehabc7e242ba2010-10-11 17:39:06 -0300384 tsport = &dev->ports[SAA7164_PORT_TS2];
Steven Toth7615e432010-07-31 14:44:53 -0300385 else
386 BUG();
387
388 fe = tsport->dvb.frontend;
389
390 if (fe && fe->ops.tuner_ops.set_analog_params)
391 fe->ops.tuner_ops.set_analog_params(fe, &params);
392 else
393 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
394
395 saa7164_encoder_initialize(port);
396
397 return 0;
398}
399
Hans Verkuil225b7832015-08-28 08:48:33 -0300400static int vidioc_s_frequency(struct file *file, void *priv,
401 const struct v4l2_frequency *f)
402{
403 struct saa7164_encoder_fh *fh = file->private_data;
404
405 return saa7164_s_frequency(fh->port, f);
406}
407
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300408static int saa7164_s_ctrl(struct v4l2_ctrl *ctrl)
Steven Toth7615e432010-07-31 14:44:53 -0300409{
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300410 struct saa7164_port *port =
411 container_of(ctrl->handler, struct saa7164_port, ctrl_handler);
Steven Toth7615e432010-07-31 14:44:53 -0300412 struct saa7164_encoder_params *params = &port->encoder_params;
413 int ret = 0;
414
415 switch (ctrl->id) {
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300416 case V4L2_CID_BRIGHTNESS:
417 port->ctl_brightness = ctrl->val;
418 saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
419 break;
420 case V4L2_CID_CONTRAST:
421 port->ctl_contrast = ctrl->val;
422 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
423 break;
424 case V4L2_CID_SATURATION:
425 port->ctl_saturation = ctrl->val;
426 saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
427 break;
428 case V4L2_CID_HUE:
429 port->ctl_hue = ctrl->val;
430 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
431 break;
432 case V4L2_CID_SHARPNESS:
433 port->ctl_sharpness = ctrl->val;
434 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
435 break;
436 case V4L2_CID_AUDIO_VOLUME:
437 port->ctl_volume = ctrl->val;
438 saa7164_api_set_audio_volume(port, port->ctl_volume);
439 break;
Steven Toth7615e432010-07-31 14:44:53 -0300440 case V4L2_CID_MPEG_VIDEO_BITRATE:
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300441 params->bitrate = ctrl->val;
Steven Toth7615e432010-07-31 14:44:53 -0300442 break;
443 case V4L2_CID_MPEG_STREAM_TYPE:
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300444 params->stream_type = ctrl->val;
Steven Toth7615e432010-07-31 14:44:53 -0300445 break;
446 case V4L2_CID_MPEG_AUDIO_MUTE:
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300447 params->ctl_mute = ctrl->val;
Steven Toth7615e432010-07-31 14:44:53 -0300448 ret = saa7164_api_audio_mute(port, params->ctl_mute);
449 if (ret != SAA_OK) {
450 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
451 ret);
452 ret = -EIO;
453 }
454 break;
455 case V4L2_CID_MPEG_VIDEO_ASPECT:
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300456 params->ctl_aspect = ctrl->val;
Steven Toth7615e432010-07-31 14:44:53 -0300457 ret = saa7164_api_set_aspect_ratio(port);
458 if (ret != SAA_OK) {
459 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
460 ret);
461 ret = -EIO;
462 }
463 break;
Steven Toth2600d712010-07-31 14:51:30 -0300464 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300465 params->bitrate_mode = ctrl->val;
Steven Toth2600d712010-07-31 14:51:30 -0300466 break;
Steven Toth3ed43cf2010-07-31 14:58:35 -0300467 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300468 params->refdist = ctrl->val;
Steven Toth3ed43cf2010-07-31 14:58:35 -0300469 break;
Steven Toth968b11b2010-07-31 14:59:38 -0300470 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300471 params->bitrate_peak = ctrl->val;
Steven Toth968b11b2010-07-31 14:59:38 -0300472 break;
Steven Toth5fa56cc2010-07-31 15:05:35 -0300473 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300474 params->gop_size = ctrl->val;
Steven Toth5fa56cc2010-07-31 15:05:35 -0300475 break;
Steven Toth7615e432010-07-31 14:44:53 -0300476 default:
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300477 ret = -EINVAL;
Steven Toth7615e432010-07-31 14:44:53 -0300478 }
479
Steven Toth7615e432010-07-31 14:44:53 -0300480 return ret;
481}
482
Steven Toth7615e432010-07-31 14:44:53 -0300483static int vidioc_querycap(struct file *file, void *priv,
484 struct v4l2_capability *cap)
485{
Steven Toth96d84202010-07-31 16:04:59 -0300486 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300487 struct saa7164_port *port = fh->port;
488 struct saa7164_dev *dev = port->dev;
489
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400490 strscpy(cap->driver, dev->name, sizeof(cap->driver));
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400491 strscpy(cap->card, saa7164_boards[dev->board].name,
Steven Toth7615e432010-07-31 14:44:53 -0300492 sizeof(cap->card));
493 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
494
Hans Verkuil534bc3e2015-03-27 15:17:56 -0300495 cap->device_caps =
Steven Toth7615e432010-07-31 14:44:53 -0300496 V4L2_CAP_VIDEO_CAPTURE |
Hans Verkuil534bc3e2015-03-27 15:17:56 -0300497 V4L2_CAP_READWRITE |
498 V4L2_CAP_TUNER;
Steven Toth7615e432010-07-31 14:44:53 -0300499
Hans Verkuil534bc3e2015-03-27 15:17:56 -0300500 cap->capabilities = cap->device_caps |
501 V4L2_CAP_VBI_CAPTURE |
502 V4L2_CAP_DEVICE_CAPS;
Steven Toth7615e432010-07-31 14:44:53 -0300503
504 return 0;
505}
506
507static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
508 struct v4l2_fmtdesc *f)
509{
510 if (f->index != 0)
511 return -EINVAL;
512
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400513 strscpy(f->description, "MPEG", sizeof(f->description));
Steven Toth7615e432010-07-31 14:44:53 -0300514 f->pixelformat = V4L2_PIX_FMT_MPEG;
515
516 return 0;
517}
518
Hans Verkuil031d2292015-08-28 08:48:30 -0300519static int vidioc_fmt_vid_cap(struct file *file, void *priv,
Steven Toth7615e432010-07-31 14:44:53 -0300520 struct v4l2_format *f)
521{
Steven Toth96d84202010-07-31 16:04:59 -0300522 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300523 struct saa7164_port *port = fh->port;
Steven Toth7615e432010-07-31 14:44:53 -0300524
525 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
526 f->fmt.pix.bytesperline = 0;
Hans Verkuil031d2292015-08-28 08:48:30 -0300527 f->fmt.pix.sizeimage = SAA7164_SIZEIMAGE;
528 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
529 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Steven Toth7615e432010-07-31 14:44:53 -0300530 f->fmt.pix.width = port->width;
531 f->fmt.pix.height = port->height;
Steven Toth7615e432010-07-31 14:44:53 -0300532 return 0;
533}
534
Steven Toth7615e432010-07-31 14:44:53 -0300535static int saa7164_encoder_stop_port(struct saa7164_port *port)
536{
537 struct saa7164_dev *dev = port->dev;
538 int ret;
539
540 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
541 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
542 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
543 __func__, ret);
544 ret = -EIO;
545 } else {
546 dprintk(DBGLVL_ENC, "%s() Stopped\n", __func__);
547 ret = 0;
548 }
549
550 return ret;
551}
552
553static int saa7164_encoder_acquire_port(struct saa7164_port *port)
554{
555 struct saa7164_dev *dev = port->dev;
556 int ret;
557
558 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
559 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
560 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
561 __func__, ret);
562 ret = -EIO;
563 } else {
564 dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
565 ret = 0;
566 }
567
568 return ret;
569}
570
571static int saa7164_encoder_pause_port(struct saa7164_port *port)
572{
573 struct saa7164_dev *dev = port->dev;
574 int ret;
575
576 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
577 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
578 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
579 __func__, ret);
580 ret = -EIO;
581 } else {
582 dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
583 ret = 0;
584 }
585
586 return ret;
587}
588
589/* Firmware is very windows centric, meaning you have to transition
590 * the part through AVStream / KS Windows stages, forwards or backwards.
591 * States are: stopped, acquired (h/w), paused, started.
592 * We have to leave here will all of the soft buffers on the free list,
593 * else the cfg_post() func won't have soft buffers to correctly configure.
594 */
595static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
596{
597 struct saa7164_dev *dev = port->dev;
598 struct saa7164_buffer *buf;
599 struct saa7164_user_buffer *ubuf;
600 struct list_head *c, *n;
601 int ret;
602
603 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
604
605 ret = saa7164_encoder_pause_port(port);
606 ret = saa7164_encoder_acquire_port(port);
607 ret = saa7164_encoder_stop_port(port);
608
609 dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
610 port->nr);
611
Steven Toth1b0e8e42010-07-31 16:01:00 -0300612 /* Reset the state of any allocated buffer resources */
Steven Toth7615e432010-07-31 14:44:53 -0300613 mutex_lock(&port->dmaqueue_lock);
614
615 /* Reset the hard and soft buffer state */
616 list_for_each_safe(c, n, &port->dmaqueue.list) {
617 buf = list_entry(c, struct saa7164_buffer, list);
618 buf->flags = SAA7164_BUFFER_FREE;
619 buf->pos = 0;
620 }
621
622 list_for_each_safe(c, n, &port->list_buf_used.list) {
623 ubuf = list_entry(c, struct saa7164_user_buffer, list);
624 ubuf->pos = 0;
625 list_move_tail(&ubuf->list, &port->list_buf_free.list);
626 }
627
628 mutex_unlock(&port->dmaqueue_lock);
Steven Toth1b0e8e42010-07-31 16:01:00 -0300629
630 /* Free any allocated resources */
631 saa7164_encoder_buffers_dealloc(port);
632
Steven Toth7615e432010-07-31 14:44:53 -0300633 dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
634
635 return ret;
636}
637
638static int saa7164_encoder_start_streaming(struct saa7164_port *port)
639{
640 struct saa7164_dev *dev = port->dev;
641 int result, ret = 0;
642
643 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
644
Steven Toth1b0e8e42010-07-31 16:01:00 -0300645 port->done_first_interrupt = 0;
646
647 /* allocate all of the PCIe DMA buffer resources on the fly,
648 * allowing switching between TS and PS payloads without
649 * requiring a complete driver reload.
650 */
651 saa7164_encoder_buffers_alloc(port);
652
Steven Toth7615e432010-07-31 14:44:53 -0300653 /* Configure the encoder with any cache values */
654 saa7164_api_set_encoder(port);
Steven Toth12d32032010-07-31 15:13:45 -0300655 saa7164_api_get_encoder(port);
Steven Toth7615e432010-07-31 14:44:53 -0300656
Steven Toth1b0e8e42010-07-31 16:01:00 -0300657 /* Place the empty buffers on the hardware */
Steven Toth7615e432010-07-31 14:44:53 -0300658 saa7164_buffer_cfg_port(port);
659
660 /* Acquire the hardware */
661 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
662 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
663 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
664 __func__, result);
665
666 /* Stop the hardware, regardless */
667 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
668 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -0200669 printk(KERN_ERR "%s() acquire/forced stop transition failed, res = 0x%x\n",
670 __func__, result);
Steven Toth7615e432010-07-31 14:44:53 -0300671 }
672 ret = -EIO;
673 goto out;
674 } else
675 dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
676
677 /* Pause the hardware */
678 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
679 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
680 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
681 __func__, result);
682
683 /* Stop the hardware, regardless */
684 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
685 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -0200686 printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n",
687 __func__, result);
Steven Toth7615e432010-07-31 14:44:53 -0300688 }
689
690 ret = -EIO;
691 goto out;
692 } else
693 dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
694
695 /* Start the hardware */
696 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
697 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
698 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
699 __func__, result);
700
701 /* Stop the hardware, regardless */
702 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
703 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -0200704 printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n",
705 __func__, result);
Steven Toth7615e432010-07-31 14:44:53 -0300706 }
707
708 ret = -EIO;
709 } else
710 dprintk(DBGLVL_ENC, "%s() Running\n", __func__);
711
712out:
713 return ret;
714}
715
716static int fops_open(struct file *file)
717{
Steven Toth214ce3f2010-10-06 21:52:22 -0300718 struct saa7164_dev *dev;
719 struct saa7164_port *port;
Steven Toth96d84202010-07-31 16:04:59 -0300720 struct saa7164_encoder_fh *fh;
Steven Toth214ce3f2010-10-06 21:52:22 -0300721
722 port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
723 if (!port)
724 return -ENODEV;
725
726 dev = port->dev;
Steven Toth7615e432010-07-31 14:44:53 -0300727
728 dprintk(DBGLVL_ENC, "%s()\n", __func__);
729
Steven Toth7615e432010-07-31 14:44:53 -0300730 /* allocate + initialize per filehandle data */
731 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Steven Toth214ce3f2010-10-06 21:52:22 -0300732 if (NULL == fh)
Steven Toth7615e432010-07-31 14:44:53 -0300733 return -ENOMEM;
Steven Toth7615e432010-07-31 14:44:53 -0300734
Steven Toth7615e432010-07-31 14:44:53 -0300735 fh->port = port;
Hans Verkuild6d3fe22015-08-28 08:48:27 -0300736 v4l2_fh_init(&fh->fh, video_devdata(file));
737 v4l2_fh_add(&fh->fh);
738 file->private_data = fh;
Steven Toth7615e432010-07-31 14:44:53 -0300739
Steven Toth7615e432010-07-31 14:44:53 -0300740 return 0;
741}
742
743static int fops_release(struct file *file)
744{
Steven Toth96d84202010-07-31 16:04:59 -0300745 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300746 struct saa7164_port *port = fh->port;
747 struct saa7164_dev *dev = port->dev;
748
749 dprintk(DBGLVL_ENC, "%s()\n", __func__);
750
751 /* Shut device down on last close */
752 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
753 if (atomic_dec_return(&port->v4l_reader_count) == 0) {
754 /* stop mpeg capture then cancel buffers */
755 saa7164_encoder_stop_streaming(port);
756 }
757 }
758
Hans Verkuild6d3fe22015-08-28 08:48:27 -0300759 v4l2_fh_del(&fh->fh);
760 v4l2_fh_exit(&fh->fh);
Steven Toth7615e432010-07-31 14:44:53 -0300761 kfree(fh);
762
763 return 0;
764}
765
Mauro Carvalho Chehab5faf7db2012-10-27 13:11:11 -0300766static struct
767saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
Steven Toth7615e432010-07-31 14:44:53 -0300768{
Peter Huewe61ca15002011-01-30 16:33:01 -0300769 struct saa7164_user_buffer *ubuf = NULL;
Steven Toth7615e432010-07-31 14:44:53 -0300770 struct saa7164_dev *dev = port->dev;
Steven Toth12d32032010-07-31 15:13:45 -0300771 u32 crc;
Steven Toth7615e432010-07-31 14:44:53 -0300772
773 mutex_lock(&port->dmaqueue_lock);
774 if (!list_empty(&port->list_buf_used.list)) {
Steven Toth1b0e8e42010-07-31 16:01:00 -0300775 ubuf = list_first_entry(&port->list_buf_used.list,
Steven Toth7615e432010-07-31 14:44:53 -0300776 struct saa7164_user_buffer, list);
Steven Toth12d32032010-07-31 15:13:45 -0300777
Steven Toth1b0e8e42010-07-31 16:01:00 -0300778 if (crc_checking) {
779 crc = crc32(0, ubuf->data, ubuf->actual_size);
780 if (crc != ubuf->crc) {
Steven Tothbc250682010-11-12 18:32:36 -0300781 printk(KERN_ERR
782 "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
783 __func__,
Steven Toth1b0e8e42010-07-31 16:01:00 -0300784 ubuf, ubuf->crc, crc);
785 }
Steven Toth12d32032010-07-31 15:13:45 -0300786 }
787
Steven Toth7615e432010-07-31 14:44:53 -0300788 }
789 mutex_unlock(&port->dmaqueue_lock);
790
Steven Toth1b0e8e42010-07-31 16:01:00 -0300791 dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
Steven Toth7615e432010-07-31 14:44:53 -0300792
Steven Toth1b0e8e42010-07-31 16:01:00 -0300793 return ubuf;
Steven Toth7615e432010-07-31 14:44:53 -0300794}
795
796static ssize_t fops_read(struct file *file, char __user *buffer,
797 size_t count, loff_t *pos)
798{
Steven Toth96d84202010-07-31 16:04:59 -0300799 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300800 struct saa7164_port *port = fh->port;
801 struct saa7164_user_buffer *ubuf = NULL;
802 struct saa7164_dev *dev = port->dev;
Gavin Hurlbutf20a07d2010-09-29 15:18:20 -0300803 int ret = 0;
Steven Toth7615e432010-07-31 14:44:53 -0300804 int rem, cnt;
805 u8 *p;
806
Steven Toth58acca12010-07-31 15:10:52 -0300807 port->last_read_msecs_diff = port->last_read_msecs;
808 port->last_read_msecs = jiffies_to_msecs(jiffies);
809 port->last_read_msecs_diff = port->last_read_msecs -
810 port->last_read_msecs_diff;
811
812 saa7164_histogram_update(&port->read_interval,
813 port->last_read_msecs_diff);
814
Steven Toth46eeb8d2010-07-31 15:11:59 -0300815 if (*pos) {
816 printk(KERN_ERR "%s() ESPIPE\n", __func__);
Steven Toth7615e432010-07-31 14:44:53 -0300817 return -ESPIPE;
Steven Toth46eeb8d2010-07-31 15:11:59 -0300818 }
Steven Toth7615e432010-07-31 14:44:53 -0300819
820 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
821 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
822
Steven Toth46eeb8d2010-07-31 15:11:59 -0300823 if (saa7164_encoder_initialize(port) < 0) {
824 printk(KERN_ERR "%s() EINVAL\n", __func__);
Steven Toth7615e432010-07-31 14:44:53 -0300825 return -EINVAL;
Steven Toth46eeb8d2010-07-31 15:11:59 -0300826 }
Steven Toth7615e432010-07-31 14:44:53 -0300827
828 saa7164_encoder_start_streaming(port);
829 msleep(200);
830 }
831 }
832
833 /* blocking wait for buffer */
834 if ((file->f_flags & O_NONBLOCK) == 0) {
835 if (wait_event_interruptible(port->wait_read,
836 saa7164_enc_next_buf(port))) {
Steven Toth46eeb8d2010-07-31 15:11:59 -0300837 printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
Steven Toth7615e432010-07-31 14:44:53 -0300838 return -ERESTARTSYS;
839 }
840 }
841
842 /* Pull the first buffer from the used list */
843 ubuf = saa7164_enc_next_buf(port);
844
845 while ((count > 0) && ubuf) {
846
847 /* set remaining bytes to copy */
848 rem = ubuf->actual_size - ubuf->pos;
849 cnt = rem > count ? count : rem;
850
851 p = ubuf->data + ubuf->pos;
852
853 dprintk(DBGLVL_ENC,
854 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
855 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
856
857 if (copy_to_user(buffer, p, cnt)) {
858 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
Steven Toth46eeb8d2010-07-31 15:11:59 -0300859 if (!ret) {
860 printk(KERN_ERR "%s() EFAULT\n", __func__);
Steven Toth7615e432010-07-31 14:44:53 -0300861 ret = -EFAULT;
Steven Toth46eeb8d2010-07-31 15:11:59 -0300862 }
Steven Toth7615e432010-07-31 14:44:53 -0300863 goto err;
864 }
865
866 ubuf->pos += cnt;
867 count -= cnt;
868 buffer += cnt;
869 ret += cnt;
870
Steven Tothbc250682010-11-12 18:32:36 -0300871 if (ubuf->pos > ubuf->actual_size)
Steven Toth46eeb8d2010-07-31 15:11:59 -0300872 printk(KERN_ERR "read() pos > actual, huh?\n");
Steven Toth46eeb8d2010-07-31 15:11:59 -0300873
Steven Toth7615e432010-07-31 14:44:53 -0300874 if (ubuf->pos == ubuf->actual_size) {
875
876 /* finished with current buffer, take next buffer */
877
878 /* Requeue the buffer on the free list */
879 ubuf->pos = 0;
880
881 mutex_lock(&port->dmaqueue_lock);
882 list_move_tail(&ubuf->list, &port->list_buf_free.list);
883 mutex_unlock(&port->dmaqueue_lock);
884
885 /* Dequeue next */
886 if ((file->f_flags & O_NONBLOCK) == 0) {
887 if (wait_event_interruptible(port->wait_read,
888 saa7164_enc_next_buf(port))) {
889 break;
890 }
891 }
892 ubuf = saa7164_enc_next_buf(port);
893 }
894 }
895err:
Steven Tothbc250682010-11-12 18:32:36 -0300896 if (!ret && !ubuf)
Steven Toth7615e432010-07-31 14:44:53 -0300897 ret = -EAGAIN;
898
899 return ret;
900}
901
Al Viroc23e0cb2017-07-03 03:02:56 -0400902static __poll_t fops_poll(struct file *file, poll_table *wait)
Steven Toth7615e432010-07-31 14:44:53 -0300903{
Al Viro01699432017-07-03 03:14:15 -0400904 __poll_t req_events = poll_requested_events(wait);
Steven Tothbc250682010-11-12 18:32:36 -0300905 struct saa7164_encoder_fh *fh =
906 (struct saa7164_encoder_fh *)file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300907 struct saa7164_port *port = fh->port;
Al Viroc23e0cb2017-07-03 03:02:56 -0400908 __poll_t mask = v4l2_ctrl_poll(file, wait);
Steven Toth7615e432010-07-31 14:44:53 -0300909
Steven Toth58acca12010-07-31 15:10:52 -0300910 port->last_poll_msecs_diff = port->last_poll_msecs;
911 port->last_poll_msecs = jiffies_to_msecs(jiffies);
912 port->last_poll_msecs_diff = port->last_poll_msecs -
913 port->last_poll_msecs_diff;
914
915 saa7164_histogram_update(&port->poll_interval,
916 port->last_poll_msecs_diff);
917
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800918 if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
Hans Verkuil45053ed2015-08-28 08:48:28 -0300919 return mask;
Steven Toth7615e432010-07-31 14:44:53 -0300920
921 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
922 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
923 if (saa7164_encoder_initialize(port) < 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800924 return mask | EPOLLERR;
Steven Toth7615e432010-07-31 14:44:53 -0300925 saa7164_encoder_start_streaming(port);
926 msleep(200);
927 }
928 }
929
Steven Toth7615e432010-07-31 14:44:53 -0300930 /* Pull the first buffer from the used list */
Dan Carpenter061d55e2010-11-24 02:36:57 -0300931 if (!list_empty(&port->list_buf_used.list))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800932 mask |= EPOLLIN | EPOLLRDNORM;
Steven Toth7615e432010-07-31 14:44:53 -0300933
934 return mask;
935}
936
Hans Verkuil1a708ea2015-08-28 08:48:26 -0300937static const struct v4l2_ctrl_ops saa7164_ctrl_ops = {
938 .s_ctrl = saa7164_s_ctrl,
939};
940
Steven Toth7615e432010-07-31 14:44:53 -0300941static const struct v4l2_file_operations mpeg_fops = {
942 .owner = THIS_MODULE,
943 .open = fops_open,
944 .release = fops_release,
945 .read = fops_read,
946 .poll = fops_poll,
947 .unlocked_ioctl = video_ioctl2,
948};
949
Steven Toth7615e432010-07-31 14:44:53 -0300950static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
951 .vidioc_s_std = vidioc_s_std,
Hans Verkuil8d2d41e2013-06-03 05:36:45 -0300952 .vidioc_g_std = vidioc_g_std,
Hans Verkuil225b7832015-08-28 08:48:33 -0300953 .vidioc_enum_input = saa7164_enum_input,
Steven Toth7615e432010-07-31 14:44:53 -0300954 .vidioc_g_input = vidioc_g_input,
955 .vidioc_s_input = vidioc_s_input,
Hans Verkuil225b7832015-08-28 08:48:33 -0300956 .vidioc_g_tuner = saa7164_g_tuner,
957 .vidioc_s_tuner = saa7164_s_tuner,
Steven Toth7615e432010-07-31 14:44:53 -0300958 .vidioc_g_frequency = vidioc_g_frequency,
959 .vidioc_s_frequency = vidioc_s_frequency,
Steven Toth7615e432010-07-31 14:44:53 -0300960 .vidioc_querycap = vidioc_querycap,
961 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
Hans Verkuil031d2292015-08-28 08:48:30 -0300962 .vidioc_g_fmt_vid_cap = vidioc_fmt_vid_cap,
963 .vidioc_try_fmt_vid_cap = vidioc_fmt_vid_cap,
964 .vidioc_s_fmt_vid_cap = vidioc_fmt_vid_cap,
Hans Verkuil245b5ae2015-08-28 08:48:29 -0300965 .vidioc_log_status = v4l2_ctrl_log_status,
966 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
967 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Steven Toth7615e432010-07-31 14:44:53 -0300968};
969
970static struct video_device saa7164_mpeg_template = {
971 .name = "saa7164",
972 .fops = &mpeg_fops,
973 .ioctl_ops = &mpeg_ioctl_ops,
974 .minor = -1,
975 .tvnorms = SAA7164_NORMS,
Steven Toth7615e432010-07-31 14:44:53 -0300976};
977
978static struct video_device *saa7164_encoder_alloc(
979 struct saa7164_port *port,
980 struct pci_dev *pci,
981 struct video_device *template,
982 char *type)
983{
984 struct video_device *vfd;
985 struct saa7164_dev *dev = port->dev;
986
987 dprintk(DBGLVL_ENC, "%s()\n", __func__);
988
989 vfd = video_device_alloc();
990 if (NULL == vfd)
991 return NULL;
992
993 *vfd = *template;
994 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
995 type, saa7164_boards[dev->board].name);
996
Hans Verkuild66de792013-06-12 05:49:50 -0300997 vfd->v4l2_dev = &dev->v4l2_dev;
Steven Toth7615e432010-07-31 14:44:53 -0300998 vfd->release = video_device_release;
999 return vfd;
1000}
1001
1002int saa7164_encoder_register(struct saa7164_port *port)
1003{
1004 struct saa7164_dev *dev = port->dev;
Hans Verkuil1a708ea2015-08-28 08:48:26 -03001005 struct v4l2_ctrl_handler *hdl = &port->ctrl_handler;
Steven Toth1b0e8e42010-07-31 16:01:00 -03001006 int result = -ENODEV;
Steven Toth7615e432010-07-31 14:44:53 -03001007
1008 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1009
Amitoj Kaur Chawla2aefee02016-05-28 13:43:39 -03001010 BUG_ON(port->type != SAA7164_MPEG_ENCODER);
Steven Toth7615e432010-07-31 14:44:53 -03001011
1012 /* Sanity check that the PCI configuration space is active */
1013 if (port->hwcfg.BARLocation == 0) {
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -02001014 printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n",
Steven Toth7615e432010-07-31 14:44:53 -03001015 __func__, result);
1016 result = -ENOMEM;
1017 goto failed;
1018 }
1019
Steven Toth7615e432010-07-31 14:44:53 -03001020 /* Establish encoder defaults here */
1021 /* Set default TV standard */
1022 port->encodernorm = saa7164_tvnorms[0];
1023 port->width = 720;
1024 port->mux_input = 1; /* Composite */
Steven Toth7615e432010-07-31 14:44:53 -03001025 port->video_format = EU_VIDEO_FORMAT_MPEG_2;
1026 port->audio_format = 0;
1027 port->video_resolution = 0;
Hans Verkuil6b996122015-08-28 08:48:32 -03001028 port->freq = SAA7164_TV_MIN_FREQ;
Hans Verkuil1a708ea2015-08-28 08:48:26 -03001029
1030 v4l2_ctrl_handler_init(hdl, 14);
1031 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1032 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1033 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1034 V4L2_CID_CONTRAST, 0, 255, 1, 66);
1035 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1036 V4L2_CID_SATURATION, 0, 255, 1, 62);
1037 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1038 V4L2_CID_HUE, 0, 255, 1, 128);
1039 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1040 V4L2_CID_SHARPNESS, 0x0, 0x0f, 1, 8);
1041 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1042 V4L2_CID_MPEG_AUDIO_MUTE, 0x0, 0x01, 1, 0);
1043 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1044 V4L2_CID_AUDIO_VOLUME, -83, 24, 1, 20);
1045 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1046 V4L2_CID_MPEG_VIDEO_BITRATE,
1047 ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
1048 100000, ENCODER_DEF_BITRATE);
1049 v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
1050 V4L2_CID_MPEG_STREAM_TYPE,
1051 V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 0,
1052 V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
1053 v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
1054 V4L2_CID_MPEG_VIDEO_ASPECT,
1055 V4L2_MPEG_VIDEO_ASPECT_221x100, 0,
1056 V4L2_MPEG_VIDEO_ASPECT_4x3);
1057 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1058 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, 15);
1059 v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
1060 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1061 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
1062 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
1063 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1064 V4L2_CID_MPEG_VIDEO_B_FRAMES, 1, 3, 1, 1);
1065 v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1066 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1067 ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
1068 100000, ENCODER_DEF_BITRATE);
1069 if (hdl->error) {
1070 result = hdl->error;
1071 goto failed;
1072 }
1073
Hans Verkuil8d2d41e2013-06-03 05:36:45 -03001074 port->std = V4L2_STD_NTSC_M;
Steven Toth7615e432010-07-31 14:44:53 -03001075
1076 if (port->encodernorm.id & V4L2_STD_525_60)
1077 port->height = 480;
1078 else
1079 port->height = 576;
1080
1081 /* Allocate and register the video device node */
1082 port->v4l_device = saa7164_encoder_alloc(port,
1083 dev->pci, &saa7164_mpeg_template, "mpeg");
1084
Peter Huewe61ca15002011-01-30 16:33:01 -03001085 if (!port->v4l_device) {
Steven Toth7615e432010-07-31 14:44:53 -03001086 printk(KERN_INFO "%s: can't allocate mpeg device\n",
1087 dev->name);
1088 result = -ENOMEM;
1089 goto failed;
1090 }
1091
Hans Verkuil1a708ea2015-08-28 08:48:26 -03001092 port->v4l_device->ctrl_handler = hdl;
1093 v4l2_ctrl_handler_setup(hdl);
Steven Toth214ce3f2010-10-06 21:52:22 -03001094 video_set_drvdata(port->v4l_device, port);
Steven Toth7615e432010-07-31 14:44:53 -03001095 result = video_register_device(port->v4l_device,
1096 VFL_TYPE_GRABBER, -1);
1097 if (result < 0) {
1098 printk(KERN_INFO "%s: can't register mpeg device\n",
1099 dev->name);
1100 /* TODO: We're going to leak here if we don't dealloc
1101 The buffers above. The unreg function can't deal wit it.
1102 */
1103 goto failed;
1104 }
1105
1106 printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
1107 dev->name, port->v4l_device->num);
1108
1109 /* Configure the hardware defaults */
1110 saa7164_api_set_videomux(port);
1111 saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
1112 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
1113 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
1114 saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
1115 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
1116 saa7164_api_audio_mute(port, 0);
1117 saa7164_api_set_audio_volume(port, 20);
1118 saa7164_api_set_aspect_ratio(port);
1119
1120 /* Disable audio standard detection, it's buggy */
1121 saa7164_api_set_audio_detection(port, 0);
1122
1123 saa7164_api_set_encoder(port);
1124 saa7164_api_get_encoder(port);
1125
1126 result = 0;
1127failed:
1128 return result;
1129}
1130
1131void saa7164_encoder_unregister(struct saa7164_port *port)
1132{
1133 struct saa7164_dev *dev = port->dev;
Steven Toth7615e432010-07-31 14:44:53 -03001134
1135 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1136
Amitoj Kaur Chawla2aefee02016-05-28 13:43:39 -03001137 BUG_ON(port->type != SAA7164_MPEG_ENCODER);
Steven Toth7615e432010-07-31 14:44:53 -03001138
1139 if (port->v4l_device) {
1140 if (port->v4l_device->minor != -1)
1141 video_unregister_device(port->v4l_device);
1142 else
1143 video_device_release(port->v4l_device);
1144
1145 port->v4l_device = NULL;
1146 }
Hans Verkuil1a708ea2015-08-28 08:48:26 -03001147 v4l2_ctrl_handler_free(&port->ctrl_handler);
Steven Toth7615e432010-07-31 14:44:53 -03001148
Steven Toth7615e432010-07-31 14:44:53 -03001149 dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
1150}
1151