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