2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
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.
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
15 * GNU General Public License for more details.
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.
22 #include <linux/wait.h>
23 #include <linux/slab.h>
27 int saa7164_api_get_load_info(struct saa7164_dev *dev, struct tmFwInfoStruct *i)
31 if (!(saa_debug & DBGLVL_CPU))
34 dprintk(DBGLVL_API, "%s()\n", __func__);
41 ret = saa7164_cmd_send(dev, 0, GET_CUR,
42 GET_FW_STATUS_CONTROL, sizeof(struct tmFwInfoStruct), i);
44 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
47 printk(KERN_INFO "saa7164[%d]-CPU: %d percent", dev->nr, i->CPULoad);
52 int saa7164_api_collect_debug(struct saa7164_dev *dev)
54 struct tmComResDebugGetData d;
58 dprintk(DBGLVL_API, "%s()\n", __func__);
62 memset(&d, 0, sizeof(d));
64 ret = saa7164_cmd_send(dev, 0, GET_CUR,
65 GET_DEBUG_DATA_CONTROL, sizeof(d), &d);
67 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
70 if (d.dwResult != SAA_OK)
73 printk(KERN_INFO "saa7164[%d]-FWMSG: %s", dev->nr, d.ucDebugData);
79 int saa7164_api_set_debug(struct saa7164_dev *dev, u8 level)
81 struct tmComResDebugSetLevel lvl;
84 dprintk(DBGLVL_API, "%s(level=%d)\n", __func__, level);
86 /* Retrieve current state */
87 ret = saa7164_cmd_send(dev, 0, GET_CUR,
88 SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
90 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
92 dprintk(DBGLVL_API, "%s() Was %d\n", __func__, lvl.dwDebugLevel);
94 lvl.dwDebugLevel = level;
97 ret = saa7164_cmd_send(dev, 0, SET_CUR,
98 SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
100 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
106 int saa7164_api_set_vbi_format(struct saa7164_port *port)
108 struct saa7164_dev *dev = port->dev;
109 struct tmComResProbeCommit fmt, rsp;
112 dprintk(DBGLVL_API, "%s(nr=%d, unitid=0x%x)\n", __func__,
113 port->nr, port->hwcfg.unitid);
116 fmt.bFormatIndex = 1;
119 /* Probe, see if it can support this format */
120 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
121 SET_CUR, SAA_PROBE_CONTROL, sizeof(fmt), &fmt);
123 printk(KERN_ERR "%s() set error, ret = 0x%x\n", __func__, ret);
125 /* See of the format change was successful */
126 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
127 GET_CUR, SAA_PROBE_CONTROL, sizeof(rsp), &rsp);
129 printk(KERN_ERR "%s() get error, ret = 0x%x\n", __func__, ret);
131 /* Compare requested vs received, should be same */
132 if (memcmp(&fmt, &rsp, sizeof(rsp)) == 0) {
133 dprintk(DBGLVL_API, "SET/PROBE Verified\n");
135 /* Ask the device to select the negotiated format */
136 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
137 SET_CUR, SAA_COMMIT_CONTROL, sizeof(fmt), &fmt);
139 printk(KERN_ERR "%s() commit error, ret = 0x%x\n",
142 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
143 GET_CUR, SAA_COMMIT_CONTROL, sizeof(rsp), &rsp);
145 printk(KERN_ERR "%s() GET commit error, ret = 0x%x\n",
148 if (memcmp(&fmt, &rsp, sizeof(rsp)) != 0) {
149 printk(KERN_ERR "%s() memcmp error, ret = 0x%x\n",
152 dprintk(DBGLVL_API, "SET/COMMIT Verified\n");
154 dprintk(DBGLVL_API, "rsp.bmHint = 0x%x\n", rsp.bmHint);
155 dprintk(DBGLVL_API, "rsp.bFormatIndex = 0x%x\n", rsp.bFormatIndex);
156 dprintk(DBGLVL_API, "rsp.bFrameIndex = 0x%x\n", rsp.bFrameIndex);
158 printk(KERN_ERR "%s() compare failed\n", __func__);
162 dprintk(DBGLVL_API, "%s(nr=%d) Success\n", __func__, port->nr);
167 int saa7164_api_set_gop_size(struct saa7164_port *port)
169 struct saa7164_dev *dev = port->dev;
170 struct tmComResEncVideoGopStructure gs;
173 dprintk(DBGLVL_ENC, "%s()\n", __func__);
175 gs.ucRefFrameDist = port->encoder_params.refdist;
176 gs.ucGOPSize = port->encoder_params.gop_size;
177 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
178 EU_VIDEO_GOP_STRUCTURE_CONTROL,
181 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
186 int saa7164_api_set_encoder(struct saa7164_port *port)
188 struct saa7164_dev *dev = port->dev;
189 struct tmComResEncVideoBitRate vb;
190 struct tmComResEncAudioBitRate ab;
193 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
194 port->hwcfg.sourceid);
196 if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
197 port->encoder_profile = EU_PROFILE_PS_DVD;
199 port->encoder_profile = EU_PROFILE_TS_HQ;
201 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
202 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
204 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
207 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
208 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
210 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
212 /* Establish video bitrates */
213 if (port->encoder_params.bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
214 vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_CONSTANT;
216 vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_VARIABLE_PEAK;
217 vb.dwVideoBitRate = port->encoder_params.bitrate;
218 vb.dwVideoBitRatePeak = port->encoder_params.bitrate_peak;
219 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
220 EU_VIDEO_BIT_RATE_CONTROL, sizeof(struct tmComResEncVideoBitRate), &vb);
222 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
224 /* Establish audio bitrates */
225 ab.ucAudioBitRateMode = 0;
226 ab.dwAudioBitRate = 384000;
227 ab.dwAudioBitRatePeak = ab.dwAudioBitRate;
228 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
229 EU_AUDIO_BIT_RATE_CONTROL, sizeof(struct tmComResEncAudioBitRate), &ab);
231 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
233 saa7164_api_set_aspect_ratio(port);
234 saa7164_api_set_gop_size(port);
239 int saa7164_api_get_encoder(struct saa7164_port *port)
241 struct saa7164_dev *dev = port->dev;
242 struct tmComResEncVideoBitRate v;
243 struct tmComResEncAudioBitRate a;
244 struct tmComResEncVideoInputAspectRatio ar;
247 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__, port->hwcfg.sourceid);
249 port->encoder_profile = 0;
250 port->video_format = 0;
251 port->video_resolution = 0;
252 port->audio_format = 0;
254 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
255 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
257 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
259 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
260 EU_VIDEO_RESOLUTION_CONTROL, sizeof(u8), &port->video_resolution);
262 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
264 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
265 EU_VIDEO_FORMAT_CONTROL, sizeof(u8), &port->video_format);
267 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
269 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
270 EU_VIDEO_BIT_RATE_CONTROL, sizeof(v), &v);
272 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
274 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
275 EU_AUDIO_FORMAT_CONTROL, sizeof(u8), &port->audio_format);
277 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
279 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
280 EU_AUDIO_BIT_RATE_CONTROL, sizeof(a), &a);
282 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
287 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
288 EU_VIDEO_INPUT_ASPECT_CONTROL,
289 sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
291 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
293 dprintk(DBGLVL_ENC, "encoder_profile = %d\n", port->encoder_profile);
294 dprintk(DBGLVL_ENC, "video_format = %d\n", port->video_format);
295 dprintk(DBGLVL_ENC, "audio_format = %d\n", port->audio_format);
296 dprintk(DBGLVL_ENC, "video_resolution= %d\n", port->video_resolution);
297 dprintk(DBGLVL_ENC, "v.ucVideoBitRateMode = %d\n", v.ucVideoBitRateMode);
298 dprintk(DBGLVL_ENC, "v.dwVideoBitRate = %d\n", v.dwVideoBitRate);
299 dprintk(DBGLVL_ENC, "v.dwVideoBitRatePeak = %d\n", v.dwVideoBitRatePeak);
300 dprintk(DBGLVL_ENC, "a.ucVideoBitRateMode = %d\n", a.ucAudioBitRateMode);
301 dprintk(DBGLVL_ENC, "a.dwVideoBitRate = %d\n", a.dwAudioBitRate);
302 dprintk(DBGLVL_ENC, "a.dwVideoBitRatePeak = %d\n", a.dwAudioBitRatePeak);
303 dprintk(DBGLVL_ENC, "aspect.width / height = %d:%d\n", ar.width, ar.height);
308 int saa7164_api_set_aspect_ratio(struct saa7164_port *port)
310 struct saa7164_dev *dev = port->dev;
311 struct tmComResEncVideoInputAspectRatio ar;
314 dprintk(DBGLVL_ENC, "%s(%d)\n", __func__,
315 port->encoder_params.ctl_aspect);
317 switch (port->encoder_params.ctl_aspect) {
318 case V4L2_MPEG_VIDEO_ASPECT_1x1:
322 case V4L2_MPEG_VIDEO_ASPECT_4x3:
326 case V4L2_MPEG_VIDEO_ASPECT_16x9:
330 case V4L2_MPEG_VIDEO_ASPECT_221x100:
338 dprintk(DBGLVL_ENC, "%s(%d) now %d:%d\n", __func__,
339 port->encoder_params.ctl_aspect,
340 ar.width, ar.height);
343 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
344 EU_VIDEO_INPUT_ASPECT_CONTROL,
345 sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
347 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
352 int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl)
354 struct saa7164_dev *dev = port->dev;
358 if (ctl == PU_BRIGHTNESS_CONTROL)
359 val = port->ctl_brightness;
361 if (ctl == PU_CONTRAST_CONTROL)
362 val = port->ctl_contrast;
364 if (ctl == PU_HUE_CONTROL)
367 if (ctl == PU_SATURATION_CONTROL)
368 val = port->ctl_saturation;
370 if (ctl == PU_SHARPNESS_CONTROL)
371 val = port->ctl_sharpness;
375 dprintk(DBGLVL_ENC, "%s() unitid=0x%x ctl=%d, val=%d\n",
376 __func__, port->encunit.vsourceid, ctl, val);
378 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, SET_CUR,
379 ctl, sizeof(u16), &val);
381 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
386 int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl)
388 struct saa7164_dev *dev = port->dev;
392 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, GET_CUR,
393 ctl, sizeof(u16), &val);
395 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
399 dprintk(DBGLVL_ENC, "%s() ctl=%d, val=%d\n",
402 if (ctl == PU_BRIGHTNESS_CONTROL)
403 port->ctl_brightness = val;
405 if (ctl == PU_CONTRAST_CONTROL)
406 port->ctl_contrast = val;
408 if (ctl == PU_HUE_CONTROL)
411 if (ctl == PU_SATURATION_CONTROL)
412 port->ctl_saturation = val;
414 if (ctl == PU_SHARPNESS_CONTROL)
415 port->ctl_sharpness = val;
420 int saa7164_api_set_videomux(struct saa7164_port *port)
422 struct saa7164_dev *dev = port->dev;
423 u8 inputs[] = { 1, 2, 2, 2, 5, 5, 5 };
426 dprintk(DBGLVL_ENC, "%s() v_mux=%d a_mux=%d\n",
427 __func__, port->mux_input, inputs[port->mux_input - 1]);
430 ret = saa7164_api_audio_mute(port, 1);
432 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
435 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, SET_CUR,
436 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
438 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
441 ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
442 SU_INPUT_SELECT_CONTROL, sizeof(u8), &inputs[port->mux_input - 1]);
444 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
447 ret = saa7164_api_audio_mute(port, 0);
449 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
454 int saa7164_api_audio_mute(struct saa7164_port *port, int mute)
456 struct saa7164_dev *dev = port->dev;
460 dprintk(DBGLVL_API, "%s(%d)\n", __func__, mute);
462 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
463 MUTE_CONTROL, sizeof(u8), &v);
465 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
470 /* 0 = silence, 0xff = full */
471 int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level)
473 struct saa7164_dev *dev = port->dev;
477 dprintk(DBGLVL_API, "%s(%d)\n", __func__, level);
479 /* Obtain the min/max ranges */
480 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MIN,
481 VOLUME_CONTROL, sizeof(u16), &min);
483 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
485 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MAX,
486 VOLUME_CONTROL, sizeof(u16), &max);
488 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
490 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
491 (0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
493 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
495 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
504 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
505 (0x01 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
507 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
510 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
511 (0x02 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
513 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
515 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
516 (0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
518 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
520 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
525 int saa7164_api_set_audio_std(struct saa7164_port *port)
527 struct saa7164_dev *dev = port->dev;
528 struct tmComResAudioDefaults lvl;
529 struct tmComResTunerStandard tvaudio;
532 dprintk(DBGLVL_API, "%s()\n", __func__);
534 /* Establish default levels */
535 lvl.ucDecoderLevel = TMHW_LEV_ADJ_DECLEV_DEFAULT;
536 lvl.ucDecoderFM_Level = TMHW_LEV_ADJ_DECLEV_DEFAULT;
537 lvl.ucMonoLevel = TMHW_LEV_ADJ_MONOLEV_DEFAULT;
538 lvl.ucNICAM_Level = TMHW_LEV_ADJ_NICLEV_DEFAULT;
539 lvl.ucSAP_Level = TMHW_LEV_ADJ_SAPLEV_DEFAULT;
540 lvl.ucADC_Level = TMHW_LEV_ADJ_ADCLEV_DEFAULT;
541 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
542 AUDIO_DEFAULT_CONTROL, sizeof(struct tmComResAudioDefaults), &lvl);
544 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
546 /* Manually select the appropriate TV audio standard */
547 if (port->encodernorm.id & V4L2_STD_NTSC) {
548 tvaudio.std = TU_STANDARD_NTSC_M;
551 tvaudio.std = TU_STANDARD_PAL_I;
552 tvaudio.country = 44;
555 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
556 TU_STANDARD_CONTROL, sizeof(tvaudio), &tvaudio);
558 printk(KERN_ERR "%s() TU_STANDARD_CONTROL error, ret = 0x%x\n", __func__, ret);
562 int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect)
564 struct saa7164_dev *dev = port->dev;
565 struct tmComResTunerStandardAuto p;
568 dprintk(DBGLVL_API, "%s(%d)\n", __func__, autodetect);
570 /* Disable TV Audio autodetect if not already set (buggy) */
572 p.mode = TU_STANDARD_AUTO;
574 p.mode = TU_STANDARD_MANUAL;
575 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
576 TU_STANDARD_AUTO_CONTROL, sizeof(p), &p);
578 printk(KERN_ERR "%s() TU_STANDARD_AUTO_CONTROL error, ret = 0x%x\n", __func__, ret);
583 int saa7164_api_get_videomux(struct saa7164_port *port)
585 struct saa7164_dev *dev = port->dev;
588 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, GET_CUR,
589 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
591 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
593 dprintk(DBGLVL_ENC, "%s() v_mux=%d\n",
594 __func__, port->mux_input);
599 int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
601 struct saa7164_dev *dev = port->dev;
608 dprintk(DBGLVL_API, "%s(nr=%d type=%d val=%x)\n", __func__,
609 port->nr, port->type, val);
616 memset(buf, 0, sizeof(buf));
638 ret = saa7164_cmd_send(dev, port->ifunit.unitid, GET_LEN,
639 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
641 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
645 ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
646 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
648 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
650 //saa7164_dumphex16(dev, buf, 16);
652 return ret == SAA_OK ? 0 : -EIO;
655 /* Disable the IF block AGC controls */
656 int saa7164_api_configure_dif(struct saa7164_port *port, u32 std)
658 struct saa7164_dev *dev = port->dev;
662 dprintk(DBGLVL_API, "%s(nr=%d, 0x%x)\n", __func__, port->nr, std);
664 if (std & V4L2_STD_NTSC) {
665 dprintk(DBGLVL_API, " NTSC\n");
666 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
668 } else if (std & V4L2_STD_PAL_I) {
669 dprintk(DBGLVL_API, " PAL-I\n");
670 saa7164_api_set_dif(port, 0x00, 0x08); /* Video Standard */
672 } else if (std & V4L2_STD_PAL_M) {
673 dprintk(DBGLVL_API, " PAL-M\n");
674 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
676 } else if (std & V4L2_STD_PAL_N) {
677 dprintk(DBGLVL_API, " PAL-N\n");
678 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
680 } else if (std & V4L2_STD_PAL_Nc) {
681 dprintk(DBGLVL_API, " PAL-Nc\n");
682 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
684 } else if (std & V4L2_STD_PAL_B) {
685 dprintk(DBGLVL_API, " PAL-B\n");
686 saa7164_api_set_dif(port, 0x00, 0x02); /* Video Standard */
688 } else if (std & V4L2_STD_PAL_DK) {
689 dprintk(DBGLVL_API, " PAL-DK\n");
690 saa7164_api_set_dif(port, 0x00, 0x10); /* Video Standard */
692 } else if (std & V4L2_STD_SECAM_L) {
693 dprintk(DBGLVL_API, " SECAM-L\n");
694 saa7164_api_set_dif(port, 0x00, 0x20); /* Video Standard */
697 /* Unknown standard, assume DTV */
698 dprintk(DBGLVL_API, " Unknown (assuming DTV)\n");
699 saa7164_api_set_dif(port, 0x00, 0x80); /* Undefined Video Standard */
703 saa7164_api_set_dif(port, 0x48, 0xa0); /* AGC Functions 1 */
704 saa7164_api_set_dif(port, 0xc0, agc_disable); /* AGC Output Disable */
705 saa7164_api_set_dif(port, 0x7c, 0x04); /* CVBS EQ */
706 saa7164_api_set_dif(port, 0x04, 0x01); /* Active */
708 saa7164_api_set_dif(port, 0x04, 0x00); /* Active (again) */
714 /* Ensure the dif is in the correct state for the operating mode
715 * (analog / dtv). We only configure the diff through the analog encoder
716 * so when we're in digital mode we need to find the appropriate encoder
717 * and use it to configure the DIF.
719 int saa7164_api_initialize_dif(struct saa7164_port *port)
721 struct saa7164_dev *dev = port->dev;
722 struct saa7164_port *p = 0;
726 dprintk(DBGLVL_API, "%s(nr=%d type=%d)\n", __func__,
727 port->nr, port->type);
729 if (port->type == SAA7164_MPEG_ENCODER) {
730 /* Pick any analog standard to init the diff.
731 * we'll come back during encoder_init'
732 * and set the correct standard if requried.
736 if (port->type == SAA7164_MPEG_DVB) {
737 if (port->nr == SAA7164_PORT_TS1)
738 p = &dev->ports[SAA7164_PORT_ENC1];
740 p = &dev->ports[SAA7164_PORT_ENC2];
742 if (port->type == SAA7164_MPEG_VBI) {
744 if (port->nr == SAA7164_PORT_VBI1)
745 p = &dev->ports[SAA7164_PORT_ENC1];
747 p = &dev->ports[SAA7164_PORT_ENC2];
752 ret = saa7164_api_configure_dif(p, std);
757 int saa7164_api_transition_port(struct saa7164_port *port, u8 mode)
759 struct saa7164_dev *dev = port->dev;
763 dprintk(DBGLVL_API, "%s(nr=%d unitid=0x%x,%d)\n",
764 __func__, port->nr, port->hwcfg.unitid, mode);
766 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid, SET_CUR,
767 SAA_STATE_CONTROL, sizeof(mode), &mode);
769 printk(KERN_ERR "%s(portnr %d unitid 0x%x) error, ret = 0x%x\n",
770 __func__, port->nr, port->hwcfg.unitid, ret);
775 int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version)
779 ret = saa7164_cmd_send(dev, 0, GET_CUR,
780 GET_FW_VERSION_CONTROL, sizeof(u32), version);
782 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
787 int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen)
789 u8 reg[] = { 0x0f, 0x00 };
794 /* Assumption: Hauppauge eeprom is at 0xa0 on on bus 0 */
795 /* TODO: Pull the details from the boards struct */
796 return saa7164_api_i2c_read(&dev->i2c_bus[0], 0xa0 >> 1, sizeof(reg),
800 int saa7164_api_configure_port_vbi(struct saa7164_dev *dev,
801 struct saa7164_port *port)
803 struct tmComResVBIFormatDescrHeader *fmt = &port->vbi_fmt_ntsc;
805 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
806 dprintk(DBGLVL_API, " VideoStandard = 0x%x\n", fmt->VideoStandard);
807 dprintk(DBGLVL_API, " StartLine = %d\n", fmt->StartLine);
808 dprintk(DBGLVL_API, " EndLine = %d\n", fmt->EndLine);
809 dprintk(DBGLVL_API, " FieldRate = %d\n", fmt->FieldRate);
810 dprintk(DBGLVL_API, " bNumLines = %d\n", fmt->bNumLines);
812 /* Cache the hardware configuration in the port */
814 port->bufcounter = port->hwcfg.BARLocation;
815 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
816 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
817 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
818 port->bufptr32l = port->hwcfg.BARLocation +
820 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
821 port->bufptr32h = port->hwcfg.BARLocation +
823 (sizeof(u32) * port->hwcfg.buffercount);
824 port->bufptr64 = port->hwcfg.BARLocation +
826 (sizeof(u32) * port->hwcfg.buffercount);
827 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
828 port->hwcfg.BARLocation);
830 dprintk(DBGLVL_API, " = VS_FORMAT_VBI (becomes dev->en[%d])\n",
836 int saa7164_api_configure_port_mpeg2ts(struct saa7164_dev *dev,
837 struct saa7164_port *port,
838 struct tmComResTSFormatDescrHeader *tsfmt)
840 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", tsfmt->bFormatIndex);
841 dprintk(DBGLVL_API, " bDataOffset = 0x%x\n", tsfmt->bDataOffset);
842 dprintk(DBGLVL_API, " bPacketLength= 0x%x\n", tsfmt->bPacketLength);
843 dprintk(DBGLVL_API, " bStrideLength= 0x%x\n", tsfmt->bStrideLength);
844 dprintk(DBGLVL_API, " bguid = (....)\n");
846 /* Cache the hardware configuration in the port */
848 port->bufcounter = port->hwcfg.BARLocation;
849 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
850 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
851 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
852 port->bufptr32l = port->hwcfg.BARLocation +
854 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
855 port->bufptr32h = port->hwcfg.BARLocation +
857 (sizeof(u32) * port->hwcfg.buffercount);
858 port->bufptr64 = port->hwcfg.BARLocation +
860 (sizeof(u32) * port->hwcfg.buffercount);
861 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
862 port->hwcfg.BARLocation);
864 dprintk(DBGLVL_API, " = VS_FORMAT_MPEGTS (becomes dev->ts[%d])\n",
870 int saa7164_api_configure_port_mpeg2ps(struct saa7164_dev *dev,
871 struct saa7164_port *port,
872 struct tmComResPSFormatDescrHeader *fmt)
874 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
875 dprintk(DBGLVL_API, " wPacketLength= 0x%x\n", fmt->wPacketLength);
876 dprintk(DBGLVL_API, " wPackLength= 0x%x\n", fmt->wPackLength);
877 dprintk(DBGLVL_API, " bPackDataType= 0x%x\n", fmt->bPackDataType);
879 /* Cache the hardware configuration in the port */
880 /* TODO: CHECK THIS in the port config */
881 port->bufcounter = port->hwcfg.BARLocation;
882 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
883 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
884 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
885 port->bufptr32l = port->hwcfg.BARLocation +
887 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
888 port->bufptr32h = port->hwcfg.BARLocation +
890 (sizeof(u32) * port->hwcfg.buffercount);
891 port->bufptr64 = port->hwcfg.BARLocation +
893 (sizeof(u32) * port->hwcfg.buffercount);
894 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
895 port->hwcfg.BARLocation);
897 dprintk(DBGLVL_API, " = VS_FORMAT_MPEGPS (becomes dev->enc[%d])\n",
903 int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
905 struct saa7164_port *tsport = 0;
906 struct saa7164_port *encport = 0;
907 struct saa7164_port *vbiport = 0;
908 u32 idx, next_offset;
910 struct tmComResDescrHeader *hdr, *t;
911 struct tmComResExtDevDescrHeader *exthdr;
912 struct tmComResPathDescrHeader *pathhdr;
913 struct tmComResAntTermDescrHeader *anttermhdr;
914 struct tmComResTunerDescrHeader *tunerunithdr;
915 struct tmComResDMATermDescrHeader *vcoutputtermhdr;
916 struct tmComResTSFormatDescrHeader *tsfmt;
917 struct tmComResPSFormatDescrHeader *psfmt;
918 struct tmComResSelDescrHeader *psel;
919 struct tmComResProcDescrHeader *pdh;
920 struct tmComResAFeatureDescrHeader *afd;
921 struct tmComResEncoderDescrHeader *edh;
922 struct tmComResVBIFormatDescrHeader *vbifmt;
926 "%s(?,?,%d) sizeof(struct tmComResDescrHeader) = %d bytes\n",
927 __func__, len, (u32)sizeof(struct tmComResDescrHeader));
929 for (idx = 0; idx < (len - sizeof(struct tmComResDescrHeader));) {
931 hdr = (struct tmComResDescrHeader *)(buf + idx);
933 if (hdr->type != CS_INTERFACE)
934 return SAA_ERR_NOT_SUPPORTED;
936 dprintk(DBGLVL_API, "@ 0x%x = \n", idx);
937 switch (hdr->subtype) {
938 case GENERAL_REQUEST:
939 dprintk(DBGLVL_API, " GENERAL_REQUEST\n");
942 dprintk(DBGLVL_API, " VC_TUNER_PATH\n");
943 pathhdr = (struct tmComResPathDescrHeader *)(buf + idx);
944 dprintk(DBGLVL_API, " pathid = 0x%x\n",
946 currpath = pathhdr->pathid;
948 case VC_INPUT_TERMINAL:
949 dprintk(DBGLVL_API, " VC_INPUT_TERMINAL\n");
951 (struct tmComResAntTermDescrHeader *)(buf + idx);
952 dprintk(DBGLVL_API, " terminalid = 0x%x\n",
953 anttermhdr->terminalid);
954 dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
955 anttermhdr->terminaltype);
956 switch (anttermhdr->terminaltype) {
958 dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
961 dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
963 case SPDIF_CONNECTOR:
964 dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
966 case COMPOSITE_CONNECTOR:
968 " = COMPOSITE_CONNECTOR\n");
970 case SVIDEO_CONNECTOR:
971 dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
973 case COMPONENT_CONNECTOR:
975 " = COMPONENT_CONNECTOR\n");
978 dprintk(DBGLVL_API, " = STANDARD_DMA\n");
981 dprintk(DBGLVL_API, " = undefined (0x%x)\n",
982 anttermhdr->terminaltype);
984 dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
985 anttermhdr->assocterminal);
986 dprintk(DBGLVL_API, " iterminal = 0x%x\n",
987 anttermhdr->iterminal);
988 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
989 anttermhdr->controlsize);
991 case VC_OUTPUT_TERMINAL:
992 dprintk(DBGLVL_API, " VC_OUTPUT_TERMINAL\n");
994 (struct tmComResDMATermDescrHeader *)(buf + idx);
995 dprintk(DBGLVL_API, " unitid = 0x%x\n",
996 vcoutputtermhdr->unitid);
997 dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
998 vcoutputtermhdr->terminaltype);
999 switch (vcoutputtermhdr->terminaltype) {
1001 dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
1003 case LINE_CONNECTOR:
1004 dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
1006 case SPDIF_CONNECTOR:
1007 dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
1009 case COMPOSITE_CONNECTOR:
1011 " = COMPOSITE_CONNECTOR\n");
1013 case SVIDEO_CONNECTOR:
1014 dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
1016 case COMPONENT_CONNECTOR:
1018 " = COMPONENT_CONNECTOR\n");
1021 dprintk(DBGLVL_API, " = STANDARD_DMA\n");
1024 dprintk(DBGLVL_API, " = undefined (0x%x)\n",
1025 vcoutputtermhdr->terminaltype);
1027 dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
1028 vcoutputtermhdr->assocterminal);
1029 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1030 vcoutputtermhdr->sourceid);
1031 dprintk(DBGLVL_API, " iterminal = 0x%x\n",
1032 vcoutputtermhdr->iterminal);
1033 dprintk(DBGLVL_API, " BARLocation = 0x%x\n",
1034 vcoutputtermhdr->BARLocation);
1035 dprintk(DBGLVL_API, " flags = 0x%x\n",
1036 vcoutputtermhdr->flags);
1037 dprintk(DBGLVL_API, " interruptid = 0x%x\n",
1038 vcoutputtermhdr->interruptid);
1039 dprintk(DBGLVL_API, " buffercount = 0x%x\n",
1040 vcoutputtermhdr->buffercount);
1041 dprintk(DBGLVL_API, " metadatasize = 0x%x\n",
1042 vcoutputtermhdr->metadatasize);
1043 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1044 vcoutputtermhdr->controlsize);
1045 dprintk(DBGLVL_API, " numformats = 0x%x\n",
1046 vcoutputtermhdr->numformats);
1048 t = (struct tmComResDescrHeader *)
1049 ((struct tmComResDMATermDescrHeader *)(buf + idx));
1050 next_offset = idx + (vcoutputtermhdr->len);
1051 for (i = 0; i < vcoutputtermhdr->numformats; i++) {
1052 t = (struct tmComResDescrHeader *)
1053 (buf + next_offset);
1054 switch (t->subtype) {
1055 case VS_FORMAT_MPEG2TS:
1057 (struct tmComResTSFormatDescrHeader *)t;
1059 tsport = &dev->ports[SAA7164_PORT_TS1];
1061 tsport = &dev->ports[SAA7164_PORT_TS2];
1062 memcpy(&tsport->hwcfg, vcoutputtermhdr,
1063 sizeof(*vcoutputtermhdr));
1064 saa7164_api_configure_port_mpeg2ts(dev,
1067 case VS_FORMAT_MPEG2PS:
1069 (struct tmComResPSFormatDescrHeader *)t;
1071 encport = &dev->ports[SAA7164_PORT_ENC1];
1073 encport = &dev->ports[SAA7164_PORT_ENC2];
1074 memcpy(&encport->hwcfg, vcoutputtermhdr,
1075 sizeof(*vcoutputtermhdr));
1076 saa7164_api_configure_port_mpeg2ps(dev,
1081 (struct tmComResVBIFormatDescrHeader *)t;
1083 vbiport = &dev->ports[SAA7164_PORT_VBI1];
1085 vbiport = &dev->ports[SAA7164_PORT_VBI2];
1086 memcpy(&vbiport->hwcfg, vcoutputtermhdr,
1087 sizeof(*vcoutputtermhdr));
1088 memcpy(&vbiport->vbi_fmt_ntsc, vbifmt, sizeof(*vbifmt));
1089 saa7164_api_configure_port_vbi(dev,
1094 " = VS_FORMAT_RDS\n");
1096 case VS_FORMAT_UNCOMPRESSED:
1098 " = VS_FORMAT_UNCOMPRESSED\n");
1100 case VS_FORMAT_TYPE:
1102 " = VS_FORMAT_TYPE\n");
1106 " = undefined (0x%x)\n",
1109 next_offset += t->len;
1114 dprintk(DBGLVL_API, " TUNER_UNIT\n");
1116 (struct tmComResTunerDescrHeader *)(buf + idx);
1117 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1118 tunerunithdr->unitid);
1119 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1120 tunerunithdr->sourceid);
1121 dprintk(DBGLVL_API, " iunit = 0x%x\n",
1122 tunerunithdr->iunit);
1123 dprintk(DBGLVL_API, " tuningstandards = 0x%x\n",
1124 tunerunithdr->tuningstandards);
1125 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1126 tunerunithdr->controlsize);
1127 dprintk(DBGLVL_API, " controls = 0x%x\n",
1128 tunerunithdr->controls);
1130 if (tunerunithdr->unitid == tunerunithdr->iunit) {
1132 encport = &dev->ports[SAA7164_PORT_ENC1];
1134 encport = &dev->ports[SAA7164_PORT_ENC2];
1135 memcpy(&encport->tunerunit, tunerunithdr,
1136 sizeof(struct tmComResTunerDescrHeader));
1137 dprintk(DBGLVL_API, " (becomes dev->enc[%d] tuner)\n", encport->nr);
1140 case VC_SELECTOR_UNIT:
1141 psel = (struct tmComResSelDescrHeader *)(buf + idx);
1142 dprintk(DBGLVL_API, " VC_SELECTOR_UNIT\n");
1143 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1145 dprintk(DBGLVL_API, " nrinpins = 0x%x\n",
1147 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1150 case VC_PROCESSING_UNIT:
1151 pdh = (struct tmComResProcDescrHeader *)(buf + idx);
1152 dprintk(DBGLVL_API, " VC_PROCESSING_UNIT\n");
1153 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1155 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1157 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1159 if (pdh->controlsize == 0x04) {
1161 encport = &dev->ports[SAA7164_PORT_ENC1];
1163 encport = &dev->ports[SAA7164_PORT_ENC2];
1164 memcpy(&encport->vidproc, pdh,
1165 sizeof(struct tmComResProcDescrHeader));
1166 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
1170 afd = (struct tmComResAFeatureDescrHeader *)(buf + idx);
1171 dprintk(DBGLVL_API, " FEATURE_UNIT\n");
1172 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1174 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1176 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1179 encport = &dev->ports[SAA7164_PORT_ENC1];
1181 encport = &dev->ports[SAA7164_PORT_ENC2];
1182 memcpy(&encport->audfeat, afd,
1183 sizeof(struct tmComResAFeatureDescrHeader));
1184 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
1187 edh = (struct tmComResEncoderDescrHeader *)(buf + idx);
1188 dprintk(DBGLVL_API, " ENCODER_UNIT\n");
1189 dprintk(DBGLVL_API, " subtype = 0x%x\n", edh->subtype);
1190 dprintk(DBGLVL_API, " unitid = 0x%x\n", edh->unitid);
1191 dprintk(DBGLVL_API, " vsourceid = 0x%x\n", edh->vsourceid);
1192 dprintk(DBGLVL_API, " asourceid = 0x%x\n", edh->asourceid);
1193 dprintk(DBGLVL_API, " iunit = 0x%x\n", edh->iunit);
1194 if (edh->iunit == edh->unitid) {
1196 encport = &dev->ports[SAA7164_PORT_ENC1];
1198 encport = &dev->ports[SAA7164_PORT_ENC2];
1199 memcpy(&encport->encunit, edh,
1200 sizeof(struct tmComResEncoderDescrHeader));
1201 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
1204 case EXTENSION_UNIT:
1205 dprintk(DBGLVL_API, " EXTENSION_UNIT\n");
1206 exthdr = (struct tmComResExtDevDescrHeader *)(buf + idx);
1207 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1209 dprintk(DBGLVL_API, " deviceid = 0x%x\n",
1211 dprintk(DBGLVL_API, " devicetype = 0x%x\n",
1212 exthdr->devicetype);
1213 if (exthdr->devicetype & 0x1)
1214 dprintk(DBGLVL_API, " = Decoder Device\n");
1215 if (exthdr->devicetype & 0x2)
1216 dprintk(DBGLVL_API, " = GPIO Source\n");
1217 if (exthdr->devicetype & 0x4)
1218 dprintk(DBGLVL_API, " = Video Decoder\n");
1219 if (exthdr->devicetype & 0x8)
1220 dprintk(DBGLVL_API, " = Audio Decoder\n");
1221 if (exthdr->devicetype & 0x20)
1222 dprintk(DBGLVL_API, " = Crossbar\n");
1223 if (exthdr->devicetype & 0x40)
1224 dprintk(DBGLVL_API, " = Tuner\n");
1225 if (exthdr->devicetype & 0x80)
1226 dprintk(DBGLVL_API, " = IF PLL\n");
1227 if (exthdr->devicetype & 0x100)
1228 dprintk(DBGLVL_API, " = Demodulator\n");
1229 if (exthdr->devicetype & 0x200)
1230 dprintk(DBGLVL_API, " = RDS Decoder\n");
1231 if (exthdr->devicetype & 0x400)
1232 dprintk(DBGLVL_API, " = Encoder\n");
1233 if (exthdr->devicetype & 0x800)
1234 dprintk(DBGLVL_API, " = IR Decoder\n");
1235 if (exthdr->devicetype & 0x1000)
1236 dprintk(DBGLVL_API, " = EEPROM\n");
1237 if (exthdr->devicetype & 0x2000)
1239 " = VBI Decoder\n");
1240 if (exthdr->devicetype & 0x10000)
1242 " = Streaming Device\n");
1243 if (exthdr->devicetype & 0x20000)
1246 if (exthdr->devicetype & 0x40000000)
1248 " = Generic Device\n");
1249 if (exthdr->devicetype & 0x80000000)
1251 " = Config Space Device\n");
1252 dprintk(DBGLVL_API, " numgpiopins = 0x%x\n",
1253 exthdr->numgpiopins);
1254 dprintk(DBGLVL_API, " numgpiogroups = 0x%x\n",
1255 exthdr->numgpiogroups);
1256 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1257 exthdr->controlsize);
1258 if (exthdr->devicetype & 0x80) {
1260 encport = &dev->ports[SAA7164_PORT_ENC1];
1262 encport = &dev->ports[SAA7164_PORT_ENC2];
1263 memcpy(&encport->ifunit, exthdr,
1264 sizeof(struct tmComResExtDevDescrHeader));
1265 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
1268 case PVC_INFRARED_UNIT:
1269 dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n");
1272 dprintk(DBGLVL_API, " DRM_UNIT\n");
1275 dprintk(DBGLVL_API, "default %d\n", hdr->subtype);
1278 dprintk(DBGLVL_API, " 1.%x\n", hdr->len);
1279 dprintk(DBGLVL_API, " 2.%x\n", hdr->type);
1280 dprintk(DBGLVL_API, " 3.%x\n", hdr->subtype);
1281 dprintk(DBGLVL_API, " 4.%x\n", hdr->unitid);
1289 int saa7164_api_enum_subdevs(struct saa7164_dev *dev)
1295 dprintk(DBGLVL_API, "%s()\n", __func__);
1297 /* Get the total descriptor length */
1298 ret = saa7164_cmd_send(dev, 0, GET_LEN,
1299 GET_DESCRIPTORS_CONTROL, sizeof(buflen), &buflen);
1301 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1303 dprintk(DBGLVL_API, "%s() total descriptor size = %d bytes.\n",
1306 /* Allocate enough storage for all of the descs */
1307 buf = kzalloc(buflen, GFP_KERNEL);
1309 return SAA_ERR_NO_RESOURCES;
1312 ret = saa7164_cmd_send(dev, 0, GET_CUR,
1313 GET_DESCRIPTORS_CONTROL, buflen, buf);
1314 if (ret != SAA_OK) {
1315 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1319 if (saa_debug & DBGLVL_API)
1320 saa7164_dumphex16(dev, buf, (buflen/16)*16);
1322 saa7164_api_dump_subdevs(dev, buf, buflen);
1329 int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
1330 u32 datalen, u8 *data)
1332 struct saa7164_dev *dev = bus->dev;
1339 dprintk(DBGLVL_API, "%s()\n", __func__);
1348 regval = ((*(reg) << 8) || *(reg+1));
1351 regval = ((*(reg) << 16) | (*(reg+1) << 8) | *(reg+2));
1354 regval = ((*(reg) << 24) | (*(reg+1) << 16) |
1355 (*(reg+2) << 8) | *(reg+3));
1357 /* Prepare the send buffer */
1358 /* Bytes 00-03 source register length
1359 * 04-07 source bytes to read
1360 * 08... register address
1362 memset(buf, 0, sizeof(buf));
1363 memcpy((buf + 2 * sizeof(u32) + 0), reg, reglen);
1364 *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1365 *((u32 *)(buf + 1 * sizeof(u32))) = datalen;
1367 unitid = saa7164_i2caddr_to_unitid(bus, addr);
1370 "%s() error, cannot translate regaddr 0x%x to unitid\n",
1375 ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1376 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1377 if (ret != SAA_OK) {
1378 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1382 dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1384 if (saa_debug & DBGLVL_I2C)
1385 saa7164_dumphex16(dev, buf, 2 * 16);
1387 ret = saa7164_cmd_send(bus->dev, unitid, GET_CUR,
1388 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1390 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1392 if (saa_debug & DBGLVL_I2C)
1393 saa7164_dumphex16(dev, buf, sizeof(buf));
1394 memcpy(data, (buf + 2 * sizeof(u32) + reglen), datalen);
1397 return ret == SAA_OK ? 0 : -EIO;
1400 /* For a given 8 bit i2c address device, write the buffer */
1401 int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr, u32 datalen,
1404 struct saa7164_dev *dev = bus->dev;
1411 dprintk(DBGLVL_API, "%s()\n", __func__);
1413 if ((datalen == 0) || (datalen > 232))
1416 memset(buf, 0, sizeof(buf));
1418 unitid = saa7164_i2caddr_to_unitid(bus, addr);
1421 "%s() error, cannot translate regaddr 0x%x to unitid\n",
1426 reglen = saa7164_i2caddr_to_reglen(bus, addr);
1429 "%s() error, cannot translate regaddr to reglen\n",
1434 ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1435 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1436 if (ret != SAA_OK) {
1437 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1441 dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1443 /* Prepare the send buffer */
1444 /* Bytes 00-03 dest register length
1445 * 04-07 dest bytes to write
1446 * 08... register address
1448 *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1449 *((u32 *)(buf + 1 * sizeof(u32))) = datalen - reglen;
1450 memcpy((buf + 2 * sizeof(u32)), data, datalen);
1452 if (saa_debug & DBGLVL_I2C)
1453 saa7164_dumphex16(dev, buf, sizeof(buf));
1455 ret = saa7164_cmd_send(bus->dev, unitid, SET_CUR,
1456 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1458 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1460 return ret == SAA_OK ? 0 : -EIO;
1463 int saa7164_api_modify_gpio(struct saa7164_dev *dev, u8 unitid,
1467 struct tmComResGPIO t;
1469 dprintk(DBGLVL_API, "%s(0x%x, %d, %d)\n",
1470 __func__, unitid, pin, state);
1472 if ((pin > 7) || (state > 2))
1473 return SAA_ERR_BAD_PARAMETER;
1478 ret = saa7164_cmd_send(dev, unitid, SET_CUR,
1479 EXU_GPIO_CONTROL, sizeof(t), &t);
1481 printk(KERN_ERR "%s() error, ret = 0x%x\n",
1487 int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid,
1490 return saa7164_api_modify_gpio(dev, unitid, pin, 1);
1493 int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid,
1496 return saa7164_api_modify_gpio(dev, unitid, pin, 0);