Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
9b8b0199 ST |
2 | /* |
3 | * Driver for the NXP SAA7164 PCIe bridge | |
4 | * | |
63a412ec | 5 | * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com> |
9b8b0199 ST |
6 | */ |
7 | ||
8 | #include "saa7164.h" | |
9 | ||
7615e434 ST |
10 | #define ENCODER_MAX_BITRATE 6500000 |
11 | #define ENCODER_MIN_BITRATE 1000000 | |
12 | #define ENCODER_DEF_BITRATE 5000000 | |
13 | ||
031d2297 HV |
14 | /* |
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 | ||
7615e434 ST |
26 | static 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 | ||
7615e434 ST |
36 | /* Take the encoder configuration form the port struct and |
37 | * flush it to the hardware. | |
38 | */ | |
39 | static 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 | ||
1b0e8e46 ST |
59 | static 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 */ | |
97 | static int saa7164_encoder_buffers_alloc(struct saa7164_port *port) | |
7615e434 ST |
98 | { |
99 | struct saa7164_dev *dev = port->dev; | |
1b0e8e46 ST |
100 | struct saa7164_buffer *buf; |
101 | struct saa7164_user_buffer *ubuf; | |
4d270cfb | 102 | struct tmHWStreamParameters *params = &port->hw_streamingparams; |
1b0e8e46 ST |
103 | int result = -ENODEV, i; |
104 | int len = 0; | |
7615e434 ST |
105 | |
106 | dprintk(DBGLVL_ENC, "%s()\n", __func__); | |
107 | ||
bc250684 ST |
108 | 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__); | |
1b0e8e46 ST |
113 | 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 | |
bc250684 ST |
119 | 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__); | |
1b0e8e46 ST |
124 | 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(); | |
131 | ||
132 | /* Init and establish defaults */ | |
133 | params->bitspersample = 8; | |
134 | params->linethreshold = 0; | |
61ca1500 PH |
135 | params->pagetablelistvirt = NULL; |
136 | params->pagetablelistphys = NULL; | |
1b0e8e46 ST |
137 | 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) { | |
24f711c1 | 146 | printk(KERN_ERR "%s() failed (errno = %d), unable to allocate buffer\n", |
1b0e8e46 ST |
147 | __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 | ||
70f23fd6 | 159 | /* Allocate some kernel buffers for copying |
1b0e8e46 ST |
160 | * 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; | |
7615e434 | 181 | |
1b0e8e46 ST |
182 | failed: |
183 | return result; | |
184 | } | |
185 | ||
186 | static int saa7164_encoder_initialize(struct saa7164_port *port) | |
187 | { | |
188 | saa7164_encoder_configure(port); | |
7615e434 ST |
189 | return 0; |
190 | } | |
191 | ||
192 | /* -- V4L2 --------------------------------------------------------- */ | |
225b783b | 193 | int saa7164_s_std(struct saa7164_port *port, v4l2_std_id id) |
7615e434 | 194 | { |
7615e434 ST |
195 | struct saa7164_dev *dev = port->dev; |
196 | unsigned int i; | |
197 | ||
314527ac | 198 | dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)id); |
7615e434 ST |
199 | |
200 | for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) { | |
314527ac | 201 | if (id & saa7164_tvnorms[i].id) |
7615e434 ST |
202 | break; |
203 | } | |
204 | if (i == ARRAY_SIZE(saa7164_tvnorms)) | |
205 | return -EINVAL; | |
206 | ||
207 | port->encodernorm = saa7164_tvnorms[i]; | |
8d2d41e9 | 208 | port->std = id; |
7615e434 ST |
209 | |
210 | /* Update the audio decoder while is not running in | |
211 | * auto detect mode. | |
212 | */ | |
213 | saa7164_api_set_audio_std(port); | |
214 | ||
314527ac | 215 | dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)id); |
7615e434 ST |
216 | |
217 | return 0; | |
218 | } | |
219 | ||
225b783b | 220 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id) |
8d2d41e9 HV |
221 | { |
222 | struct saa7164_encoder_fh *fh = file->private_data; | |
8d2d41e9 | 223 | |
225b783b HV |
224 | return saa7164_s_std(fh->port, id); |
225 | } | |
226 | ||
227 | int saa7164_g_std(struct saa7164_port *port, v4l2_std_id *id) | |
228 | { | |
8d2d41e9 HV |
229 | *id = port->std; |
230 | return 0; | |
231 | } | |
232 | ||
225b783b HV |
233 | static 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 | ||
240 | int saa7164_enum_input(struct file *file, void *priv, struct v4l2_input *i) | |
7615e434 | 241 | { |
6b996126 HV |
242 | static const char * const inputs[] = { |
243 | "tuner", "composite", "svideo", "aux", | |
244 | "composite 2", "svideo 2", "aux 2" | |
245 | }; | |
7615e434 ST |
246 | int n; |
247 | ||
7615e434 ST |
248 | if (i->index >= 7) |
249 | return -EINVAL; | |
250 | ||
cc1e6315 | 251 | strscpy(i->name, inputs[i->index], sizeof(i->name)); |
7615e434 ST |
252 | |
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 | ||
225b783b | 264 | int saa7164_g_input(struct saa7164_port *port, unsigned int *i) |
7615e434 | 265 | { |
7615e434 ST |
266 | 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 | ||
225b783b | 278 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
7615e434 | 279 | { |
96d8420d | 280 | struct saa7164_encoder_fh *fh = file->private_data; |
225b783b HV |
281 | |
282 | return saa7164_g_input(fh->port, i); | |
283 | } | |
284 | ||
285 | int saa7164_s_input(struct saa7164_port *port, unsigned int i) | |
286 | { | |
7615e434 ST |
287 | 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 | ||
225b783b HV |
302 | static 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 | ||
309 | int saa7164_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t) | |
7615e434 | 310 | { |
96d8420d | 311 | struct saa7164_encoder_fh *fh = file->private_data; |
7615e434 ST |
312 | struct saa7164_port *port = fh->port; |
313 | struct saa7164_dev *dev = port->dev; | |
314 | ||
315 | if (0 != t->index) | |
316 | return -EINVAL; | |
317 | ||
cc1e6315 | 318 | strscpy(t->name, "tuner", sizeof(t->name)); |
7615e434 | 319 | t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO; |
6b996126 HV |
320 | t->rangelow = SAA7164_TV_MIN_FREQ; |
321 | t->rangehigh = SAA7164_TV_MAX_FREQ; | |
7615e434 ST |
322 | |
323 | dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type); | |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
225b783b HV |
328 | int saa7164_s_tuner(struct file *file, void *priv, |
329 | const struct v4l2_tuner *t) | |
7615e434 | 330 | { |
6b996126 HV |
331 | if (0 != t->index) |
332 | return -EINVAL; | |
333 | ||
7615e434 | 334 | /* Update the A/V core */ |
7615e434 ST |
335 | return 0; |
336 | } | |
337 | ||
225b783b | 338 | int saa7164_g_frequency(struct saa7164_port *port, struct v4l2_frequency *f) |
7615e434 | 339 | { |
6b996126 HV |
340 | if (f->tuner) |
341 | return -EINVAL; | |
342 | ||
7615e434 | 343 | f->frequency = port->freq; |
7615e434 ST |
344 | return 0; |
345 | } | |
346 | ||
225b783b HV |
347 | static int vidioc_g_frequency(struct file *file, void *priv, |
348 | struct v4l2_frequency *f) | |
7615e434 | 349 | { |
96d8420d | 350 | struct saa7164_encoder_fh *fh = file->private_data; |
225b783b HV |
351 | |
352 | return saa7164_g_frequency(fh->port, f); | |
353 | } | |
354 | ||
355 | int saa7164_s_frequency(struct saa7164_port *port, | |
356 | const struct v4l2_frequency *f) | |
357 | { | |
7615e434 ST |
358 | 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 | ||
6b996126 HV |
377 | port->freq = clamp(f->frequency, |
378 | SAA7164_TV_MIN_FREQ, SAA7164_TV_MAX_FREQ); | |
7615e434 ST |
379 | |
380 | /* Update the hardware */ | |
381 | if (port->nr == SAA7164_PORT_ENC1) | |
c7e242ba | 382 | tsport = &dev->ports[SAA7164_PORT_TS1]; |
225b783b | 383 | else if (port->nr == SAA7164_PORT_ENC2) |
c7e242ba | 384 | tsport = &dev->ports[SAA7164_PORT_TS2]; |
7615e434 ST |
385 | 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, ¶ms); | |
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 | ||
225b783b HV |
400 | static 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 | ||
1a708ea0 | 408 | static int saa7164_s_ctrl(struct v4l2_ctrl *ctrl) |
7615e434 | 409 | { |
1a708ea0 HV |
410 | struct saa7164_port *port = |
411 | container_of(ctrl->handler, struct saa7164_port, ctrl_handler); | |
412 | struct saa7164_encoder_params *params = &port->encoder_params; | |
7615e434 ST |
413 | int ret = 0; |
414 | ||
1a708ea0 | 415 | switch (ctrl->id) { |
7615e434 | 416 | case V4L2_CID_BRIGHTNESS: |
1a708ea0 HV |
417 | port->ctl_brightness = ctrl->val; |
418 | saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL); | |
7615e434 ST |
419 | break; |
420 | case V4L2_CID_CONTRAST: | |
1a708ea0 HV |
421 | port->ctl_contrast = ctrl->val; |
422 | saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL); | |
7615e434 ST |
423 | break; |
424 | case V4L2_CID_SATURATION: | |
1a708ea0 HV |
425 | port->ctl_saturation = ctrl->val; |
426 | saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL); | |
7615e434 ST |
427 | break; |
428 | case V4L2_CID_HUE: | |
1a708ea0 HV |
429 | port->ctl_hue = ctrl->val; |
430 | saa7164_api_set_usercontrol(port, PU_HUE_CONTROL); | |
7615e434 ST |
431 | break; |
432 | case V4L2_CID_SHARPNESS: | |
1a708ea0 HV |
433 | port->ctl_sharpness = ctrl->val; |
434 | saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL); | |
7615e434 ST |
435 | break; |
436 | case V4L2_CID_AUDIO_VOLUME: | |
1a708ea0 HV |
437 | port->ctl_volume = ctrl->val; |
438 | saa7164_api_set_audio_volume(port, port->ctl_volume); | |
7615e434 | 439 | break; |
7615e434 | 440 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
1a708ea0 | 441 | params->bitrate = ctrl->val; |
7615e434 ST |
442 | break; |
443 | case V4L2_CID_MPEG_STREAM_TYPE: | |
1a708ea0 | 444 | params->stream_type = ctrl->val; |
7615e434 ST |
445 | break; |
446 | case V4L2_CID_MPEG_AUDIO_MUTE: | |
1a708ea0 | 447 | params->ctl_mute = ctrl->val; |
7615e434 ST |
448 | 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: | |
1a708ea0 | 456 | params->ctl_aspect = ctrl->val; |
7615e434 ST |
457 | 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; | |
2600d71c | 464 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: |
1a708ea0 | 465 | params->bitrate_mode = ctrl->val; |
2600d71c | 466 | break; |
3ed43cf9 | 467 | case V4L2_CID_MPEG_VIDEO_B_FRAMES: |
1a708ea0 | 468 | params->refdist = ctrl->val; |
3ed43cf9 | 469 | break; |
968b11b2 | 470 | case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: |
1a708ea0 | 471 | params->bitrate_peak = ctrl->val; |
968b11b2 | 472 | break; |
5fa56ccd | 473 | case V4L2_CID_MPEG_VIDEO_GOP_SIZE: |
1a708ea0 | 474 | params->gop_size = ctrl->val; |
5fa56ccd | 475 | break; |
7615e434 | 476 | default: |
1a708ea0 | 477 | ret = -EINVAL; |
7615e434 ST |
478 | } |
479 | ||
7615e434 ST |
480 | return ret; |
481 | } | |
482 | ||
7615e434 ST |
483 | static int vidioc_querycap(struct file *file, void *priv, |
484 | struct v4l2_capability *cap) | |
485 | { | |
96d8420d | 486 | struct saa7164_encoder_fh *fh = file->private_data; |
7615e434 ST |
487 | struct saa7164_port *port = fh->port; |
488 | struct saa7164_dev *dev = port->dev; | |
489 | ||
cc1e6315 | 490 | strscpy(cap->driver, dev->name, sizeof(cap->driver)); |
c0decac1 | 491 | strscpy(cap->card, saa7164_boards[dev->board].name, |
7615e434 ST |
492 | sizeof(cap->card)); |
493 | sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci)); | |
21615365 HV |
494 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | |
495 | V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | | |
496 | V4L2_CAP_DEVICE_CAPS; | |
7615e434 ST |
497 | return 0; |
498 | } | |
499 | ||
500 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, | |
501 | struct v4l2_fmtdesc *f) | |
502 | { | |
503 | if (f->index != 0) | |
504 | return -EINVAL; | |
505 | ||
7615e434 ST |
506 | f->pixelformat = V4L2_PIX_FMT_MPEG; |
507 | ||
508 | return 0; | |
509 | } | |
510 | ||
031d2297 | 511 | static int vidioc_fmt_vid_cap(struct file *file, void *priv, |
7615e434 ST |
512 | struct v4l2_format *f) |
513 | { | |
96d8420d | 514 | struct saa7164_encoder_fh *fh = file->private_data; |
7615e434 | 515 | struct saa7164_port *port = fh->port; |
7615e434 ST |
516 | |
517 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; | |
518 | f->fmt.pix.bytesperline = 0; | |
031d2297 HV |
519 | f->fmt.pix.sizeimage = SAA7164_SIZEIMAGE; |
520 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
521 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
7615e434 ST |
522 | f->fmt.pix.width = port->width; |
523 | f->fmt.pix.height = port->height; | |
7615e434 ST |
524 | return 0; |
525 | } | |
526 | ||
7615e434 ST |
527 | static int saa7164_encoder_stop_port(struct saa7164_port *port) |
528 | { | |
529 | struct saa7164_dev *dev = port->dev; | |
530 | int ret; | |
531 | ||
532 | ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); | |
533 | if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { | |
534 | printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n", | |
535 | __func__, ret); | |
536 | ret = -EIO; | |
537 | } else { | |
538 | dprintk(DBGLVL_ENC, "%s() Stopped\n", __func__); | |
539 | ret = 0; | |
540 | } | |
541 | ||
542 | return ret; | |
543 | } | |
544 | ||
545 | static int saa7164_encoder_acquire_port(struct saa7164_port *port) | |
546 | { | |
547 | struct saa7164_dev *dev = port->dev; | |
548 | int ret; | |
549 | ||
550 | ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE); | |
551 | if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { | |
552 | printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n", | |
553 | __func__, ret); | |
554 | ret = -EIO; | |
555 | } else { | |
556 | dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__); | |
557 | ret = 0; | |
558 | } | |
559 | ||
560 | return ret; | |
561 | } | |
562 | ||
563 | static int saa7164_encoder_pause_port(struct saa7164_port *port) | |
564 | { | |
565 | struct saa7164_dev *dev = port->dev; | |
566 | int ret; | |
567 | ||
568 | ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE); | |
569 | if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { | |
570 | printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n", | |
571 | __func__, ret); | |
572 | ret = -EIO; | |
573 | } else { | |
574 | dprintk(DBGLVL_ENC, "%s() Paused\n", __func__); | |
575 | ret = 0; | |
576 | } | |
577 | ||
578 | return ret; | |
579 | } | |
580 | ||
581 | /* Firmware is very windows centric, meaning you have to transition | |
582 | * the part through AVStream / KS Windows stages, forwards or backwards. | |
583 | * States are: stopped, acquired (h/w), paused, started. | |
584 | * We have to leave here will all of the soft buffers on the free list, | |
585 | * else the cfg_post() func won't have soft buffers to correctly configure. | |
586 | */ | |
587 | static int saa7164_encoder_stop_streaming(struct saa7164_port *port) | |
588 | { | |
589 | struct saa7164_dev *dev = port->dev; | |
590 | struct saa7164_buffer *buf; | |
591 | struct saa7164_user_buffer *ubuf; | |
592 | struct list_head *c, *n; | |
593 | int ret; | |
594 | ||
595 | dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); | |
596 | ||
597 | ret = saa7164_encoder_pause_port(port); | |
598 | ret = saa7164_encoder_acquire_port(port); | |
599 | ret = saa7164_encoder_stop_port(port); | |
600 | ||
601 | dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__, | |
602 | port->nr); | |
603 | ||
1b0e8e46 | 604 | /* Reset the state of any allocated buffer resources */ |
7615e434 ST |
605 | mutex_lock(&port->dmaqueue_lock); |
606 | ||
607 | /* Reset the hard and soft buffer state */ | |
608 | list_for_each_safe(c, n, &port->dmaqueue.list) { | |
609 | buf = list_entry(c, struct saa7164_buffer, list); | |
610 | buf->flags = SAA7164_BUFFER_FREE; | |
611 | buf->pos = 0; | |
612 | } | |
613 | ||
614 | list_for_each_safe(c, n, &port->list_buf_used.list) { | |
615 | ubuf = list_entry(c, struct saa7164_user_buffer, list); | |
616 | ubuf->pos = 0; | |
617 | list_move_tail(&ubuf->list, &port->list_buf_free.list); | |
618 | } | |
619 | ||
620 | mutex_unlock(&port->dmaqueue_lock); | |
1b0e8e46 ST |
621 | |
622 | /* Free any allocated resources */ | |
623 | saa7164_encoder_buffers_dealloc(port); | |
624 | ||
7615e434 ST |
625 | dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr); |
626 | ||
627 | return ret; | |
628 | } | |
629 | ||
630 | static int saa7164_encoder_start_streaming(struct saa7164_port *port) | |
631 | { | |
632 | struct saa7164_dev *dev = port->dev; | |
633 | int result, ret = 0; | |
634 | ||
635 | dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); | |
636 | ||
1b0e8e46 ST |
637 | port->done_first_interrupt = 0; |
638 | ||
639 | /* allocate all of the PCIe DMA buffer resources on the fly, | |
640 | * allowing switching between TS and PS payloads without | |
641 | * requiring a complete driver reload. | |
642 | */ | |
643 | saa7164_encoder_buffers_alloc(port); | |
644 | ||
7615e434 ST |
645 | /* Configure the encoder with any cache values */ |
646 | saa7164_api_set_encoder(port); | |
12d3203e | 647 | saa7164_api_get_encoder(port); |
7615e434 | 648 | |
1b0e8e46 | 649 | /* Place the empty buffers on the hardware */ |
7615e434 ST |
650 | saa7164_buffer_cfg_port(port); |
651 | ||
652 | /* Acquire the hardware */ | |
653 | result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE); | |
654 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { | |
655 | printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n", | |
656 | __func__, result); | |
657 | ||
658 | /* Stop the hardware, regardless */ | |
659 | result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); | |
660 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { | |
24f711c1 MCC |
661 | printk(KERN_ERR "%s() acquire/forced stop transition failed, res = 0x%x\n", |
662 | __func__, result); | |
7615e434 ST |
663 | } |
664 | ret = -EIO; | |
665 | goto out; | |
666 | } else | |
667 | dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__); | |
668 | ||
669 | /* Pause the hardware */ | |
670 | result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE); | |
671 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { | |
672 | printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n", | |
673 | __func__, result); | |
674 | ||
675 | /* Stop the hardware, regardless */ | |
676 | result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); | |
677 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { | |
24f711c1 MCC |
678 | printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n", |
679 | __func__, result); | |
7615e434 ST |
680 | } |
681 | ||
682 | ret = -EIO; | |
683 | goto out; | |
684 | } else | |
685 | dprintk(DBGLVL_ENC, "%s() Paused\n", __func__); | |
686 | ||
687 | /* Start the hardware */ | |
688 | result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN); | |
689 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { | |
690 | printk(KERN_ERR "%s() run transition failed, result = 0x%x\n", | |
691 | __func__, result); | |
692 | ||
693 | /* Stop the hardware, regardless */ | |
694 | result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); | |
695 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { | |
24f711c1 MCC |
696 | printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n", |
697 | __func__, result); | |
7615e434 ST |
698 | } |
699 | ||
700 | ret = -EIO; | |
701 | } else | |
702 | dprintk(DBGLVL_ENC, "%s() Running\n", __func__); | |
703 | ||
704 | out: | |
705 | return ret; | |
706 | } | |
707 | ||
708 | static int fops_open(struct file *file) | |
709 | { | |
214ce3fa ST |
710 | struct saa7164_dev *dev; |
711 | struct saa7164_port *port; | |
96d8420d | 712 | struct saa7164_encoder_fh *fh; |
7615e434 | 713 | |
214ce3fa ST |
714 | port = (struct saa7164_port *)video_get_drvdata(video_devdata(file)); |
715 | if (!port) | |
716 | return -ENODEV; | |
7615e434 | 717 | |
214ce3fa | 718 | dev = port->dev; |
7615e434 | 719 | |
214ce3fa | 720 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
7615e434 ST |
721 | |
722 | /* allocate + initialize per filehandle data */ | |
723 | fh = kzalloc(sizeof(*fh), GFP_KERNEL); | |
214ce3fa | 724 | if (NULL == fh) |
7615e434 | 725 | return -ENOMEM; |
7615e434 | 726 | |
7615e434 | 727 | fh->port = port; |
d6d3fe2f HV |
728 | v4l2_fh_init(&fh->fh, video_devdata(file)); |
729 | v4l2_fh_add(&fh->fh); | |
730 | file->private_data = fh; | |
7615e434 | 731 | |
7615e434 ST |
732 | return 0; |
733 | } | |
734 | ||
735 | static int fops_release(struct file *file) | |
736 | { | |
96d8420d | 737 | struct saa7164_encoder_fh *fh = file->private_data; |
7615e434 ST |
738 | struct saa7164_port *port = fh->port; |
739 | struct saa7164_dev *dev = port->dev; | |
740 | ||
741 | dprintk(DBGLVL_ENC, "%s()\n", __func__); | |
742 | ||
743 | /* Shut device down on last close */ | |
744 | if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) { | |
745 | if (atomic_dec_return(&port->v4l_reader_count) == 0) { | |
746 | /* stop mpeg capture then cancel buffers */ | |
747 | saa7164_encoder_stop_streaming(port); | |
748 | } | |
749 | } | |
750 | ||
d6d3fe2f HV |
751 | v4l2_fh_del(&fh->fh); |
752 | v4l2_fh_exit(&fh->fh); | |
7615e434 ST |
753 | kfree(fh); |
754 | ||
755 | return 0; | |
756 | } | |
757 | ||
5faf7db8 MCC |
758 | static struct |
759 | saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port) | |
7615e434 | 760 | { |
61ca1500 | 761 | struct saa7164_user_buffer *ubuf = NULL; |
7615e434 | 762 | struct saa7164_dev *dev = port->dev; |
12d3203e | 763 | u32 crc; |
7615e434 ST |
764 | |
765 | mutex_lock(&port->dmaqueue_lock); | |
766 | if (!list_empty(&port->list_buf_used.list)) { | |
1b0e8e46 | 767 | ubuf = list_first_entry(&port->list_buf_used.list, |
7615e434 | 768 | struct saa7164_user_buffer, list); |
12d3203e | 769 | |
1b0e8e46 ST |
770 | if (crc_checking) { |
771 | crc = crc32(0, ubuf->data, ubuf->actual_size); | |
772 | if (crc != ubuf->crc) { | |
bc250684 ST |
773 | printk(KERN_ERR |
774 | "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n", | |
775 | __func__, | |
1b0e8e46 ST |
776 | ubuf, ubuf->crc, crc); |
777 | } | |
12d3203e ST |
778 | } |
779 | ||
7615e434 ST |
780 | } |
781 | mutex_unlock(&port->dmaqueue_lock); | |
782 | ||
1b0e8e46 | 783 | dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf); |
7615e434 | 784 | |
1b0e8e46 | 785 | return ubuf; |
7615e434 ST |
786 | } |
787 | ||
788 | static ssize_t fops_read(struct file *file, char __user *buffer, | |
789 | size_t count, loff_t *pos) | |
790 | { | |
96d8420d | 791 | struct saa7164_encoder_fh *fh = file->private_data; |
7615e434 ST |
792 | struct saa7164_port *port = fh->port; |
793 | struct saa7164_user_buffer *ubuf = NULL; | |
794 | struct saa7164_dev *dev = port->dev; | |
f20a07d4 | 795 | int ret = 0; |
7615e434 ST |
796 | int rem, cnt; |
797 | u8 *p; | |
798 | ||
58acca10 ST |
799 | port->last_read_msecs_diff = port->last_read_msecs; |
800 | port->last_read_msecs = jiffies_to_msecs(jiffies); | |
801 | port->last_read_msecs_diff = port->last_read_msecs - | |
802 | port->last_read_msecs_diff; | |
803 | ||
804 | saa7164_histogram_update(&port->read_interval, | |
805 | port->last_read_msecs_diff); | |
806 | ||
46eeb8dd ST |
807 | if (*pos) { |
808 | printk(KERN_ERR "%s() ESPIPE\n", __func__); | |
7615e434 | 809 | return -ESPIPE; |
46eeb8dd | 810 | } |
7615e434 ST |
811 | |
812 | if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) { | |
813 | if (atomic_inc_return(&port->v4l_reader_count) == 1) { | |
814 | ||
46eeb8dd ST |
815 | if (saa7164_encoder_initialize(port) < 0) { |
816 | printk(KERN_ERR "%s() EINVAL\n", __func__); | |
7615e434 | 817 | return -EINVAL; |
46eeb8dd | 818 | } |
7615e434 ST |
819 | |
820 | saa7164_encoder_start_streaming(port); | |
821 | msleep(200); | |
822 | } | |
823 | } | |
824 | ||
825 | /* blocking wait for buffer */ | |
826 | if ((file->f_flags & O_NONBLOCK) == 0) { | |
827 | if (wait_event_interruptible(port->wait_read, | |
828 | saa7164_enc_next_buf(port))) { | |
46eeb8dd | 829 | printk(KERN_ERR "%s() ERESTARTSYS\n", __func__); |
7615e434 ST |
830 | return -ERESTARTSYS; |
831 | } | |
832 | } | |
833 | ||
834 | /* Pull the first buffer from the used list */ | |
835 | ubuf = saa7164_enc_next_buf(port); | |
836 | ||
837 | while ((count > 0) && ubuf) { | |
838 | ||
839 | /* set remaining bytes to copy */ | |
840 | rem = ubuf->actual_size - ubuf->pos; | |
841 | cnt = rem > count ? count : rem; | |
842 | ||
843 | p = ubuf->data + ubuf->pos; | |
844 | ||
845 | dprintk(DBGLVL_ENC, | |
846 | "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n", | |
847 | __func__, (int)count, cnt, rem, ubuf, ubuf->pos); | |
848 | ||
849 | if (copy_to_user(buffer, p, cnt)) { | |
850 | printk(KERN_ERR "%s() copy_to_user failed\n", __func__); | |
46eeb8dd ST |
851 | if (!ret) { |
852 | printk(KERN_ERR "%s() EFAULT\n", __func__); | |
7615e434 | 853 | ret = -EFAULT; |
46eeb8dd | 854 | } |
7615e434 ST |
855 | goto err; |
856 | } | |
857 | ||
858 | ubuf->pos += cnt; | |
859 | count -= cnt; | |
860 | buffer += cnt; | |
861 | ret += cnt; | |
862 | ||
bc250684 | 863 | if (ubuf->pos > ubuf->actual_size) |
46eeb8dd | 864 | printk(KERN_ERR "read() pos > actual, huh?\n"); |
46eeb8dd | 865 | |
7615e434 ST |
866 | if (ubuf->pos == ubuf->actual_size) { |
867 | ||
868 | /* finished with current buffer, take next buffer */ | |
869 | ||
870 | /* Requeue the buffer on the free list */ | |
871 | ubuf->pos = 0; | |
872 | ||
873 | mutex_lock(&port->dmaqueue_lock); | |
874 | list_move_tail(&ubuf->list, &port->list_buf_free.list); | |
875 | mutex_unlock(&port->dmaqueue_lock); | |
876 | ||
877 | /* Dequeue next */ | |
878 | if ((file->f_flags & O_NONBLOCK) == 0) { | |
879 | if (wait_event_interruptible(port->wait_read, | |
880 | saa7164_enc_next_buf(port))) { | |
881 | break; | |
882 | } | |
883 | } | |
884 | ubuf = saa7164_enc_next_buf(port); | |
885 | } | |
886 | } | |
887 | err: | |
bc250684 | 888 | if (!ret && !ubuf) |
7615e434 ST |
889 | ret = -EAGAIN; |
890 | ||
891 | return ret; | |
892 | } | |
893 | ||
c23e0cb8 | 894 | static __poll_t fops_poll(struct file *file, poll_table *wait) |
7615e434 | 895 | { |
01699437 | 896 | __poll_t req_events = poll_requested_events(wait); |
bc250684 ST |
897 | struct saa7164_encoder_fh *fh = |
898 | (struct saa7164_encoder_fh *)file->private_data; | |
7615e434 | 899 | struct saa7164_port *port = fh->port; |
c23e0cb8 | 900 | __poll_t mask = v4l2_ctrl_poll(file, wait); |
7615e434 | 901 | |
58acca10 ST |
902 | port->last_poll_msecs_diff = port->last_poll_msecs; |
903 | port->last_poll_msecs = jiffies_to_msecs(jiffies); | |
904 | port->last_poll_msecs_diff = port->last_poll_msecs - | |
905 | port->last_poll_msecs_diff; | |
906 | ||
907 | saa7164_histogram_update(&port->poll_interval, | |
908 | port->last_poll_msecs_diff); | |
909 | ||
a9a08845 | 910 | if (!(req_events & (EPOLLIN | EPOLLRDNORM))) |
45053edc | 911 | return mask; |
7615e434 ST |
912 | |
913 | if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) { | |
914 | if (atomic_inc_return(&port->v4l_reader_count) == 1) { | |
915 | if (saa7164_encoder_initialize(port) < 0) | |
a9a08845 | 916 | return mask | EPOLLERR; |
7615e434 ST |
917 | saa7164_encoder_start_streaming(port); |
918 | msleep(200); | |
919 | } | |
920 | } | |
921 | ||
7615e434 | 922 | /* Pull the first buffer from the used list */ |
061d55eb | 923 | if (!list_empty(&port->list_buf_used.list)) |
a9a08845 | 924 | mask |= EPOLLIN | EPOLLRDNORM; |
7615e434 ST |
925 | |
926 | return mask; | |
927 | } | |
928 | ||
1a708ea0 HV |
929 | static const struct v4l2_ctrl_ops saa7164_ctrl_ops = { |
930 | .s_ctrl = saa7164_s_ctrl, | |
931 | }; | |
932 | ||
7615e434 ST |
933 | static const struct v4l2_file_operations mpeg_fops = { |
934 | .owner = THIS_MODULE, | |
935 | .open = fops_open, | |
936 | .release = fops_release, | |
937 | .read = fops_read, | |
938 | .poll = fops_poll, | |
939 | .unlocked_ioctl = video_ioctl2, | |
940 | }; | |
941 | ||
7615e434 ST |
942 | static const struct v4l2_ioctl_ops mpeg_ioctl_ops = { |
943 | .vidioc_s_std = vidioc_s_std, | |
8d2d41e9 | 944 | .vidioc_g_std = vidioc_g_std, |
225b783b | 945 | .vidioc_enum_input = saa7164_enum_input, |
7615e434 ST |
946 | .vidioc_g_input = vidioc_g_input, |
947 | .vidioc_s_input = vidioc_s_input, | |
225b783b HV |
948 | .vidioc_g_tuner = saa7164_g_tuner, |
949 | .vidioc_s_tuner = saa7164_s_tuner, | |
7615e434 ST |
950 | .vidioc_g_frequency = vidioc_g_frequency, |
951 | .vidioc_s_frequency = vidioc_s_frequency, | |
7615e434 ST |
952 | .vidioc_querycap = vidioc_querycap, |
953 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, | |
031d2297 HV |
954 | .vidioc_g_fmt_vid_cap = vidioc_fmt_vid_cap, |
955 | .vidioc_try_fmt_vid_cap = vidioc_fmt_vid_cap, | |
956 | .vidioc_s_fmt_vid_cap = vidioc_fmt_vid_cap, | |
245b5ae9 HV |
957 | .vidioc_log_status = v4l2_ctrl_log_status, |
958 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
959 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
7615e434 ST |
960 | }; |
961 | ||
962 | static struct video_device saa7164_mpeg_template = { | |
963 | .name = "saa7164", | |
964 | .fops = &mpeg_fops, | |
965 | .ioctl_ops = &mpeg_ioctl_ops, | |
966 | .minor = -1, | |
967 | .tvnorms = SAA7164_NORMS, | |
21615365 HV |
968 | .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | |
969 | V4L2_CAP_TUNER, | |
7615e434 ST |
970 | }; |
971 | ||
972 | static struct video_device *saa7164_encoder_alloc( | |
973 | struct saa7164_port *port, | |
974 | struct pci_dev *pci, | |
975 | struct video_device *template, | |
976 | char *type) | |
977 | { | |
978 | struct video_device *vfd; | |
979 | struct saa7164_dev *dev = port->dev; | |
980 | ||
981 | dprintk(DBGLVL_ENC, "%s()\n", __func__); | |
982 | ||
983 | vfd = video_device_alloc(); | |
984 | if (NULL == vfd) | |
985 | return NULL; | |
986 | ||
987 | *vfd = *template; | |
988 | snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, | |
989 | type, saa7164_boards[dev->board].name); | |
990 | ||
d66de790 | 991 | vfd->v4l2_dev = &dev->v4l2_dev; |
7615e434 ST |
992 | vfd->release = video_device_release; |
993 | return vfd; | |
994 | } | |
995 | ||
996 | int saa7164_encoder_register(struct saa7164_port *port) | |
997 | { | |
998 | struct saa7164_dev *dev = port->dev; | |
1a708ea0 | 999 | struct v4l2_ctrl_handler *hdl = &port->ctrl_handler; |
1b0e8e46 | 1000 | int result = -ENODEV; |
7615e434 ST |
1001 | |
1002 | dprintk(DBGLVL_ENC, "%s()\n", __func__); | |
1003 | ||
2aefee05 | 1004 | BUG_ON(port->type != SAA7164_MPEG_ENCODER); |
7615e434 ST |
1005 | |
1006 | /* Sanity check that the PCI configuration space is active */ | |
1007 | if (port->hwcfg.BARLocation == 0) { | |
24f711c1 | 1008 | printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n", |
7615e434 ST |
1009 | __func__, result); |
1010 | result = -ENOMEM; | |
1011 | goto failed; | |
1012 | } | |
1013 | ||
7615e434 ST |
1014 | /* Establish encoder defaults here */ |
1015 | /* Set default TV standard */ | |
1016 | port->encodernorm = saa7164_tvnorms[0]; | |
1017 | port->width = 720; | |
1018 | port->mux_input = 1; /* Composite */ | |
7615e434 ST |
1019 | port->video_format = EU_VIDEO_FORMAT_MPEG_2; |
1020 | port->audio_format = 0; | |
1021 | port->video_resolution = 0; | |
6b996126 | 1022 | port->freq = SAA7164_TV_MIN_FREQ; |
1a708ea0 HV |
1023 | |
1024 | v4l2_ctrl_handler_init(hdl, 14); | |
1025 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1026 | V4L2_CID_BRIGHTNESS, 0, 255, 1, 127); | |
1027 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1028 | V4L2_CID_CONTRAST, 0, 255, 1, 66); | |
1029 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1030 | V4L2_CID_SATURATION, 0, 255, 1, 62); | |
1031 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1032 | V4L2_CID_HUE, 0, 255, 1, 128); | |
1033 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1034 | V4L2_CID_SHARPNESS, 0x0, 0x0f, 1, 8); | |
1035 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1036 | V4L2_CID_MPEG_AUDIO_MUTE, 0x0, 0x01, 1, 0); | |
1037 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1038 | V4L2_CID_AUDIO_VOLUME, -83, 24, 1, 20); | |
1039 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1040 | V4L2_CID_MPEG_VIDEO_BITRATE, | |
1041 | ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE, | |
1042 | 100000, ENCODER_DEF_BITRATE); | |
1043 | v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops, | |
1044 | V4L2_CID_MPEG_STREAM_TYPE, | |
1045 | V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 0, | |
1046 | V4L2_MPEG_STREAM_TYPE_MPEG2_PS); | |
1047 | v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops, | |
1048 | V4L2_CID_MPEG_VIDEO_ASPECT, | |
1049 | V4L2_MPEG_VIDEO_ASPECT_221x100, 0, | |
1050 | V4L2_MPEG_VIDEO_ASPECT_4x3); | |
1051 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1052 | V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, 15); | |
1053 | v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops, | |
1054 | V4L2_CID_MPEG_VIDEO_BITRATE_MODE, | |
1055 | V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0, | |
1056 | V4L2_MPEG_VIDEO_BITRATE_MODE_VBR); | |
1057 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1058 | V4L2_CID_MPEG_VIDEO_B_FRAMES, 1, 3, 1, 1); | |
1059 | v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops, | |
1060 | V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, | |
1061 | ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE, | |
1062 | 100000, ENCODER_DEF_BITRATE); | |
1063 | if (hdl->error) { | |
1064 | result = hdl->error; | |
1065 | goto failed; | |
1066 | } | |
1067 | ||
8d2d41e9 | 1068 | port->std = V4L2_STD_NTSC_M; |
7615e434 ST |
1069 | |
1070 | if (port->encodernorm.id & V4L2_STD_525_60) | |
1071 | port->height = 480; | |
1072 | else | |
1073 | port->height = 576; | |
1074 | ||
1075 | /* Allocate and register the video device node */ | |
1076 | port->v4l_device = saa7164_encoder_alloc(port, | |
1077 | dev->pci, &saa7164_mpeg_template, "mpeg"); | |
1078 | ||
61ca1500 | 1079 | if (!port->v4l_device) { |
7615e434 ST |
1080 | printk(KERN_INFO "%s: can't allocate mpeg device\n", |
1081 | dev->name); | |
1082 | result = -ENOMEM; | |
1083 | goto failed; | |
1084 | } | |
1085 | ||
1a708ea0 HV |
1086 | port->v4l_device->ctrl_handler = hdl; |
1087 | v4l2_ctrl_handler_setup(hdl); | |
214ce3fa | 1088 | video_set_drvdata(port->v4l_device, port); |
7615e434 | 1089 | result = video_register_device(port->v4l_device, |
3e30a927 | 1090 | VFL_TYPE_VIDEO, -1); |
7615e434 ST |
1091 | if (result < 0) { |
1092 | printk(KERN_INFO "%s: can't register mpeg device\n", | |
1093 | dev->name); | |
1094 | /* TODO: We're going to leak here if we don't dealloc | |
1095 | The buffers above. The unreg function can't deal wit it. | |
1096 | */ | |
1097 | goto failed; | |
1098 | } | |
1099 | ||
1100 | printk(KERN_INFO "%s: registered device video%d [mpeg]\n", | |
1101 | dev->name, port->v4l_device->num); | |
1102 | ||
1103 | /* Configure the hardware defaults */ | |
1104 | saa7164_api_set_videomux(port); | |
1105 | saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL); | |
1106 | saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL); | |
1107 | saa7164_api_set_usercontrol(port, PU_HUE_CONTROL); | |
1108 | saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL); | |
1109 | saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL); | |
1110 | saa7164_api_audio_mute(port, 0); | |
1111 | saa7164_api_set_audio_volume(port, 20); | |
1112 | saa7164_api_set_aspect_ratio(port); | |
1113 | ||
1114 | /* Disable audio standard detection, it's buggy */ | |
1115 | saa7164_api_set_audio_detection(port, 0); | |
1116 | ||
1117 | saa7164_api_set_encoder(port); | |
1118 | saa7164_api_get_encoder(port); | |
1119 | ||
1120 | result = 0; | |
1121 | failed: | |
1122 | return result; | |
1123 | } | |
1124 | ||
1125 | void saa7164_encoder_unregister(struct saa7164_port *port) | |
1126 | { | |
1127 | struct saa7164_dev *dev = port->dev; | |
7615e434 ST |
1128 | |
1129 | dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); | |
1130 | ||
2aefee05 | 1131 | BUG_ON(port->type != SAA7164_MPEG_ENCODER); |
7615e434 ST |
1132 | |
1133 | if (port->v4l_device) { | |
1134 | if (port->v4l_device->minor != -1) | |
1135 | video_unregister_device(port->v4l_device); | |
1136 | else | |
1137 | video_device_release(port->v4l_device); | |
1138 | ||
1139 | port->v4l_device = NULL; | |
1140 | } | |
1a708ea0 | 1141 | v4l2_ctrl_handler_free(&port->ctrl_handler); |
7615e434 | 1142 | |
7615e434 ST |
1143 | dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr); |
1144 | } | |
1145 |