aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/saa7164
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/saa7164')
-rw-r--r--drivers/media/video/saa7164/saa7164-api.c669
-rw-r--r--drivers/media/video/saa7164/saa7164-buffer.c38
-rw-r--r--drivers/media/video/saa7164/saa7164-cards.c14
-rw-r--r--drivers/media/video/saa7164/saa7164-core.c167
-rw-r--r--drivers/media/video/saa7164/saa7164-encoder.c1371
-rw-r--r--drivers/media/video/saa7164/saa7164.h127
6 files changed, 2348 insertions, 38 deletions
diff --git a/drivers/media/video/saa7164/saa7164-api.c b/drivers/media/video/saa7164/saa7164-api.c
index a810e6d8827..5e05723fe44 100644
--- a/drivers/media/video/saa7164/saa7164-api.c
+++ b/drivers/media/video/saa7164/saa7164-api.c
@@ -24,6 +24,543 @@
24 24
25#include "saa7164.h" 25#include "saa7164.h"
26 26
27int saa7164_api_set_encoder(struct saa7164_port *port)
28{
29 struct saa7164_dev *dev = port->dev;
30 tmComResEncVideoBitRate_t vb;
31 tmComResEncAudioBitRate_t ab;
32 int ret;
33
34 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__, port->hwcfg.sourceid);
35
36 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
37 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
38 if (ret != SAA_OK)
39 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
40
41 /* Establish video bitrates */
42 vb.ucVideoBitRateMode = 0;
43 vb.dwVideoBitRate = port->encoder_params.bitrate;
44 vb.dwVideoBitRatePeak = vb.dwVideoBitRate;
45 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
46 EU_VIDEO_BIT_RATE_CONTROL, sizeof(tmComResEncVideoBitRate_t), &vb);
47 if (ret != SAA_OK)
48 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
49
50 /* Establish audio bitrates */
51 ab.ucAudioBitRateMode = 0;
52 ab.dwAudioBitRate = 384000;
53 ab.dwAudioBitRatePeak = ab.dwAudioBitRate;
54 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
55 EU_AUDIO_BIT_RATE_CONTROL, sizeof(tmComResEncAudioBitRate_t), &ab);
56 if (ret != SAA_OK)
57 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
58
59 saa7164_api_set_aspect_ratio(port);
60
61 return ret;
62}
63
64int saa7164_api_get_encoder(struct saa7164_port *port)
65{
66 struct saa7164_dev *dev = port->dev;
67 tmComResEncVideoBitRate_t v;
68 tmComResEncAudioBitRate_t a;
69 tmComResEncVideoInputAspectRatio_t ar;
70 int ret;
71
72 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__, port->hwcfg.sourceid);
73
74 port->encoder_profile = 0;
75 port->video_format = 0;
76 port->video_resolution = 0;
77 port->audio_format = 0;
78
79 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
80 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
81 if (ret != SAA_OK)
82 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
83
84 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
85 EU_VIDEO_FORMAT_CONTROL, sizeof(u8), &port->video_format);
86 if (ret != SAA_OK)
87 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
88
89 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
90 EU_VIDEO_BIT_RATE_CONTROL, sizeof(v), &v);
91 if (ret != SAA_OK)
92 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
93
94 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
95 EU_AUDIO_FORMAT_CONTROL, sizeof(u8), &port->audio_format);
96 if (ret != SAA_OK)
97 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
98
99 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
100 EU_AUDIO_BIT_RATE_CONTROL, sizeof(a), &a);
101 if (ret != SAA_OK)
102 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
103
104 /* Aspect Ratio */
105 ar.width = 0;
106 ar.height = 0;
107 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
108 EU_VIDEO_INPUT_ASPECT_CONTROL,
109 sizeof(tmComResEncVideoInputAspectRatio_t), &ar);
110 if (ret != SAA_OK)
111 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
112
113 dprintk(DBGLVL_ENC, "encoder_profile = %d\n", port->encoder_profile);
114 dprintk(DBGLVL_ENC, "video_format = %d\n", port->video_format);
115 dprintk(DBGLVL_ENC, "audio_format = %d\n", port->audio_format);
116 dprintk(DBGLVL_ENC, "video_resolution= %d\n", port->video_resolution);
117 dprintk(DBGLVL_ENC, "v.ucVideoBitRateMode = %d\n", v.ucVideoBitRateMode);
118 dprintk(DBGLVL_ENC, "v.dwVideoBitRate = %d\n", v.dwVideoBitRate);
119 dprintk(DBGLVL_ENC, "v.dwVideoBitRatePeak = %d\n", v.dwVideoBitRatePeak);
120 dprintk(DBGLVL_ENC, "a.ucVideoBitRateMode = %d\n", a.ucAudioBitRateMode);
121 dprintk(DBGLVL_ENC, "a.dwVideoBitRate = %d\n", a.dwAudioBitRate);
122 dprintk(DBGLVL_ENC, "a.dwVideoBitRatePeak = %d\n", a.dwAudioBitRatePeak);
123 dprintk(DBGLVL_ENC, "aspect.width / height = %d:%d\n", ar.width, ar.height);
124
125 return ret;
126}
127
128int saa7164_api_set_aspect_ratio(struct saa7164_port *port)
129{
130 struct saa7164_dev *dev = port->dev;
131 tmComResEncVideoInputAspectRatio_t ar;
132 int ret;
133
134 dprintk(DBGLVL_ENC, "%s(%d)\n", __func__,
135 port->encoder_params.ctl_aspect);
136
137 switch (port->encoder_params.ctl_aspect) {
138 case V4L2_MPEG_VIDEO_ASPECT_1x1:
139 ar.width = 1;
140 ar.height = 1;
141 break;
142 case V4L2_MPEG_VIDEO_ASPECT_4x3:
143 ar.width = 4;
144 ar.height = 3;
145 break;
146 case V4L2_MPEG_VIDEO_ASPECT_16x9:
147 ar.width = 16;
148 ar.height = 9;
149 break;
150 case V4L2_MPEG_VIDEO_ASPECT_221x100:
151 ar.width = 221;
152 ar.height = 100;
153 break;
154 default:
155 BUG();
156 }
157
158 dprintk(DBGLVL_ENC, "%s(%d) now %d:%d\n", __func__,
159 port->encoder_params.ctl_aspect,
160 ar.width, ar.height);
161
162 /* Aspect Ratio */
163 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
164 EU_VIDEO_INPUT_ASPECT_CONTROL,
165 sizeof(tmComResEncVideoInputAspectRatio_t), &ar);
166 if (ret != SAA_OK)
167 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
168
169 return ret;
170}
171
172int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl)
173{
174 struct saa7164_dev *dev = port->dev;
175 int ret;
176 u16 val;
177
178 if (ctl == PU_BRIGHTNESS_CONTROL)
179 val = port->ctl_brightness;
180 else
181 if (ctl == PU_CONTRAST_CONTROL)
182 val = port->ctl_contrast;
183 else
184 if (ctl == PU_HUE_CONTROL)
185 val = port->ctl_hue;
186 else
187 if (ctl == PU_SATURATION_CONTROL)
188 val = port->ctl_saturation;
189 else
190 if (ctl == PU_SHARPNESS_CONTROL)
191 val = port->ctl_sharpness;
192 else
193 return -EINVAL;
194
195 dprintk(DBGLVL_ENC, "%s() unitid=0x%x ctl=%d, val=%d\n",
196 __func__, port->encunit.vsourceid, ctl, val);
197
198 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, SET_CUR,
199 ctl, sizeof(u16), &val);
200 if (ret != SAA_OK)
201 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
202
203 return ret;
204}
205
206int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl)
207{
208 struct saa7164_dev *dev = port->dev;
209 int ret;
210 u16 val;
211
212 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, GET_CUR,
213 ctl, sizeof(u16), &val);
214 if (ret != SAA_OK) {
215 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
216 return ret;
217 }
218
219 dprintk(DBGLVL_ENC, "%s() ctl=%d, val=%d\n",
220 __func__, ctl, val);
221
222 if (ctl == PU_BRIGHTNESS_CONTROL)
223 port->ctl_brightness = val;
224 else
225 if (ctl == PU_CONTRAST_CONTROL)
226 port->ctl_contrast = val;
227 else
228 if (ctl == PU_HUE_CONTROL)
229 port->ctl_hue = val;
230 else
231 if (ctl == PU_SATURATION_CONTROL)
232 port->ctl_saturation = val;
233 else
234 if (ctl == PU_SHARPNESS_CONTROL)
235 port->ctl_sharpness = val;
236
237 return ret;
238}
239
240int saa7164_api_set_videomux(struct saa7164_port *port)
241{
242 struct saa7164_dev *dev = port->dev;
243 u8 inputs[] = { 1, 2, 2, 2, 5, 5, 5 };
244 int ret;
245
246 dprintk(DBGLVL_ENC, "%s() v_mux=%d a_mux=%d\n",
247 __func__, port->mux_input, inputs[ port->mux_input - 1 ]);
248
249 /* Audio Mute */
250 ret = saa7164_api_audio_mute(port, 1);
251 if (ret != SAA_OK)
252 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
253
254 /* Video Mux */
255 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, SET_CUR,
256 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
257 if (ret != SAA_OK)
258 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
259 /* Audio Mux */
260 ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
261 SU_INPUT_SELECT_CONTROL, sizeof(u8), &inputs[ port->mux_input - 1 ]);
262 if (ret != SAA_OK)
263 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
264 /* Audio UnMute */
265 ret = saa7164_api_audio_mute(port, 0);
266 if (ret != SAA_OK)
267 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
268
269 return ret;
270}
271
272int saa7164_api_audio_mute(struct saa7164_port *port, int mute)
273{
274 struct saa7164_dev *dev = port->dev;
275 u8 v = mute;
276 int ret;
277
278 dprintk(DBGLVL_API, "%s(%d)\n", __func__, mute);
279
280 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
281 MUTE_CONTROL, sizeof(u8), &v);
282 if (ret != SAA_OK)
283 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
284
285 return ret;
286}
287
288/* 0 = silence, 0xff = full */
289int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level)
290{
291 struct saa7164_dev *dev = port->dev;
292 s16 v, min, max;
293 int ret;
294
295 dprintk(DBGLVL_API, "%s(%d)\n", __func__, level);
296
297 /* Obtain the min/max ranges */
298 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MIN,
299 VOLUME_CONTROL, sizeof(u16), &min);
300 if (ret != SAA_OK)
301 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
302
303 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MAX,
304 VOLUME_CONTROL, sizeof(u16), &max);
305 if (ret != SAA_OK)
306 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
307
308 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
309 ( 0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
310 if (ret != SAA_OK)
311 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
312
313 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
314
315 v = level;
316 if (v < min)
317 v = min;
318 if (v > max)
319 v = max;
320
321 /* Left */
322 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
323 ( 0x01 << 8 ) | VOLUME_CONTROL, sizeof(s16), &v);
324 if (ret != SAA_OK)
325 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
326
327 /* Right */
328 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
329 ( 0x02 << 8 ) | VOLUME_CONTROL, sizeof(s16), &v);
330 if (ret != SAA_OK)
331 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
332
333 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
334 ( 0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
335 if (ret != SAA_OK)
336 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
337
338 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
339
340 return ret;
341}
342
343int saa7164_api_set_audio_std(struct saa7164_port *port)
344{
345 struct saa7164_dev *dev = port->dev;
346 tmComResAudioDefaults_t lvl;
347 tmComResTunerStandard_t tvaudio;
348 int ret;
349
350 dprintk(DBGLVL_API, "%s()\n", __func__);
351
352 /* Establish default levels */
353 lvl.ucDecoderLevel = TMHW_LEV_ADJ_DECLEV_DEFAULT;
354 lvl.ucDecoderFM_Level = TMHW_LEV_ADJ_DECLEV_DEFAULT;
355 lvl.ucMonoLevel = TMHW_LEV_ADJ_MONOLEV_DEFAULT;
356 lvl.ucNICAM_Level = TMHW_LEV_ADJ_NICLEV_DEFAULT;
357 lvl.ucSAP_Level = TMHW_LEV_ADJ_SAPLEV_DEFAULT;
358 lvl.ucADC_Level = TMHW_LEV_ADJ_ADCLEV_DEFAULT;
359 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
360 AUDIO_DEFAULT_CONTROL, sizeof(tmComResAudioDefaults_t), &lvl);
361 if (ret != SAA_OK)
362 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
363
364 /* Manually select the appropriate TV audio standard */
365 if (port->encodernorm.id & V4L2_STD_NTSC) {
366 tvaudio.std = TU_STANDARD_NTSC_M;
367 tvaudio.country = 1;
368 } else {
369 tvaudio.std = TU_STANDARD_PAL_I;
370 tvaudio.country = 44;
371 }
372
373 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
374 TU_STANDARD_CONTROL, sizeof(tvaudio), &tvaudio);
375 if (ret != SAA_OK)
376 printk(KERN_ERR "%s() TU_STANDARD_CONTROL error, ret = 0x%x\n", __func__, ret);
377 return ret;
378}
379
380int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect)
381{
382 struct saa7164_dev *dev = port->dev;
383 tmComResTunerStandardAuto_t p;
384 int ret;
385
386 dprintk(DBGLVL_API, "%s(%d)\n", __func__, autodetect);
387
388 /* Disable TV Audio autodetect if not already set (buggy) */
389 if (autodetect)
390 p.mode = TU_STANDARD_AUTO;
391 else
392 p.mode = TU_STANDARD_MANUAL;
393 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
394 TU_STANDARD_AUTO_CONTROL, sizeof(p), &p);
395 if (ret != SAA_OK)
396 printk(KERN_ERR "%s() TU_STANDARD_AUTO_CONTROL error, ret = 0x%x\n", __func__, ret);
397
398 return ret;
399}
400
401int saa7164_api_get_videomux(struct saa7164_port *port)
402{
403 struct saa7164_dev *dev = port->dev;
404 int ret;
405
406 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, GET_CUR,
407 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
408 if (ret != SAA_OK)
409 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
410
411 dprintk(DBGLVL_ENC, "%s() v_mux=%d\n",
412 __func__, port->mux_input);
413
414 return ret;
415}
416
417int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
418{
419 struct saa7164_dev *dev = port->dev;
420
421 u16 len = 0;
422 u8 buf[256];
423 int ret;
424 u8 mas;
425
426 dprintk(DBGLVL_API, "%s()\n", __func__);
427
428 if (port->nr == 0)
429 mas = 0xd0;
430 else
431 mas = 0xe0;
432
433 memset(buf, 0, sizeof(buf));
434
435 buf[0x00] = 0x04;
436 buf[0x01] = 0x00;
437 buf[0x02] = 0x00;
438 buf[0x03] = 0x00;
439
440 buf[0x04] = 0x04;
441 buf[0x05] = 0x00;
442 buf[0x06] = 0x00;
443 buf[0x07] = 0x00;
444
445 buf[0x08] = reg;
446 buf[0x09] = 0x26;
447 buf[0x0a] = mas;
448 buf[0x0b] = 0xb0;
449
450 buf[0x0c] = val;
451 buf[0x0d] = 0x00;
452 buf[0x0e] = 0x00;
453 buf[0x0f] = 0x00;
454
455 ret = saa7164_cmd_send(dev, port->ifunit.unitid, GET_LEN,
456 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
457 if (ret != SAA_OK) {
458 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
459 return -EIO;
460 }
461
462 ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
463 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
464 if (ret != SAA_OK)
465 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
466
467 //saa7164_dumphex16(dev, buf, 16);
468
469 return ret == SAA_OK ? 0 : -EIO;
470}
471
472/* Disable the IF block AGC controls */
473int saa7164_api_configure_dif(struct saa7164_port *port, u32 std)
474{
475 struct saa7164_dev *dev = port->dev;
476 int ret = 0;
477 u8 agc_disable;
478
479 dprintk(DBGLVL_API, "%s(%p, 0x%x)\n", __func__, port, std);
480
481 if (std & V4L2_STD_NTSC) {
482 dprintk(DBGLVL_API, " NTSC\n");
483 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
484 agc_disable = 0;
485 } else if (std & V4L2_STD_PAL_I) {
486 dprintk(DBGLVL_API, " PAL-I\n");
487 saa7164_api_set_dif(port, 0x00, 0x08); /* Video Standard */
488 agc_disable = 0;
489 } else if (std & V4L2_STD_PAL_M) {
490 dprintk(DBGLVL_API, " PAL-M\n");
491 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
492 agc_disable = 0;
493 } else if (std & V4L2_STD_PAL_N) {
494 dprintk(DBGLVL_API, " PAL-N\n");
495 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
496 agc_disable = 0;
497 } else if (std & V4L2_STD_PAL_Nc) {
498 dprintk(DBGLVL_API, " PAL-Nc\n");
499 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
500 agc_disable = 0;
501 } else if (std & V4L2_STD_PAL_B) {
502 dprintk(DBGLVL_API, " PAL-B\n");
503 saa7164_api_set_dif(port, 0x00, 0x02); /* Video Standard */
504 agc_disable = 0;
505 } else if (std & V4L2_STD_PAL_DK) {
506 dprintk(DBGLVL_API, " PAL-DK\n");
507 saa7164_api_set_dif(port, 0x00, 0x10); /* Video Standard */
508 agc_disable = 0;
509 } else if (std & V4L2_STD_SECAM_L) {
510 dprintk(DBGLVL_API, " SECAM-L\n");
511 saa7164_api_set_dif(port, 0x00, 0x20); /* Video Standard */
512 agc_disable = 0;
513 } else {
514 /* Unknown standard, assume DTV */
515 dprintk(DBGLVL_API, " Unknown (assuming DTV)\n");
516 saa7164_api_set_dif(port, 0x00, 0x80); /* Undefined Video Standard */
517 agc_disable = 1;
518 }
519
520 saa7164_api_set_dif(port, 0x48, 0xa0); /* AGC Functions 1 */
521 saa7164_api_set_dif(port, 0xc0, agc_disable); /* AGC Output Disable */
522 saa7164_api_set_dif(port, 0x7c, 0x04); /* CVBS EQ */
523 saa7164_api_set_dif(port, 0x04, 0x01); /* Active */
524 msleep(100);
525 saa7164_api_set_dif(port, 0x04, 0x00); /* Active (again) */
526 msleep(100);
527
528 return ret;
529}
530
531/* Ensure the dif is in the correct state for the operating mode
532 * (analog / dtv). We only configure the diff through the analog encoder
533 * so when we're in digital mode we need to find the appropriate encoder
534 * and use it to configure the DIF.
535 */
536int saa7164_api_initialize_dif(struct saa7164_port *port)
537{
538 struct saa7164_dev *dev = port->dev;
539 struct saa7164_port *p = 0;
540 int ret = -EINVAL;
541 u32 std = 0;
542
543 if (port->type == SAA7164_MPEG_ENCODER) {
544 /* Pick any analog standard to init the diff.
545 * we'll come back during encoder_init'
546 * and set the correct standard if requried.
547 */
548 std = V4L2_STD_NTSC;
549 } else
550 if (port->type == SAA7164_MPEG_DVB) {
551 if (port->nr == SAA7164_PORT_TS1)
552 p = &dev->ports[ SAA7164_PORT_ENC1 ];
553 else
554 p = &dev->ports[ SAA7164_PORT_ENC2 ];
555 } else
556 BUG();
557
558 if (p)
559 ret = saa7164_api_configure_dif(p, std);
560
561 return ret;
562}
563
27int saa7164_api_transition_port(struct saa7164_port *port, u8 mode) 564int saa7164_api_transition_port(struct saa7164_port *port, u8 mode)
28{ 565{
29 int ret; 566 int ret;
@@ -96,9 +633,43 @@ int saa7164_api_configure_port_mpeg2ts(struct saa7164_dev *dev,
96 return 0; 633 return 0;
97} 634}
98 635
636int saa7164_api_configure_port_mpeg2ps(struct saa7164_dev *dev,
637 struct saa7164_port *port,
638 tmComResPSFormatDescrHeader_t *fmt)
639{
640 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
641 dprintk(DBGLVL_API, " wPacketLength= 0x%x\n", fmt->wPacketLength);
642 dprintk(DBGLVL_API, " wPackLength= 0x%x\n", fmt->wPackLength);
643 dprintk(DBGLVL_API, " bPackDataType= 0x%x\n", fmt->bPackDataType);
644
645 /* Cache the hardware configuration in the port */
646 /* TODO: CHECK THIS in the port config */
647 port->bufcounter = port->hwcfg.BARLocation;
648 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
649 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
650 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
651 port->bufptr32l = port->hwcfg.BARLocation +
652 (4 * sizeof(u32)) +
653 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
654 port->bufptr32h = port->hwcfg.BARLocation +
655 (4 * sizeof(u32)) +
656 (sizeof(u32) * port->hwcfg.buffercount);
657 port->bufptr64 = port->hwcfg.BARLocation +
658 (4 * sizeof(u32)) +
659 (sizeof(u32) * port->hwcfg.buffercount);
660 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
661 port->hwcfg.BARLocation);
662
663 dprintk(DBGLVL_API, " = VS_FORMAT_MPEGPS (becomes dev->enc[%d])\n",
664 port->nr);
665
666 return 0;
667}
668
99int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len) 669int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
100{ 670{
101 struct saa7164_port *port = 0; 671 struct saa7164_port *tsport = 0;
672 struct saa7164_port *encport = 0;
102 u32 idx, next_offset; 673 u32 idx, next_offset;
103 int i; 674 int i;
104 tmComResDescrHeader_t *hdr, *t; 675 tmComResDescrHeader_t *hdr, *t;
@@ -108,6 +679,11 @@ int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
108 tmComResTunerDescrHeader_t *tunerunithdr; 679 tmComResTunerDescrHeader_t *tunerunithdr;
109 tmComResDMATermDescrHeader_t *vcoutputtermhdr; 680 tmComResDMATermDescrHeader_t *vcoutputtermhdr;
110 tmComResTSFormatDescrHeader_t *tsfmt; 681 tmComResTSFormatDescrHeader_t *tsfmt;
682 tmComResPSFormatDescrHeader_t *psfmt;
683 tmComResSelDescrHeader_t *psel;
684 tmComResProcDescrHeader_t *pdh;
685 tmComResAFeatureDescrHeader_t *afd;
686 tmComResEncoderDescrHeader_t *edh;
111 u32 currpath = 0; 687 u32 currpath = 0;
112 688
113 dprintk(DBGLVL_API, 689 dprintk(DBGLVL_API,
@@ -244,17 +820,25 @@ int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
244 tsfmt = 820 tsfmt =
245 (tmComResTSFormatDescrHeader_t *)t; 821 (tmComResTSFormatDescrHeader_t *)t;
246 if (currpath == 1) 822 if (currpath == 1)
247 port = &dev->ts1; 823 tsport = &dev->ports[ SAA7164_PORT_TS1 ];
248 else 824 else
249 port = &dev->ts2; 825 tsport = &dev->ports[ SAA7164_PORT_TS2 ];
250 memcpy(&port->hwcfg, vcoutputtermhdr, 826 memcpy(&tsport->hwcfg, vcoutputtermhdr,
251 sizeof(*vcoutputtermhdr)); 827 sizeof(*vcoutputtermhdr));
252 saa7164_api_configure_port_mpeg2ts(dev, 828 saa7164_api_configure_port_mpeg2ts(dev,
253 port, tsfmt); 829 tsport, tsfmt);
254 break; 830 break;
255 case VS_FORMAT_MPEG2PS: 831 case VS_FORMAT_MPEG2PS:
256 dprintk(DBGLVL_API, 832 psfmt =
257 " = VS_FORMAT_MPEG2PS\n"); 833 (tmComResPSFormatDescrHeader_t *)t;
834 if (currpath == 1)
835 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
836 else
837 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
838 memcpy(&encport->hwcfg, vcoutputtermhdr,
839 sizeof(*vcoutputtermhdr));
840 saa7164_api_configure_port_mpeg2ps(dev,
841 encport, psfmt);
258 break; 842 break;
259 case VS_FORMAT_VBI: 843 case VS_FORMAT_VBI:
260 dprintk(DBGLVL_API, 844 dprintk(DBGLVL_API,
@@ -297,18 +881,80 @@ int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
297 tunerunithdr->controlsize); 881 tunerunithdr->controlsize);
298 dprintk(DBGLVL_API, " controls = 0x%x\n", 882 dprintk(DBGLVL_API, " controls = 0x%x\n",
299 tunerunithdr->controls); 883 tunerunithdr->controls);
884
885 if (tunerunithdr->unitid == tunerunithdr->iunit) {
886 if (currpath == 1)
887 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
888 else
889 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
890 memcpy(&encport->tunerunit, tunerunithdr,
891 sizeof(tmComResTunerDescrHeader_t));
892 dprintk(DBGLVL_API, " (becomes dev->enc[%d] tuner)\n", encport->nr);
893 }
300 break; 894 break;
301 case VC_SELECTOR_UNIT: 895 case VC_SELECTOR_UNIT:
896 psel = (tmComResSelDescrHeader_t *)(buf + idx);
302 dprintk(DBGLVL_API, " VC_SELECTOR_UNIT\n"); 897 dprintk(DBGLVL_API, " VC_SELECTOR_UNIT\n");
898 dprintk(DBGLVL_API, " unitid = 0x%x\n",
899 psel->unitid);
900 dprintk(DBGLVL_API, " nrinpins = 0x%x\n",
901 psel->nrinpins);
902 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
903 psel->sourceid);
303 break; 904 break;
304 case VC_PROCESSING_UNIT: 905 case VC_PROCESSING_UNIT:
906 pdh = (tmComResProcDescrHeader_t *)(buf + idx);
305 dprintk(DBGLVL_API, " VC_PROCESSING_UNIT\n"); 907 dprintk(DBGLVL_API, " VC_PROCESSING_UNIT\n");
908 dprintk(DBGLVL_API, " unitid = 0x%x\n",
909 pdh->unitid);
910 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
911 pdh->sourceid);
912 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
913 pdh->controlsize);
914 if (pdh->controlsize == 0x04) {
915 if (currpath == 1)
916 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
917 else
918 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
919 memcpy(&encport->vidproc, pdh,
920 sizeof(tmComResProcDescrHeader_t));
921 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
922 }
306 break; 923 break;
307 case FEATURE_UNIT: 924 case FEATURE_UNIT:
925 afd = (tmComResAFeatureDescrHeader_t *)(buf + idx);
308 dprintk(DBGLVL_API, " FEATURE_UNIT\n"); 926 dprintk(DBGLVL_API, " FEATURE_UNIT\n");
927 dprintk(DBGLVL_API, " unitid = 0x%x\n",
928 afd->unitid);
929 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
930 afd->sourceid);
931 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
932 afd->controlsize);
933 if (currpath == 1)
934 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
935 else
936 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
937 memcpy(&encport->audfeat, afd,
938 sizeof(tmComResAFeatureDescrHeader_t));
939 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
309 break; 940 break;
310 case ENCODER_UNIT: 941 case ENCODER_UNIT:
942 edh = (tmComResEncoderDescrHeader_t *)(buf + idx);
311 dprintk(DBGLVL_API, " ENCODER_UNIT\n"); 943 dprintk(DBGLVL_API, " ENCODER_UNIT\n");
944 dprintk(DBGLVL_API, " subtype = 0x%x\n", edh->subtype);
945 dprintk(DBGLVL_API, " unitid = 0x%x\n", edh->unitid);
946 dprintk(DBGLVL_API, " vsourceid = 0x%x\n", edh->vsourceid);
947 dprintk(DBGLVL_API, " asourceid = 0x%x\n", edh->asourceid);
948 dprintk(DBGLVL_API, " iunit = 0x%x\n", edh->iunit);
949 if (edh->iunit == edh->unitid) {
950 if (currpath == 1)
951 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
952 else
953 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
954 memcpy(&encport->encunit, edh,
955 sizeof(tmComResEncoderDescrHeader_t));
956 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
957 }
312 break; 958 break;
313 case EXTENSION_UNIT: 959 case EXTENSION_UNIT:
314 dprintk(DBGLVL_API, " EXTENSION_UNIT\n"); 960 dprintk(DBGLVL_API, " EXTENSION_UNIT\n");
@@ -364,6 +1010,15 @@ int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
364 exthdr->numgpiogroups); 1010 exthdr->numgpiogroups);
365 dprintk(DBGLVL_API, " controlsize = 0x%x\n", 1011 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
366 exthdr->controlsize); 1012 exthdr->controlsize);
1013 if (exthdr->devicetype & 0x80) {
1014 if (currpath == 1)
1015 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
1016 else
1017 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
1018 memcpy(&encport->ifunit, exthdr,
1019 sizeof(tmComResExtDevDescrHeader_t));
1020 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
1021 }
367 break; 1022 break;
368 case PVC_INFRARED_UNIT: 1023 case PVC_INFRARED_UNIT:
369 dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n"); 1024 dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n");
diff --git a/drivers/media/video/saa7164/saa7164-buffer.c b/drivers/media/video/saa7164/saa7164-buffer.c
index 6f58af44d02..5fccecd588c 100644
--- a/drivers/media/video/saa7164/saa7164-buffer.c
+++ b/drivers/media/video/saa7164/saa7164-buffer.c
@@ -265,3 +265,41 @@ int saa7164_buffer_cfg_port(struct saa7164_port *port)
265 return 0; 265 return 0;
266} 266}
267 267
268struct saa7164_user_buffer *saa7164_buffer_alloc_user(struct saa7164_dev *dev, u32 len)
269{
270 struct saa7164_user_buffer *buf;
271
272 buf = kzalloc(sizeof(struct saa7164_user_buffer), GFP_KERNEL);
273 if (buf == 0)
274 return 0;
275
276 buf->data = kzalloc(len, GFP_KERNEL);
277
278 if (buf->data == 0) {
279 kfree(buf);
280 return 0;
281 }
282
283 buf->actual_size = len;
284 buf->pos = 0;
285
286 dprintk(DBGLVL_BUF, "%s() allocated user buffer @ 0x%p\n",
287 __func__, buf);
288
289 return buf;
290}
291
292void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf)
293{
294 if (!buf)
295 return;
296
297 if (buf->data) {
298 kfree(buf->data);
299 buf->data = 0;
300 }
301
302 if (buf)
303 kfree(buf);
304}
305
diff --git a/drivers/media/video/saa7164/saa7164-cards.c b/drivers/media/video/saa7164/saa7164-cards.c
index bd04ae0ce7d..81baa366291 100644
--- a/drivers/media/video/saa7164/saa7164-cards.c
+++ b/drivers/media/video/saa7164/saa7164-cards.c
@@ -55,6 +55,8 @@ struct saa7164_board saa7164_boards[] = {
55 .name = "Hauppauge WinTV-HVR2200", 55 .name = "Hauppauge WinTV-HVR2200",
56 .porta = SAA7164_MPEG_DVB, 56 .porta = SAA7164_MPEG_DVB,
57 .portb = SAA7164_MPEG_DVB, 57 .portb = SAA7164_MPEG_DVB,
58 .portc = SAA7164_MPEG_ENCODER,
59 .portd = SAA7164_MPEG_ENCODER,
58 .chiprev = SAA7164_CHIP_REV3, 60 .chiprev = SAA7164_CHIP_REV3,
59 .unit = {{ 61 .unit = {{
60 .id = 0x1d, 62 .id = 0x1d,
@@ -97,6 +99,8 @@ struct saa7164_board saa7164_boards[] = {
97 .name = "Hauppauge WinTV-HVR2200", 99 .name = "Hauppauge WinTV-HVR2200",
98 .porta = SAA7164_MPEG_DVB, 100 .porta = SAA7164_MPEG_DVB,
99 .portb = SAA7164_MPEG_DVB, 101 .portb = SAA7164_MPEG_DVB,
102 .portc = SAA7164_MPEG_ENCODER,
103 .portd = SAA7164_MPEG_ENCODER,
100 .chiprev = SAA7164_CHIP_REV2, 104 .chiprev = SAA7164_CHIP_REV2,
101 .unit = {{ 105 .unit = {{
102 .id = 0x06, 106 .id = 0x06,
@@ -139,6 +143,8 @@ struct saa7164_board saa7164_boards[] = {
139 .name = "Hauppauge WinTV-HVR2200", 143 .name = "Hauppauge WinTV-HVR2200",
140 .porta = SAA7164_MPEG_DVB, 144 .porta = SAA7164_MPEG_DVB,
141 .portb = SAA7164_MPEG_DVB, 145 .portb = SAA7164_MPEG_DVB,
146 .portc = SAA7164_MPEG_ENCODER,
147 .portd = SAA7164_MPEG_ENCODER,
142 .chiprev = SAA7164_CHIP_REV2, 148 .chiprev = SAA7164_CHIP_REV2,
143 .unit = {{ 149 .unit = {{
144 .id = 0x1d, 150 .id = 0x1d,
@@ -195,6 +201,10 @@ struct saa7164_board saa7164_boards[] = {
195 .name = "Hauppauge WinTV-HVR2250", 201 .name = "Hauppauge WinTV-HVR2250",
196 .porta = SAA7164_MPEG_DVB, 202 .porta = SAA7164_MPEG_DVB,
197 .portb = SAA7164_MPEG_DVB, 203 .portb = SAA7164_MPEG_DVB,
204 .portc = SAA7164_MPEG_ENCODER,
205 .portd = SAA7164_MPEG_ENCODER,
206 .portc = SAA7164_MPEG_ENCODER,
207 .portd = SAA7164_MPEG_ENCODER,
198 .chiprev = SAA7164_CHIP_REV3, 208 .chiprev = SAA7164_CHIP_REV3,
199 .unit = {{ 209 .unit = {{
200 .id = 0x22, 210 .id = 0x22,
@@ -251,6 +261,8 @@ struct saa7164_board saa7164_boards[] = {
251 .name = "Hauppauge WinTV-HVR2250", 261 .name = "Hauppauge WinTV-HVR2250",
252 .porta = SAA7164_MPEG_DVB, 262 .porta = SAA7164_MPEG_DVB,
253 .portb = SAA7164_MPEG_DVB, 263 .portb = SAA7164_MPEG_DVB,
264 .portc = SAA7164_MPEG_ENCODER,
265 .portd = SAA7164_MPEG_ENCODER,
254 .chiprev = SAA7164_CHIP_REV3, 266 .chiprev = SAA7164_CHIP_REV3,
255 .unit = {{ 267 .unit = {{
256 .id = 0x28, 268 .id = 0x28,
@@ -307,6 +319,8 @@ struct saa7164_board saa7164_boards[] = {
307 .name = "Hauppauge WinTV-HVR2250", 319 .name = "Hauppauge WinTV-HVR2250",
308 .porta = SAA7164_MPEG_DVB, 320 .porta = SAA7164_MPEG_DVB,
309 .portb = SAA7164_MPEG_DVB, 321 .portb = SAA7164_MPEG_DVB,
322 .portc = SAA7164_MPEG_ENCODER,
323 .portd = SAA7164_MPEG_ENCODER,
310 .chiprev = SAA7164_CHIP_REV3, 324 .chiprev = SAA7164_CHIP_REV3,
311 .unit = {{ 325 .unit = {{
312 .id = 0x26, 326 .id = 0x26,
diff --git a/drivers/media/video/saa7164/saa7164-core.c b/drivers/media/video/saa7164/saa7164-core.c
index 88c36bd2bd1..b8e56d88743 100644
--- a/drivers/media/video/saa7164/saa7164-core.c
+++ b/drivers/media/video/saa7164/saa7164-core.c
@@ -82,6 +82,69 @@ static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
82 82
83} 83}
84 84
85static irqreturn_t saa7164_irq_encoder(struct saa7164_port *port)
86{
87 struct saa7164_dev *dev = port->dev;
88 struct saa7164_buffer *buf;
89 struct saa7164_user_buffer *ubuf;
90 struct list_head *c, *n;
91 int wp, i = 0, rp;
92
93 /* Find the current write point from the hardware */
94 wp = saa7164_readl(port->bufcounter);
95 if (wp > (port->hwcfg.buffercount - 1))
96 BUG();
97
98 /* Find the previous buffer to the current write point */
99 if (wp == 0)
100 rp = 7;
101 else
102 rp = wp - 1;
103
104 /* Lookup the WP in the buffer list */
105 /* TODO: turn this into a worker thread */
106 list_for_each_safe(c, n, &port->dmaqueue.list) {
107 buf = list_entry(c, struct saa7164_buffer, list);
108 if (i++ > port->hwcfg.buffercount)
109 BUG();
110
111 if (buf->idx == rp) {
112 /* Found the buffer, deal with it */
113 dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
114 __func__, wp, rp);
115
116 /* */
117 /* find a free user buffer and clone to it */
118 if (!list_empty(&port->list_buf_free.list)) {
119
120 /* Pull the first buffer from the used list */
121 ubuf = list_first_entry(&port->list_buf_free.list,
122 struct saa7164_user_buffer, list);
123
124 if (ubuf->actual_size == buf->actual_size)
125 memcpy(ubuf->data, buf->cpu, ubuf->actual_size);
126
127 /* Requeue the buffer on the free list */
128 ubuf->pos = 0;
129
130
131// mutex_lock(&port->dmaqueue_lock);
132 list_move_tail(&ubuf->list, &port->list_buf_used.list);
133// mutex_unlock(&port->dmaqueue_lock);
134
135 /* Flag any userland waiters */
136 wake_up_interruptible(&port->wait_read);
137
138 } else
139 printk(KERN_ERR "encirq no free buffers\n");
140
141 break;
142 }
143
144 }
145 return 0;
146}
147
85static irqreturn_t saa7164_irq_ts(struct saa7164_port *port) 148static irqreturn_t saa7164_irq_ts(struct saa7164_port *port)
86{ 149{
87 struct saa7164_dev *dev = port->dev; 150 struct saa7164_dev *dev = port->dev;
@@ -123,6 +186,11 @@ static irqreturn_t saa7164_irq_ts(struct saa7164_port *port)
123static irqreturn_t saa7164_irq(int irq, void *dev_id) 186static irqreturn_t saa7164_irq(int irq, void *dev_id)
124{ 187{
125 struct saa7164_dev *dev = dev_id; 188 struct saa7164_dev *dev = dev_id;
189 struct saa7164_port *porta = &dev->ports[ SAA7164_PORT_TS1 ];
190 struct saa7164_port *portb = &dev->ports[ SAA7164_PORT_TS2 ];
191 struct saa7164_port *portc = &dev->ports[ SAA7164_PORT_ENC1 ];
192 struct saa7164_port *portd = &dev->ports[ SAA7164_PORT_ENC2 ];
193
126 u32 intid, intstat[INT_SIZE/4]; 194 u32 intid, intstat[INT_SIZE/4];
127 int i, handled = 0, bit; 195 int i, handled = 0, bit;
128 196
@@ -168,17 +236,25 @@ static irqreturn_t saa7164_irq(int irq, void *dev_id)
168 if (intid == dev->intfdesc.bInterruptId) { 236 if (intid == dev->intfdesc.bInterruptId) {
169 /* A response to an cmd/api call */ 237 /* A response to an cmd/api call */
170 schedule_work(&dev->workcmd); 238 schedule_work(&dev->workcmd);
171 } else if (intid == 239 } else if (intid == porta->hwcfg.interruptid) {
172 dev->ts1.hwcfg.interruptid) {
173 240
174 /* Transport path 1 */ 241 /* Transport path 1 */
175 saa7164_irq_ts(&dev->ts1); 242 saa7164_irq_ts(porta);
176 243
177 } else if (intid == 244 } else if (intid == portb->hwcfg.interruptid) {
178 dev->ts2.hwcfg.interruptid) {
179 245
180 /* Transport path 2 */ 246 /* Transport path 2 */
181 saa7164_irq_ts(&dev->ts2); 247 saa7164_irq_ts(portb);
248
249 } else if (intid == portc->hwcfg.interruptid) {
250
251 /* Encoder path 1 */
252 saa7164_irq_encoder(portc);
253
254 } else if (intid == portd->hwcfg.interruptid) {
255
256 /* Encoder path 1 */
257 saa7164_irq_encoder(portd);
182 258
183 } else { 259 } else {
184 /* Find the function */ 260 /* Find the function */
@@ -402,6 +478,37 @@ static int get_resources(struct saa7164_dev *dev)
402 return -EBUSY; 478 return -EBUSY;
403} 479}
404 480
481static int saa7164_port_init(struct saa7164_dev *dev, int portnr)
482{
483 struct saa7164_port *port = 0;
484
485 if ((portnr < 0) || (portnr >= SAA7164_MAX_PORTS))
486 BUG();
487
488 port = &dev->ports[ portnr ];
489
490 port->dev = dev;
491 port->nr = portnr;
492
493 if ((portnr == SAA7164_PORT_TS1) || (portnr == SAA7164_PORT_TS2))
494 port->type = SAA7164_MPEG_DVB;
495 else
496 if ((portnr == SAA7164_PORT_ENC1) || (portnr == SAA7164_PORT_ENC2))
497 port->type = SAA7164_MPEG_ENCODER;
498 else
499 BUG();
500
501 /* Init all the critical resources */
502 mutex_init(&port->dvb.lock);
503 INIT_LIST_HEAD(&port->dmaqueue.list);
504 mutex_init(&port->dmaqueue_lock);
505
506 INIT_LIST_HEAD(&port->list_buf_used.list);
507 INIT_LIST_HEAD(&port->list_buf_free.list);
508 init_waitqueue_head(&port->wait_read);
509 return 0;
510}
511
405static int saa7164_dev_setup(struct saa7164_dev *dev) 512static int saa7164_dev_setup(struct saa7164_dev *dev)
406{ 513{
407 int i; 514 int i;
@@ -443,21 +550,11 @@ static int saa7164_dev_setup(struct saa7164_dev *dev)
443 dev->i2c_bus[2].dev = dev; 550 dev->i2c_bus[2].dev = dev;
444 dev->i2c_bus[2].nr = 2; 551 dev->i2c_bus[2].nr = 2;
445 552
446 /* Transport port A Defaults / setup */ 553 /* Transport + Encoder ports 1, 2, 3, 4 - Defaults / setup */
447 dev->ts1.dev = dev; 554 saa7164_port_init(dev, SAA7164_PORT_TS1);
448 dev->ts1.nr = 0; 555 saa7164_port_init(dev, SAA7164_PORT_TS2);
449 dev->ts1.type = SAA7164_MPEG_UNDEFINED; 556 saa7164_port_init(dev, SAA7164_PORT_ENC1);
450 mutex_init(&dev->ts1.dvb.lock); 557 saa7164_port_init(dev, SAA7164_PORT_ENC2);
451 INIT_LIST_HEAD(&dev->ts1.dmaqueue.list);
452 mutex_init(&dev->ts1.dmaqueue_lock);
453
454 /* Transport port B Defaults / setup */
455 dev->ts2.dev = dev;
456 dev->ts2.nr = 1;
457 dev->ts2.type = SAA7164_MPEG_UNDEFINED;
458 mutex_init(&dev->ts2.dvb.lock);
459 INIT_LIST_HEAD(&dev->ts2.dmaqueue.list);
460 mutex_init(&dev->ts2.dmaqueue_lock);
461 558
462 if (get_resources(dev) < 0) { 559 if (get_resources(dev) < 0) {
463 printk(KERN_ERR "CORE %s No more PCIe resources for " 560 printk(KERN_ERR "CORE %s No more PCIe resources for "
@@ -631,7 +728,7 @@ static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
631 728
632 /* Begin to create the video sub-systems and register funcs */ 729 /* Begin to create the video sub-systems and register funcs */
633 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) { 730 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) {
634 if (saa7164_dvb_register(&dev->ts1) < 0) { 731 if (saa7164_dvb_register(&dev->ports[ SAA7164_PORT_TS1 ]) < 0) {
635 printk(KERN_ERR "%s() Failed to register " 732 printk(KERN_ERR "%s() Failed to register "
636 "dvb adapters on porta\n", 733 "dvb adapters on porta\n",
637 __func__); 734 __func__);
@@ -639,13 +736,27 @@ static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
639 } 736 }
640 737
641 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) { 738 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) {
642 if (saa7164_dvb_register(&dev->ts2) < 0) { 739 if (saa7164_dvb_register(&dev->ports[ SAA7164_PORT_TS2 ]) < 0) {
643 printk(KERN_ERR"%s() Failed to register " 740 printk(KERN_ERR"%s() Failed to register "
644 "dvb adapters on portb\n", 741 "dvb adapters on portb\n",
645 __func__); 742 __func__);
646 } 743 }
647 } 744 }
648 745
746 if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER) {
747 if (saa7164_encoder_register(&dev->ports[ SAA7164_PORT_ENC1 ]) < 0) {
748 printk(KERN_ERR"%s() Failed to register "
749 "mpeg encoder\n", __func__);
750 }
751 }
752
753 if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER) {
754 if (saa7164_encoder_register(&dev->ports[ SAA7164_PORT_ENC2 ]) < 0) {
755 printk(KERN_ERR"%s() Failed to register "
756 "mpeg encoder\n", __func__);
757 }
758 }
759
649 } /* != BOARD_UNKNOWN */ 760 } /* != BOARD_UNKNOWN */
650 else 761 else
651 printk(KERN_ERR "%s() Unsupported board detected, " 762 printk(KERN_ERR "%s() Unsupported board detected, "
@@ -676,10 +787,16 @@ static void __devexit saa7164_finidev(struct pci_dev *pci_dev)
676 saa7164_shutdown(dev); 787 saa7164_shutdown(dev);
677 788
678 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) 789 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
679 saa7164_dvb_unregister(&dev->ts1); 790 saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS1 ]);
680 791
681 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) 792 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
682 saa7164_dvb_unregister(&dev->ts2); 793 saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS2 ]);
794
795 if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER)
796 saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC1 ]);
797
798 if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER)
799 saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC2 ]);
683 800
684 saa7164_i2c_unregister(&dev->i2c_bus[0]); 801 saa7164_i2c_unregister(&dev->i2c_bus[0]);
685 saa7164_i2c_unregister(&dev->i2c_bus[1]); 802 saa7164_i2c_unregister(&dev->i2c_bus[1]);
diff --git a/drivers/media/video/saa7164/saa7164-encoder.c b/drivers/media/video/saa7164/saa7164-encoder.c
index eae0e1b8a03..74aec26dbb2 100644
--- a/drivers/media/video/saa7164/saa7164-encoder.c
+++ b/drivers/media/video/saa7164/saa7164-encoder.c
@@ -21,3 +21,1374 @@
21 21
22#include "saa7164.h" 22#include "saa7164.h"
23 23
24#define ENCODER_MAX_BITRATE 6500000
25#define ENCODER_MIN_BITRATE 1000000
26#define ENCODER_DEF_BITRATE 5000000
27
28static 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
38static 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,
45 V4L2_CID_MPEG_VIDEO_ASPECT,
46 V4L2_CID_MPEG_STREAM_TYPE,
47 V4L2_CID_MPEG_AUDIO_MUTE,
48 V4L2_CID_MPEG_VIDEO_BITRATE,
49 0
50};
51
52/* Take the encoder configuration form the port struct and
53 * flush it to the hardware.
54 */
55static void saa7164_encoder_configure(struct saa7164_port *port)
56{
57 struct saa7164_dev *dev = port->dev;
58 dprintk(DBGLVL_ENC, "%s()\n", __func__);
59
60 port->encoder_params.width = port->width;
61 port->encoder_params.height = port->height;
62 port->encoder_params.is_50hz =
63 (port->encodernorm.id & V4L2_STD_625_50) != 0;
64
65 /* Set up the DIF (enable it) for analog mode by default */
66 saa7164_api_initialize_dif(port);
67
68 /* Configure the correct video standard */
69 saa7164_api_configure_dif(port, port->encodernorm.id);
70
71 /* Ensure the audio decoder is correct configured */
72 saa7164_api_set_audio_std(port);
73}
74
75/* One time configuration at registration time */
76static int saa7164_encoder_initialize(struct saa7164_port *port)
77{
78 struct saa7164_dev *dev = port->dev;
79
80 dprintk(DBGLVL_ENC, "%s()\n", __func__);
81
82 saa7164_encoder_configure(port);
83
84 return 0;
85}
86
87/* -- V4L2 --------------------------------------------------------- */
88static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
89{
90 struct saa7164_fh *fh = file->private_data;
91 struct saa7164_port *port = fh->port;
92 struct saa7164_dev *dev = port->dev;
93 unsigned int i;
94
95 dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)*id);
96
97 for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
98 if (*id & saa7164_tvnorms[i].id)
99 break;
100 }
101 if (i == ARRAY_SIZE(saa7164_tvnorms))
102 return -EINVAL;
103
104 port->encodernorm = saa7164_tvnorms[i];
105
106 /* Update the audio decoder while is not running in
107 * auto detect mode.
108 */
109 saa7164_api_set_audio_std(port);
110
111 dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)*id);
112
113 return 0;
114}
115
116static int vidioc_enum_input(struct file *file, void *priv,
117 struct v4l2_input *i)
118{
119 int n;
120
121 char *inputs[] = { "tuner", "composite", "svideo", "aux",
122 "composite", "svideo", "aux" };
123
124 if (i->index >= 7)
125 return -EINVAL;
126
127 strcpy(i->name, inputs[ i->index ]);
128
129 if (i->index == 0)
130 i->type = V4L2_INPUT_TYPE_TUNER;
131 else
132 i->type = V4L2_INPUT_TYPE_CAMERA;
133
134 for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
135 i->std |= saa7164_tvnorms[n].id;
136
137 return 0;
138}
139
140static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
141{
142 struct saa7164_fh *fh = file->private_data;
143 struct saa7164_port *port = fh->port;
144 struct saa7164_dev *dev = port->dev;
145
146 if (saa7164_api_get_videomux(port) != SAA_OK)
147 return -EIO;
148
149 *i = (port->mux_input - 1);
150
151 dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);
152
153 return 0;
154}
155
156static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
157{
158 struct saa7164_fh *fh = file->private_data;
159 struct saa7164_port *port = fh->port;
160 struct saa7164_dev *dev = port->dev;
161
162 dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);
163
164 if (i >= 7)
165 return -EINVAL;
166
167 port->mux_input = i + 1;
168
169 if (saa7164_api_set_videomux(port) != SAA_OK)
170 return -EIO;
171
172 return 0;
173}
174
175static int vidioc_g_tuner(struct file *file, void *priv,
176 struct v4l2_tuner *t)
177{
178 struct saa7164_fh *fh = file->private_data;
179 struct saa7164_port *port = fh->port;
180 struct saa7164_dev *dev = port->dev;
181
182 if (0 != t->index)
183 return -EINVAL;
184
185 strcpy(t->name, "tuner");
186 t->type = V4L2_TUNER_ANALOG_TV;
187 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
188
189 dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
190
191 return 0;
192}
193
194static int vidioc_s_tuner(struct file *file, void *priv,
195 struct v4l2_tuner *t)
196{
197
198 /* Update the A/V core */
199
200 return 0;
201}
202
203static int vidioc_g_frequency(struct file *file, void *priv,
204 struct v4l2_frequency *f)
205{
206 struct saa7164_fh *fh = file->private_data;
207 struct saa7164_port *port = fh->port;
208
209 f->type = V4L2_TUNER_ANALOG_TV;
210 f->frequency = port->freq;
211
212 return 0;
213}
214
215static int vidioc_s_frequency(struct file *file, void *priv,
216 struct v4l2_frequency *f)
217{
218 struct saa7164_fh *fh = file->private_data;
219 struct saa7164_port *port = fh->port;
220 struct saa7164_dev *dev = port->dev;
221 struct saa7164_port *tsport;
222 struct dvb_frontend *fe;
223
224 /* TODO: Pull this for the std */
225 struct analog_parameters params = {
226 .mode = V4L2_TUNER_ANALOG_TV,
227 .audmode = V4L2_TUNER_MODE_STEREO,
228 .std = port->encodernorm.id,
229 .frequency = f->frequency
230 };
231
232 /* Stop the encoder */
233 dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
234 f->frequency, f->tuner);
235
236 if (f->tuner != 0)
237 return -EINVAL;
238
239 if (f->type != V4L2_TUNER_ANALOG_TV)
240 return -EINVAL;
241
242 port->freq = f->frequency;
243
244 /* Update the hardware */
245 if (port->nr == SAA7164_PORT_ENC1)
246 tsport = &dev->ports[ SAA7164_PORT_TS1 ];
247 else
248 if (port->nr == SAA7164_PORT_ENC2)
249 tsport = &dev->ports[ SAA7164_PORT_TS2 ];
250 else
251 BUG();
252
253 fe = tsport->dvb.frontend;
254
255 if (fe && fe->ops.tuner_ops.set_analog_params)
256 fe->ops.tuner_ops.set_analog_params(fe, &params);
257 else
258 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
259
260 saa7164_encoder_initialize(port);
261
262 return 0;
263}
264
265static int vidioc_g_ctrl(struct file *file, void *priv,
266 struct v4l2_control *ctl)
267{
268 struct saa7164_fh *fh = file->private_data;
269 struct saa7164_port *port = fh->port;
270 struct saa7164_dev *dev = port->dev;
271
272 dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
273 ctl->id, ctl->value);
274
275 switch (ctl->id) {
276 case V4L2_CID_BRIGHTNESS:
277 ctl->value = port->ctl_brightness;
278 break;
279 case V4L2_CID_CONTRAST:
280 ctl->value = port->ctl_contrast;
281 break;
282 case V4L2_CID_SATURATION:
283 ctl->value = port->ctl_saturation;
284 break;
285 case V4L2_CID_HUE:
286 ctl->value = port->ctl_hue;
287 break;
288 case V4L2_CID_SHARPNESS:
289 ctl->value = port->ctl_sharpness;
290 break;
291 case V4L2_CID_AUDIO_VOLUME:
292 ctl->value = port->ctl_volume;
293 break;
294 default:
295 return -EINVAL;
296 }
297
298 return 0;
299}
300
301static int vidioc_s_ctrl(struct file *file, void *priv,
302 struct v4l2_control *ctl)
303{
304 struct saa7164_fh *fh = file->private_data;
305 struct saa7164_port *port = fh->port;
306 struct saa7164_dev *dev = port->dev;
307 int ret = 0;
308
309 dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
310 ctl->id, ctl->value);
311
312 switch (ctl->id) {
313 case V4L2_CID_BRIGHTNESS:
314 if ((ctl->value >= 0) && (ctl->value <= 255)) {
315 port->ctl_brightness = ctl->value;
316 saa7164_api_set_usercontrol(port,
317 PU_BRIGHTNESS_CONTROL);
318 } else
319 ret = -EINVAL;
320 break;
321 case V4L2_CID_CONTRAST:
322 if ((ctl->value >= 0) && (ctl->value <= 255)) {
323 port->ctl_contrast = ctl->value;
324 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
325 } else
326 ret = -EINVAL;
327 break;
328 case V4L2_CID_SATURATION:
329 if ((ctl->value >= 0) && (ctl->value <= 255)) {
330 port->ctl_saturation = ctl->value;
331 saa7164_api_set_usercontrol(port,
332 PU_SATURATION_CONTROL);
333 } else
334 ret = -EINVAL;
335 break;
336 case V4L2_CID_HUE:
337 if ((ctl->value >= 0) && (ctl->value <= 255)) {
338 port->ctl_hue = ctl->value;
339 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
340 } else
341 ret = -EINVAL;
342 break;
343 case V4L2_CID_SHARPNESS:
344 if ((ctl->value >= 0) && (ctl->value <= 255)) {
345 port->ctl_sharpness = ctl->value;
346 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
347 } else
348 ret = -EINVAL;
349 break;
350 case V4L2_CID_AUDIO_VOLUME:
351 if ((ctl->value >= -83) && (ctl->value <= 24)) {
352 port->ctl_volume = ctl->value;
353 saa7164_api_set_audio_volume(port, port->ctl_volume);
354 } else
355 ret = -EINVAL;
356 break;
357 default:
358 ret = -EINVAL;
359 }
360
361 return ret;
362}
363
364static int saa7164_get_ctrl(struct saa7164_port *port,
365 struct v4l2_ext_control *ctrl)
366{
367 struct saa7164_encoder_params *params = &port->encoder_params;
368
369 switch (ctrl->id) {
370 case V4L2_CID_MPEG_VIDEO_BITRATE:
371 ctrl->value = params->bitrate;
372 break;
373 case V4L2_CID_MPEG_STREAM_TYPE:
374 ctrl->value = params->stream_type;
375 break;
376 case V4L2_CID_MPEG_AUDIO_MUTE:
377 ctrl->value = params->ctl_mute;
378 break;
379 case V4L2_CID_MPEG_VIDEO_ASPECT:
380 ctrl->value = params->ctl_aspect;
381 break;
382 default:
383 return -EINVAL;
384 }
385 return 0;
386}
387
388static int vidioc_g_ext_ctrls(struct file *file, void *priv,
389 struct v4l2_ext_controls *ctrls)
390{
391 struct saa7164_fh *fh = file->private_data;
392 struct saa7164_port *port = fh->port;
393 int i, err = 0;
394
395 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
396 for (i = 0; i < ctrls->count; i++) {
397 struct v4l2_ext_control *ctrl = ctrls->controls + i;
398
399 err = saa7164_get_ctrl(port, ctrl);
400 if (err) {
401 ctrls->error_idx = i;
402 break;
403 }
404 }
405 return err;
406
407 }
408
409 return -EINVAL;
410}
411
412static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
413{
414 int ret = -EINVAL;
415
416 switch (ctrl->id) {
417 case V4L2_CID_MPEG_VIDEO_BITRATE:
418 if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
419 (ctrl->value <= ENCODER_MAX_BITRATE))
420 ret = 0;
421 break;
422 case V4L2_CID_MPEG_STREAM_TYPE:
423 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
424 ret = 0;
425 break;
426 case V4L2_CID_MPEG_AUDIO_MUTE:
427 if ((ctrl->value >= 0) &&
428 (ctrl->value <= 1))
429 ret = 0;
430 break;
431 case V4L2_CID_MPEG_VIDEO_ASPECT:
432 if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
433 (ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
434 ret = 0;
435 break;
436 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
437 if ((ctrl->value >= 0) &&
438 (ctrl->value <= 255))
439 ret = 0;
440 break;
441 default:
442 ret = -EINVAL;
443 }
444
445 return ret;
446}
447
448static int vidioc_try_ext_ctrls(struct file *file, void *priv,
449 struct v4l2_ext_controls *ctrls)
450{
451 int i, err = 0;
452
453 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
454 for (i = 0; i < ctrls->count; i++) {
455 struct v4l2_ext_control *ctrl = ctrls->controls + i;
456
457 err = saa7164_try_ctrl(ctrl, 0);
458 if (err) {
459 ctrls->error_idx = i;
460 break;
461 }
462 }
463 return err;
464 }
465
466 return -EINVAL;
467}
468
469static int saa7164_set_ctrl(struct saa7164_port *port,
470 struct v4l2_ext_control *ctrl)
471{
472 struct saa7164_encoder_params *params = &port->encoder_params;
473 int ret = 0;
474
475 switch (ctrl->id) {
476 case V4L2_CID_MPEG_VIDEO_BITRATE:
477 params->bitrate = ctrl->value;
478 break;
479 case V4L2_CID_MPEG_STREAM_TYPE:
480 params->stream_type = ctrl->value;
481 break;
482 case V4L2_CID_MPEG_AUDIO_MUTE:
483 params->ctl_mute = ctrl->value;
484 ret = saa7164_api_audio_mute(port, params->ctl_mute);
485 if (ret != SAA_OK) {
486 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
487 ret);
488 ret = -EIO;
489 }
490 break;
491 case V4L2_CID_MPEG_VIDEO_ASPECT:
492 params->ctl_aspect = ctrl->value;
493 ret = saa7164_api_set_aspect_ratio(port);
494 if (ret != SAA_OK) {
495 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
496 ret);
497 ret = -EIO;
498 }
499 break;
500 default:
501 return -EINVAL;
502 }
503
504 /* TODO: Update the hardware */
505
506 return ret;
507}
508
509static int vidioc_s_ext_ctrls(struct file *file, void *priv,
510 struct v4l2_ext_controls *ctrls)
511{
512 struct saa7164_fh *fh = file->private_data;
513 struct saa7164_port *port = fh->port;
514 int i, err = 0;
515
516 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
517 for (i = 0; i < ctrls->count; i++) {
518 struct v4l2_ext_control *ctrl = ctrls->controls + i;
519
520 err = saa7164_try_ctrl(ctrl, 0);
521 if (err) {
522 ctrls->error_idx = i;
523 break;
524 }
525 err = saa7164_set_ctrl(port, ctrl);
526 if (err) {
527 ctrls->error_idx = i;
528 break;
529 }
530 }
531 return err;
532
533 }
534
535 return -EINVAL;
536}
537
538static int vidioc_querycap(struct file *file, void *priv,
539 struct v4l2_capability *cap)
540{
541 struct saa7164_fh *fh = file->private_data;
542 struct saa7164_port *port = fh->port;
543 struct saa7164_dev *dev = port->dev;
544
545 strcpy(cap->driver, dev->name);
546 strlcpy(cap->card, saa7164_boards[dev->board].name,
547 sizeof(cap->card));
548 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
549
550 cap->capabilities =
551 V4L2_CAP_VIDEO_CAPTURE |
552 V4L2_CAP_READWRITE |
553 V4L2_CAP_STREAMING |
554 0;
555
556 cap->capabilities |= V4L2_CAP_TUNER;
557 cap->version = 0;
558
559 return 0;
560}
561
562static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
563 struct v4l2_fmtdesc *f)
564{
565 if (f->index != 0)
566 return -EINVAL;
567
568 strlcpy(f->description, "MPEG", sizeof(f->description));
569 f->pixelformat = V4L2_PIX_FMT_MPEG;
570
571 return 0;
572}
573
574static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
575 struct v4l2_format *f)
576{
577 struct saa7164_fh *fh = file->private_data;
578 struct saa7164_port *port = fh->port;
579 struct saa7164_dev *dev = port->dev;
580
581 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
582 f->fmt.pix.bytesperline = 0;
583 f->fmt.pix.sizeimage =
584 port->ts_packet_size * port->ts_packet_count;
585 f->fmt.pix.colorspace = 0;
586 f->fmt.pix.width = port->width;
587 f->fmt.pix.height = port->height;
588
589 dprintk(DBGLVL_ENC, "VIDIOC_G_FMT: w: %d, h: %d\n",
590 port->width, port->height);
591
592 return 0;
593}
594
595static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
596 struct v4l2_format *f)
597{
598 struct saa7164_fh *fh = file->private_data;
599 struct saa7164_port *port = fh->port;
600 struct saa7164_dev *dev = port->dev;
601
602 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
603 f->fmt.pix.bytesperline = 0;
604 f->fmt.pix.sizeimage =
605 port->ts_packet_size * port->ts_packet_count;
606 f->fmt.pix.colorspace = 0;
607 dprintk(DBGLVL_ENC, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
608 port->width, port->height);
609 return 0;
610}
611
612static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
613 struct v4l2_format *f)
614{
615 struct saa7164_fh *fh = file->private_data;
616 struct saa7164_port *port = fh->port;
617 struct saa7164_dev *dev = port->dev;
618
619 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
620 f->fmt.pix.bytesperline = 0;
621 f->fmt.pix.sizeimage =
622 port->ts_packet_size * port->ts_packet_count;
623 f->fmt.pix.colorspace = 0;
624
625 dprintk(DBGLVL_ENC, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
626 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
627
628 return 0;
629}
630
631static int vidioc_log_status(struct file *file, void *priv)
632{
633 return 0;
634}
635
636static int fill_queryctrl(struct saa7164_encoder_params *params,
637 struct v4l2_queryctrl *c)
638{
639 switch (c->id) {
640 case V4L2_CID_BRIGHTNESS:
641 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
642 case V4L2_CID_CONTRAST:
643 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
644 case V4L2_CID_SATURATION:
645 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
646 case V4L2_CID_HUE:
647 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
648 case V4L2_CID_SHARPNESS:
649 return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
650 case V4L2_CID_MPEG_AUDIO_MUTE:
651 return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
652 case V4L2_CID_AUDIO_VOLUME:
653 return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
654 case V4L2_CID_MPEG_VIDEO_BITRATE:
655 return v4l2_ctrl_query_fill(c,
656 ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
657 100000, ENCODER_DEF_BITRATE);
658 case V4L2_CID_MPEG_STREAM_TYPE:
659 return v4l2_ctrl_query_fill(c,
660 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
661 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
662 0, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
663 case V4L2_CID_MPEG_VIDEO_ASPECT:
664 return v4l2_ctrl_query_fill(c,
665 V4L2_MPEG_VIDEO_ASPECT_1x1,
666 V4L2_MPEG_VIDEO_ASPECT_221x100,
667 1, V4L2_MPEG_VIDEO_ASPECT_4x3);
668 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
669 return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
670 default:
671 return -EINVAL;
672 }
673}
674
675static int vidioc_queryctrl(struct file *file, void *priv,
676 struct v4l2_queryctrl *c)
677{
678 struct saa7164_fh *fh = priv;
679 struct saa7164_port *port = fh->port;
680 int i, next;
681 u32 id = c->id;
682
683 memset(c, 0, sizeof(*c));
684
685 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
686 c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
687
688 for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
689 if (next) {
690 if (c->id < saa7164_v4l2_ctrls[i])
691 c->id = saa7164_v4l2_ctrls[i];
692 else
693 continue;
694 }
695
696 if (c->id == saa7164_v4l2_ctrls[i])
697 return fill_queryctrl(&port->encoder_params, c);
698
699 if (c->id < saa7164_v4l2_ctrls[i])
700 break;
701 }
702
703 return -EINVAL;
704}
705
706static int saa7164_encoder_stop_port(struct saa7164_port *port)
707{
708 struct saa7164_dev *dev = port->dev;
709 int ret;
710
711 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
712 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
713 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
714 __func__, ret);
715 ret = -EIO;
716 } else {
717 dprintk(DBGLVL_ENC, "%s() Stopped\n", __func__);
718 ret = 0;
719 }
720
721 return ret;
722}
723
724static int saa7164_encoder_acquire_port(struct saa7164_port *port)
725{
726 struct saa7164_dev *dev = port->dev;
727 int ret;
728
729 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
730 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
731 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
732 __func__, ret);
733 ret = -EIO;
734 } else {
735 dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
736 ret = 0;
737 }
738
739 return ret;
740}
741
742static int saa7164_encoder_pause_port(struct saa7164_port *port)
743{
744 struct saa7164_dev *dev = port->dev;
745 int ret;
746
747 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
748 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
749 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
750 __func__, ret);
751 ret = -EIO;
752 } else {
753 dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
754 ret = 0;
755 }
756
757 return ret;
758}
759
760/* Firmware is very windows centric, meaning you have to transition
761 * the part through AVStream / KS Windows stages, forwards or backwards.
762 * States are: stopped, acquired (h/w), paused, started.
763 * We have to leave here will all of the soft buffers on the free list,
764 * else the cfg_post() func won't have soft buffers to correctly configure.
765 */
766static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
767{
768 struct saa7164_dev *dev = port->dev;
769 struct saa7164_buffer *buf;
770 struct saa7164_user_buffer *ubuf;
771 struct list_head *c, *n;
772 int ret;
773
774 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
775
776 ret = saa7164_encoder_pause_port(port);
777 ret = saa7164_encoder_acquire_port(port);
778 ret = saa7164_encoder_stop_port(port);
779
780 dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
781 port->nr);
782
783 mutex_lock(&port->dmaqueue_lock);
784
785 /* Reset the hard and soft buffer state */
786 list_for_each_safe(c, n, &port->dmaqueue.list) {
787 buf = list_entry(c, struct saa7164_buffer, list);
788 buf->flags = SAA7164_BUFFER_FREE;
789 buf->pos = 0;
790 }
791
792 list_for_each_safe(c, n, &port->list_buf_used.list) {
793 ubuf = list_entry(c, struct saa7164_user_buffer, list);
794 ubuf->pos = 0;
795 list_move_tail(&ubuf->list, &port->list_buf_free.list);
796 }
797
798 mutex_unlock(&port->dmaqueue_lock);
799 dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
800
801 return ret;
802}
803
804static int saa7164_encoder_start_streaming(struct saa7164_port *port)
805{
806 struct saa7164_dev *dev = port->dev;
807 int result, ret = 0;
808
809 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
810
811 /* Configure the encoder with any cache values */
812 saa7164_api_set_encoder(port);
813
814 saa7164_buffer_cfg_port(port);
815
816 /* Acquire the hardware */
817 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
818 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
819 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
820 __func__, result);
821
822 /* Stop the hardware, regardless */
823 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
824 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
825 printk(KERN_ERR "%s() acquire/forced stop transition "
826 "failed, res = 0x%x\n", __func__, result);
827 }
828 ret = -EIO;
829 goto out;
830 } else
831 dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
832
833 /* Pause the hardware */
834 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
835 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
836 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
837 __func__, result);
838
839 /* Stop the hardware, regardless */
840 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
841 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
842 printk(KERN_ERR "%s() pause/forced stop transition "
843 "failed, res = 0x%x\n", __func__, result);
844 }
845
846 ret = -EIO;
847 goto out;
848 } else
849 dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
850
851 /* Start the hardware */
852 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
853 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
854 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
855 __func__, result);
856
857 /* Stop the hardware, regardless */
858 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
859 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
860 printk(KERN_ERR "%s() run/forced stop transition "
861 "failed, res = 0x%x\n", __func__, result);
862 }
863
864 ret = -EIO;
865 } else
866 dprintk(DBGLVL_ENC, "%s() Running\n", __func__);
867
868out:
869 return ret;
870}
871
872static int fops_open(struct file *file)
873{
874 struct saa7164_dev *h, *dev = NULL;
875 struct saa7164_port *port = NULL;
876 struct saa7164_port *portc = NULL;
877 struct saa7164_port *portd = NULL;
878 struct saa7164_fh *fh;
879 struct list_head *list;
880 int minor = video_devdata(file)->minor;
881
882 dprintk(DBGLVL_ENC, "%s()\n", __func__);
883
884 /* TODO: Really, the BKL? - remove this */
885 lock_kernel();
886 list_for_each(list, &saa7164_devlist) {
887 h = list_entry(list, struct saa7164_dev, devlist);
888
889 portc = &h->ports[ SAA7164_PORT_ENC1 ];
890 portd = &h->ports[ SAA7164_PORT_ENC2 ];
891
892 if (portc->v4l_device &&
893 portc->v4l_device->minor == minor) {
894 dev = h;
895 port = portc;
896 break;
897 }
898
899 if (portd->v4l_device &&
900 portd->v4l_device->minor == minor) {
901 dev = h;
902 port = portd;
903 break;
904 }
905
906 }
907
908 if (port == NULL) {
909 unlock_kernel();
910 return -ENODEV;
911 }
912
913 /* allocate + initialize per filehandle data */
914 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
915 if (NULL == fh) {
916 unlock_kernel();
917 return -ENOMEM;
918 }
919
920 file->private_data = fh;
921 fh->port = port;
922
923 unlock_kernel();
924
925 return 0;
926}
927
928static int fops_release(struct file *file)
929{
930 struct saa7164_fh *fh = file->private_data;
931 struct saa7164_port *port = fh->port;
932 struct saa7164_dev *dev = port->dev;
933
934 dprintk(DBGLVL_ENC, "%s()\n", __func__);
935
936 /* Shut device down on last close */
937 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
938 if (atomic_dec_return(&port->v4l_reader_count) == 0) {
939 /* stop mpeg capture then cancel buffers */
940 saa7164_encoder_stop_streaming(port);
941 }
942 }
943
944 file->private_data = NULL;
945 kfree(fh);
946
947 return 0;
948}
949
950struct saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
951{
952 struct saa7164_user_buffer *buf = 0;
953 struct saa7164_dev *dev = port->dev;
954
955 mutex_lock(&port->dmaqueue_lock);
956 if (!list_empty(&port->list_buf_used.list)) {
957 buf = list_first_entry(&port->list_buf_used.list,
958 struct saa7164_user_buffer, list);
959 }
960 mutex_unlock(&port->dmaqueue_lock);
961
962 dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, buf);
963
964 return buf;
965}
966
967static ssize_t fops_read(struct file *file, char __user *buffer,
968 size_t count, loff_t *pos)
969{
970 struct saa7164_fh *fh = file->private_data;
971 struct saa7164_port *port = fh->port;
972 struct saa7164_user_buffer *ubuf = NULL;
973 struct saa7164_dev *dev = port->dev;
974 unsigned int ret = 0;
975 int rem, cnt;
976 u8 *p;
977
978 if (*pos)
979 return -ESPIPE;
980
981 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
982 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
983
984 if (saa7164_encoder_initialize(port) < 0)
985 return -EINVAL;
986
987 saa7164_encoder_start_streaming(port);
988 msleep(200);
989 }
990 }
991
992 /* blocking wait for buffer */
993 if ((file->f_flags & O_NONBLOCK) == 0) {
994 if (wait_event_interruptible(port->wait_read,
995 saa7164_enc_next_buf(port))) {
996 return -ERESTARTSYS;
997 }
998 }
999
1000 /* Pull the first buffer from the used list */
1001 ubuf = saa7164_enc_next_buf(port);
1002
1003 while ((count > 0) && ubuf) {
1004
1005 /* set remaining bytes to copy */
1006 rem = ubuf->actual_size - ubuf->pos;
1007 cnt = rem > count ? count : rem;
1008
1009 p = ubuf->data + ubuf->pos;
1010
1011 dprintk(DBGLVL_ENC,
1012 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1013 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1014
1015 if (copy_to_user(buffer, p, cnt)) {
1016 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1017 if (!ret)
1018 ret = -EFAULT;
1019 goto err;
1020 }
1021
1022 ubuf->pos += cnt;
1023 count -= cnt;
1024 buffer += cnt;
1025 ret += cnt;
1026
1027 if (ubuf->pos == ubuf->actual_size) {
1028
1029 /* finished with current buffer, take next buffer */
1030
1031 /* Requeue the buffer on the free list */
1032 ubuf->pos = 0;
1033
1034 mutex_lock(&port->dmaqueue_lock);
1035 list_move_tail(&ubuf->list, &port->list_buf_free.list);
1036 mutex_unlock(&port->dmaqueue_lock);
1037
1038 /* Dequeue next */
1039 if ((file->f_flags & O_NONBLOCK) == 0) {
1040 if (wait_event_interruptible(port->wait_read,
1041 saa7164_enc_next_buf(port))) {
1042 break;
1043 }
1044 }
1045 ubuf = saa7164_enc_next_buf(port);
1046 }
1047 }
1048err:
1049 if (!ret && !ubuf)
1050 ret = -EAGAIN;
1051
1052 return ret;
1053}
1054
1055static unsigned int fops_poll(struct file *file, poll_table *wait)
1056{
1057 struct saa7164_fh *fh = (struct saa7164_fh *)file->private_data;
1058 struct saa7164_port *port = fh->port;
1059 struct saa7164_user_buffer *ubuf;
1060 unsigned int mask = 0;
1061
1062 if (!video_is_registered(port->v4l_device)) {
1063 return -EIO;
1064 }
1065
1066 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1067 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1068 if (saa7164_encoder_initialize(port) < 0)
1069 return -EINVAL;
1070 saa7164_encoder_start_streaming(port);
1071 msleep(200);
1072 }
1073 }
1074
1075 /* blocking wait for buffer */
1076 if ((file->f_flags & O_NONBLOCK) == 0) {
1077 if (wait_event_interruptible(port->wait_read,
1078 saa7164_enc_next_buf(port))) {
1079 return -ERESTARTSYS;
1080 }
1081 }
1082
1083 /* Pull the first buffer from the used list */
1084 ubuf = list_first_entry(&port->list_buf_used.list,
1085 struct saa7164_user_buffer, list);
1086
1087 if (ubuf)
1088 mask |= POLLIN | POLLRDNORM;
1089
1090 return mask;
1091}
1092
1093static const struct v4l2_file_operations mpeg_fops = {
1094 .owner = THIS_MODULE,
1095 .open = fops_open,
1096 .release = fops_release,
1097 .read = fops_read,
1098 .poll = fops_poll,
1099 .unlocked_ioctl = video_ioctl2,
1100};
1101
1102int saa7164_g_chip_ident(struct file *file, void *fh,
1103 struct v4l2_dbg_chip_ident *chip)
1104{
1105 struct saa7164_port *port = ((struct saa7164_fh *)fh)->port;
1106 struct saa7164_dev *dev = port->dev;
1107 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1108
1109 return 0;
1110}
1111
1112int saa7164_g_register(struct file *file, void *fh,
1113 struct v4l2_dbg_register *reg)
1114{
1115 struct saa7164_port *port = ((struct saa7164_fh *)fh)->port;
1116 struct saa7164_dev *dev = port->dev;
1117 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1118
1119 if (!capable(CAP_SYS_ADMIN))
1120 return -EPERM;
1121
1122 return 0;
1123}
1124
1125int saa7164_s_register(struct file *file, void *fh,
1126 struct v4l2_dbg_register *reg)
1127{
1128 struct saa7164_port *port = ((struct saa7164_fh *)fh)->port;
1129 struct saa7164_dev *dev = port->dev;
1130 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1131
1132 if (!capable(CAP_SYS_ADMIN))
1133 return -EPERM;
1134
1135 return 0;
1136}
1137
1138static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1139 .vidioc_s_std = vidioc_s_std,
1140 .vidioc_enum_input = vidioc_enum_input,
1141 .vidioc_g_input = vidioc_g_input,
1142 .vidioc_s_input = vidioc_s_input,
1143 .vidioc_g_tuner = vidioc_g_tuner,
1144 .vidioc_s_tuner = vidioc_s_tuner,
1145 .vidioc_g_frequency = vidioc_g_frequency,
1146 .vidioc_s_frequency = vidioc_s_frequency,
1147 .vidioc_s_ctrl = vidioc_s_ctrl,
1148 .vidioc_g_ctrl = vidioc_g_ctrl,
1149 .vidioc_querycap = vidioc_querycap,
1150 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1151 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1152 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1153 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1154 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1155 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1156 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1157 .vidioc_log_status = vidioc_log_status,
1158 .vidioc_queryctrl = vidioc_queryctrl,
1159 .vidioc_g_chip_ident = saa7164_g_chip_ident,
1160#ifdef CONFIG_VIDEO_ADV_DEBUG
1161 .vidioc_g_register = saa7164_g_register,
1162 .vidioc_s_register = saa7164_s_register,
1163#endif
1164};
1165
1166static struct video_device saa7164_mpeg_template = {
1167 .name = "saa7164",
1168 .fops = &mpeg_fops,
1169 .ioctl_ops = &mpeg_ioctl_ops,
1170 .minor = -1,
1171 .tvnorms = SAA7164_NORMS,
1172 .current_norm = V4L2_STD_NTSC_M,
1173};
1174
1175static struct video_device *saa7164_encoder_alloc(
1176 struct saa7164_port *port,
1177 struct pci_dev *pci,
1178 struct video_device *template,
1179 char *type)
1180{
1181 struct video_device *vfd;
1182 struct saa7164_dev *dev = port->dev;
1183
1184 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1185
1186 vfd = video_device_alloc();
1187 if (NULL == vfd)
1188 return NULL;
1189
1190 *vfd = *template;
1191 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1192 type, saa7164_boards[dev->board].name);
1193
1194 vfd->parent = &pci->dev;
1195 vfd->release = video_device_release;
1196 return vfd;
1197}
1198
1199int saa7164_encoder_register(struct saa7164_port *port)
1200{
1201 struct saa7164_dev *dev = port->dev;
1202 struct saa7164_buffer *buf;
1203 struct saa7164_user_buffer *ubuf;
1204 int result = -ENODEV, i;
1205 int len = 0;
1206
1207 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1208
1209 if (port->type != SAA7164_MPEG_ENCODER)
1210 BUG();
1211
1212 /* Sanity check that the PCI configuration space is active */
1213 if (port->hwcfg.BARLocation == 0) {
1214 printk(KERN_ERR "%s() failed "
1215 "(errno = %d), NO PCI configuration\n",
1216 __func__, result);
1217 result = -ENOMEM;
1218 goto failed;
1219 }
1220
1221 /* Init and establish defaults */
1222 /* TODO: Check the umber of lines for PS */
1223 port->hw_streamingparams.bitspersample = 8;
1224 port->hw_streamingparams.samplesperline = 188;
1225 port->hw_streamingparams.numberoflines =
1226 (SAA7164_TS_NUMBER_OF_LINES * 188) / 188;
1227
1228 port->hw_streamingparams.pitch = 188;
1229 port->hw_streamingparams.linethreshold = 0;
1230 port->hw_streamingparams.pagetablelistvirt = 0;
1231 port->hw_streamingparams.pagetablelistphys = 0;
1232 port->hw_streamingparams.numpagetables = 2 +
1233 ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
1234
1235 port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount;
1236
1237 /* Allocate the PCI resources, buffers (hard) */
1238 for (i = 0; i < port->hwcfg.buffercount; i++) {
1239 buf = saa7164_buffer_alloc(port,
1240 port->hw_streamingparams.numberoflines *
1241 port->hw_streamingparams.pitch);
1242
1243 if (!buf) {
1244 printk(KERN_ERR "%s() failed "
1245 "(errno = %d), unable to allocate buffer\n",
1246 __func__, result);
1247 result = -ENOMEM;
1248 goto failed;
1249 } else {
1250
1251 mutex_lock(&port->dmaqueue_lock);
1252 list_add_tail(&buf->list, &port->dmaqueue.list);
1253 mutex_unlock(&port->dmaqueue_lock);
1254
1255 }
1256 }
1257
1258 /* Allocate some kenrel kernel buffers for copying
1259 * to userpsace.
1260 */
1261 len = port->hw_streamingparams.numberoflines *
1262 port->hw_streamingparams.pitch;
1263
1264 for (i = 0; i < SAA7164_MAX_ENCODER_BUFFERS; i++) {
1265
1266 ubuf = saa7164_buffer_alloc_user(dev, len);
1267 if (ubuf) {
1268 mutex_lock(&port->dmaqueue_lock);
1269 list_add_tail(&ubuf->list, &port->list_buf_free.list);
1270 mutex_unlock(&port->dmaqueue_lock);
1271 }
1272
1273 }
1274
1275 /* Establish encoder defaults here */
1276 /* Set default TV standard */
1277 port->encodernorm = saa7164_tvnorms[0];
1278 port->width = 720;
1279 port->mux_input = 1; /* Composite */
1280 port->encoder_profile = EU_PROFILE_PS_DVD;
1281 port->video_format = EU_VIDEO_FORMAT_MPEG_2;
1282 port->audio_format = 0;
1283 port->video_resolution = 0;
1284 port->ctl_brightness = 127;
1285 port->ctl_contrast = 66;
1286 port->ctl_hue = 128;
1287 port->ctl_saturation = 62;
1288 port->ctl_sharpness = 8;
1289 port->encoder_params.bitrate = ENCODER_DEF_BITRATE;
1290 port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1291 port->encoder_params.ctl_mute = 0;
1292 port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
1293
1294 if (port->encodernorm.id & V4L2_STD_525_60)
1295 port->height = 480;
1296 else
1297 port->height = 576;
1298
1299 /* Allocate and register the video device node */
1300 port->v4l_device = saa7164_encoder_alloc(port,
1301 dev->pci, &saa7164_mpeg_template, "mpeg");
1302
1303 if (port->v4l_device == NULL) {
1304 printk(KERN_INFO "%s: can't allocate mpeg device\n",
1305 dev->name);
1306 result = -ENOMEM;
1307 goto failed;
1308 }
1309
1310 result = video_register_device(port->v4l_device,
1311 VFL_TYPE_GRABBER, -1);
1312 if (result < 0) {
1313 printk(KERN_INFO "%s: can't register mpeg device\n",
1314 dev->name);
1315 /* TODO: We're going to leak here if we don't dealloc
1316 The buffers above. The unreg function can't deal wit it.
1317 */
1318 goto failed;
1319 }
1320
1321 printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
1322 dev->name, port->v4l_device->num);
1323
1324 /* Configure the hardware defaults */
1325 saa7164_api_set_videomux(port);
1326 saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
1327 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
1328 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
1329 saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
1330 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
1331 saa7164_api_audio_mute(port, 0);
1332 saa7164_api_set_audio_volume(port, 20);
1333 saa7164_api_set_aspect_ratio(port);
1334
1335 /* Disable audio standard detection, it's buggy */
1336 saa7164_api_set_audio_detection(port, 0);
1337
1338 saa7164_api_set_encoder(port);
1339 saa7164_api_get_encoder(port);
1340
1341 result = 0;
1342failed:
1343 return result;
1344}
1345
1346void saa7164_encoder_unregister(struct saa7164_port *port)
1347{
1348 struct saa7164_dev *dev = port->dev;
1349 struct saa7164_buffer *buf;
1350 struct saa7164_user_buffer *ubuf;
1351 struct list_head *c, *n, *p, *q, *l, *v;
1352
1353 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1354
1355 if (port->type != SAA7164_MPEG_ENCODER)
1356 BUG();
1357
1358 if (port->v4l_device) {
1359 if (port->v4l_device->minor != -1)
1360 video_unregister_device(port->v4l_device);
1361 else
1362 video_device_release(port->v4l_device);
1363
1364 port->v4l_device = NULL;
1365 }
1366
1367 /* Remove any allocated buffers */
1368 mutex_lock(&port->dmaqueue_lock);
1369
1370 dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
1371 list_for_each_safe(c, n, &port->dmaqueue.list) {
1372 buf = list_entry(c, struct saa7164_buffer, list);
1373 list_del(c);
1374 saa7164_buffer_dealloc(buf);
1375 }
1376
1377 dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
1378 list_for_each_safe(p, q, &port->list_buf_used.list) {
1379 ubuf = list_entry(p, struct saa7164_user_buffer, list);
1380 list_del(p);
1381 saa7164_buffer_dealloc_user(ubuf);
1382 }
1383
1384 dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
1385 list_for_each_safe(l, v, &port->list_buf_free.list) {
1386 ubuf = list_entry(l, struct saa7164_user_buffer, list);
1387 list_del(l);
1388 saa7164_buffer_dealloc_user(ubuf);
1389 }
1390
1391 mutex_unlock(&port->dmaqueue_lock);
1392 dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
1393}
1394
diff --git a/drivers/media/video/saa7164/saa7164.h b/drivers/media/video/saa7164/saa7164.h
index 1ca9bf2e342..ad390724402 100644
--- a/drivers/media/video/saa7164/saa7164.h
+++ b/drivers/media/video/saa7164/saa7164.h
@@ -48,18 +48,26 @@
48#include <linux/i2c.h> 48#include <linux/i2c.h>
49#include <linux/i2c-algo-bit.h> 49#include <linux/i2c-algo-bit.h>
50#include <linux/kdev_t.h> 50#include <linux/kdev_t.h>
51#include <linux/version.h>
52#include <linux/mutex.h>
51 53
52#include <media/tuner.h> 54#include <media/tuner.h>
53#include <media/tveeprom.h> 55#include <media/tveeprom.h>
54#include <media/videobuf-dma-sg.h> 56#include <media/videobuf-dma-sg.h>
55#include <media/videobuf-dvb.h> 57#include <media/videobuf-dvb.h>
58#include <linux/smp_lock.h>
59#include <dvb_demux.h>
60#include <dvb_frontend.h>
61#include <dvb_net.h>
62#include <dvbdev.h>
63#include <dmxdev.h>
64#include <media/v4l2-common.h>
65#include <media/v4l2-ioctl.h>
66#include <media/v4l2-chip-ident.h>
56 67
57#include "saa7164-reg.h" 68#include "saa7164-reg.h"
58#include "saa7164-types.h" 69#include "saa7164-types.h"
59 70
60#include <linux/version.h>
61#include <linux/mutex.h>
62
63#define SAA7164_MAXBOARDS 8 71#define SAA7164_MAXBOARDS 8
64 72
65#define UNSET (-1U) 73#define UNSET (-1U)
@@ -77,6 +85,14 @@
77#define SAA7164_MAX_UNITS 8 85#define SAA7164_MAX_UNITS 8
78#define SAA7164_TS_NUMBER_OF_LINES 312 86#define SAA7164_TS_NUMBER_OF_LINES 312
79#define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */ 87#define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */
88#define SAA7164_MAX_ENCODER_BUFFERS 16
89
90/* Port related defines */
91#define SAA7164_PORT_TS1 (0)
92#define SAA7164_PORT_TS2 (SAA7164_PORT_TS1 + 1)
93#define SAA7164_PORT_ENC1 (SAA7164_PORT_TS2 + 1)
94#define SAA7164_PORT_ENC2 (SAA7164_PORT_ENC1 + 1)
95#define SAA7164_MAX_PORTS (SAA7164_PORT_ENC2 + 1)
80 96
81#define DBGLVL_FW 4 97#define DBGLVL_FW 4
82#define DBGLVL_DVB 8 98#define DBGLVL_DVB 8
@@ -88,6 +104,8 @@
88#define DBGLVL_BUF 512 104#define DBGLVL_BUF 512
89#define DBGLVL_ENC 1024 105#define DBGLVL_ENC 1024
90 106
107#define SAA7164_NORMS ( V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_443 )
108
91enum port_t { 109enum port_t {
92 SAA7164_MPEG_UNDEFINED = 0, 110 SAA7164_MPEG_UNDEFINED = 0,
93 SAA7164_MPEG_DVB, 111 SAA7164_MPEG_DVB,
@@ -136,7 +154,7 @@ struct saa7164_unit {
136 154
137struct saa7164_board { 155struct saa7164_board {
138 char *name; 156 char *name;
139 enum port_t porta, portb; 157 enum port_t porta, portb, portc, portd;
140 enum { 158 enum {
141 SAA7164_CHIP_UNDEFINED = 0, 159 SAA7164_CHIP_UNDEFINED = 0,
142 SAA7164_CHIP_REV2, 160 SAA7164_CHIP_REV2,
@@ -151,6 +169,22 @@ struct saa7164_subid {
151 u32 card; 169 u32 card;
152}; 170};
153 171
172struct saa7164_fh {
173 struct saa7164_port *port;
174 u32 freq;
175 u32 tuner_type;
176 atomic_t v4l_reading;
177};
178
179struct saa7164_user_buffer {
180 struct list_head list;
181
182 /* Attributes */
183 u8 *data;
184 u32 pos;
185 u32 actual_size;
186};
187
154struct saa7164_fw_status { 188struct saa7164_fw_status {
155 189
156 /* RISC Core details */ 190 /* RISC Core details */
@@ -193,6 +227,30 @@ struct saa7164_i2c {
193 u32 i2c_rc; 227 u32 i2c_rc;
194}; 228};
195 229
230struct saa7164_ctrl {
231 struct v4l2_queryctrl v;
232};
233
234struct saa7164_tvnorm {
235 char *name;
236 v4l2_std_id id;
237// u32 cxiformat;
238// u32 cxoformat;
239};
240
241struct saa7164_encoder_params {
242 struct saa7164_tvnorm encodernorm;
243 u32 height;
244 u32 width;
245 u32 is_50hz;
246 u32 bitrate; /* bps */
247 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
248
249 u32 audio_sampling_freq;
250 u32 ctl_mute;
251 u32 ctl_aspect;
252};
253
196struct saa7164_port; 254struct saa7164_port;
197 255
198struct saa7164_buffer { 256struct saa7164_buffer {
@@ -254,7 +312,42 @@ struct saa7164_port {
254 struct saa7164_dvb dvb; 312 struct saa7164_dvb dvb;
255 313
256 /* --- Encoder/V4L related attributes --- */ 314 /* --- Encoder/V4L related attributes --- */
257 315 /* Encoder */
316 /* Defaults established in saa7164-encoder.c */
317 struct saa7164_tvnorm encodernorm;
318 u32 height;
319 u32 width;
320 u32 freq;
321 u32 ts_packet_size;
322 u32 ts_packet_count;
323 u8 mux_input;
324 u8 encoder_profile;
325 u8 video_format;
326 u8 audio_format;
327 u8 video_resolution;
328 u16 ctl_brightness;
329 u16 ctl_contrast;
330 u16 ctl_hue;
331 u16 ctl_saturation;
332 u16 ctl_sharpness;
333 s8 ctl_volume;
334
335 tmComResAFeatureDescrHeader_t audfeat;
336 tmComResEncoderDescrHeader_t encunit;
337 tmComResProcDescrHeader_t vidproc;
338 tmComResExtDevDescrHeader_t ifunit;
339 tmComResTunerDescrHeader_t tunerunit;
340
341 /* V4L */
342 struct saa7164_encoder_params encoder_params;
343 struct video_device *v4l_device;
344 atomic_t v4l_reader_count;
345// spinlock_t slock;
346// struct mutex fops_lock;
347
348 struct saa7164_buffer list_buf_used;
349 struct saa7164_buffer list_buf_free;
350 wait_queue_head_t wait_read;
258}; 351};
259 352
260struct saa7164_dev { 353struct saa7164_dev {
@@ -297,7 +390,7 @@ struct saa7164_dev {
297 struct saa7164_i2c i2c_bus[3]; 390 struct saa7164_i2c i2c_bus[3];
298 391
299 /* Transport related */ 392 /* Transport related */
300 struct saa7164_port ts1, ts2; 393 struct saa7164_port ports[ SAA7164_MAX_PORTS ];
301 394
302 /* Deferred command/api interrupts handling */ 395 /* Deferred command/api interrupts handling */
303 struct work_struct workcmd; 396 struct work_struct workcmd;
@@ -355,6 +448,19 @@ int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen);
355int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin); 448int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
356int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin); 449int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
357int saa7164_api_transition_port(struct saa7164_port *port, u8 mode); 450int saa7164_api_transition_port(struct saa7164_port *port, u8 mode);
451int saa7164_api_initialize_dif(struct saa7164_port *port);
452int saa7164_api_configure_dif(struct saa7164_port *port, u32 std);
453int saa7164_api_set_encoder(struct saa7164_port *port);
454int saa7164_api_get_encoder(struct saa7164_port *port);
455int saa7164_api_set_aspect_ratio(struct saa7164_port *port);
456int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl);
457int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl);
458int saa7164_api_set_videomux(struct saa7164_port *port);
459int saa7164_api_audio_mute(struct saa7164_port *port, int mute);
460int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level);
461int saa7164_api_set_audio_std(struct saa7164_port *port);
462int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect);
463int saa7164_api_get_videomux(struct saa7164_port *port);
358 464
359/* ----------------------------------------------------------- */ 465/* ----------------------------------------------------------- */
360/* saa7164-cards.c */ 466/* saa7164-cards.c */
@@ -385,6 +491,15 @@ extern int saa7164_buffer_dealloc(struct saa7164_buffer *buf);
385extern void saa7164_buffer_display(struct saa7164_buffer *buf); 491extern void saa7164_buffer_display(struct saa7164_buffer *buf);
386extern int saa7164_buffer_activate(struct saa7164_buffer *buf, int i); 492extern int saa7164_buffer_activate(struct saa7164_buffer *buf, int i);
387extern int saa7164_buffer_cfg_port(struct saa7164_port *port); 493extern int saa7164_buffer_cfg_port(struct saa7164_port *port);
494extern struct saa7164_user_buffer *saa7164_buffer_alloc_user(
495 struct saa7164_dev *dev, u32 len);
496extern void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf);
497
498
499/* ----------------------------------------------------------- */
500/* saa7164-encoder.c */
501int saa7164_encoder_register(struct saa7164_port *port);
502void saa7164_encoder_unregister(struct saa7164_port *port);
388 503
389/* ----------------------------------------------------------- */ 504/* ----------------------------------------------------------- */
390 505