Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelv...
[pandora-kernel.git] / drivers / media / video / saa7164 / saa7164-api.c
1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/wait.h>
23 #include <linux/slab.h>
24
25 #include "saa7164.h"
26
27 int saa7164_api_get_load_info(struct saa7164_dev *dev, struct tmFwInfoStruct *i)
28 {
29         int ret;
30
31         if (!(saa_debug & DBGLVL_CPU))
32                 return 0;
33
34         dprintk(DBGLVL_API, "%s()\n", __func__);
35
36         i->deviceinst = 0;
37         i->devicespec = 0;
38         i->mode = 0;
39         i->status = 0;
40
41         ret = saa7164_cmd_send(dev, 0, GET_CUR,
42                 GET_FW_STATUS_CONTROL, sizeof(struct tmFwInfoStruct), i);
43         if (ret != SAA_OK) {
44                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
45         }
46
47         printk(KERN_INFO "saa7164[%d]-CPU: %d percent", dev->nr, i->CPULoad);
48
49         return ret;
50 }
51
52 int saa7164_api_collect_debug(struct saa7164_dev *dev)
53 {
54         struct tmComResDebugGetData d;
55         u8 more = 255;
56         int ret;
57
58         dprintk(DBGLVL_API, "%s()\n", __func__);
59
60         while (more--) {
61
62                 memset(&d, 0, sizeof(d));
63
64                 ret = saa7164_cmd_send(dev, 0, GET_CUR,
65                         GET_DEBUG_DATA_CONTROL, sizeof(d), &d);
66                 if (ret != SAA_OK) {
67                         printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
68                 }
69
70                 if (d.dwResult != SAA_OK)
71                         break;
72
73                 printk(KERN_INFO "saa7164[%d]-FWMSG: %s", dev->nr, d.ucDebugData);
74         }
75
76         return 0;
77 }
78
79 int saa7164_api_set_debug(struct saa7164_dev *dev, u8 level)
80 {
81         struct tmComResDebugSetLevel lvl;
82         int ret;
83
84         dprintk(DBGLVL_API, "%s(level=%d)\n", __func__, level);
85
86         /* Retrieve current state */
87         ret = saa7164_cmd_send(dev, 0, GET_CUR,
88                 SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
89         if (ret != SAA_OK) {
90                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
91         }
92         dprintk(DBGLVL_API, "%s() Was %d\n", __func__, lvl.dwDebugLevel);
93
94         lvl.dwDebugLevel = level;
95
96         /* set new state */
97         ret = saa7164_cmd_send(dev, 0, SET_CUR,
98                 SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
99         if (ret != SAA_OK) {
100                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
101         }
102
103         return ret;
104 }
105
106 int saa7164_api_set_vbi_format(struct saa7164_port *port)
107 {
108         struct saa7164_dev *dev = port->dev;
109         struct tmComResProbeCommit fmt, rsp;
110         int ret;
111
112         dprintk(DBGLVL_API, "%s(nr=%d, unitid=0x%x)\n", __func__,
113                 port->nr, port->hwcfg.unitid);
114
115         fmt.bmHint = 0;
116         fmt.bFormatIndex = 1;
117         fmt.bFrameIndex = 1;
118
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);
122         if (ret != SAA_OK)
123                 printk(KERN_ERR "%s() set error, ret = 0x%x\n", __func__, ret);
124
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);
128         if (ret != SAA_OK) {
129                 printk(KERN_ERR "%s() get error, ret = 0x%x\n", __func__, ret);
130         } else {
131                 /* Compare requested vs received, should be same */
132                 if (memcmp(&fmt, &rsp, sizeof(rsp)) == 0) {
133                         dprintk(DBGLVL_API, "SET/PROBE Verified\n");
134
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);
138                         if (ret != SAA_OK)
139                                 printk(KERN_ERR "%s() commit error, ret = 0x%x\n",
140                                         __func__, ret);
141
142                         ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
143                                 GET_CUR, SAA_COMMIT_CONTROL, sizeof(rsp), &rsp);
144                         if (ret != SAA_OK)
145                                 printk(KERN_ERR "%s() GET commit error, ret = 0x%x\n",
146                                         __func__, ret);
147
148                         if (memcmp(&fmt, &rsp, sizeof(rsp)) != 0) {
149                                 printk(KERN_ERR "%s() memcmp error, ret = 0x%x\n",
150                                         __func__, ret);
151                         } else
152                                 dprintk(DBGLVL_API, "SET/COMMIT Verified\n");
153
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);
157                 } else
158                         printk(KERN_ERR "%s() compare failed\n", __func__);
159         }
160
161         if (ret == SAA_OK)
162                 dprintk(DBGLVL_API, "%s(nr=%d) Success\n", __func__, port->nr);
163
164         return ret;
165 }
166
167 int saa7164_api_set_gop_size(struct saa7164_port *port)
168 {
169         struct saa7164_dev *dev = port->dev;
170         struct tmComResEncVideoGopStructure gs;
171         int ret;
172
173         dprintk(DBGLVL_ENC, "%s()\n", __func__);
174
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,
179                 sizeof(gs), &gs);
180         if (ret != SAA_OK)
181                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
182
183         return ret;
184 }
185
186 int saa7164_api_set_encoder(struct saa7164_port *port)
187 {
188         struct saa7164_dev *dev = port->dev;
189         struct tmComResEncVideoBitRate vb;
190         struct tmComResEncAudioBitRate ab;
191         int ret;
192
193         dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
194                 port->hwcfg.sourceid);
195
196         if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
197                 port->encoder_profile = EU_PROFILE_PS_DVD;
198         else
199                 port->encoder_profile = EU_PROFILE_TS_HQ;
200
201         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
202                 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
203         if (ret != SAA_OK)
204                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
205
206         /* Resolution */
207         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
208                 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
209         if (ret != SAA_OK)
210                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
211
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;
215         else
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);
221         if (ret != SAA_OK)
222                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
223
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);
230         if (ret != SAA_OK)
231                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
232
233         saa7164_api_set_aspect_ratio(port);
234         saa7164_api_set_gop_size(port);
235
236         return ret;
237 }
238
239 int saa7164_api_get_encoder(struct saa7164_port *port)
240 {
241         struct saa7164_dev *dev = port->dev;
242         struct tmComResEncVideoBitRate v;
243         struct tmComResEncAudioBitRate a;
244         struct tmComResEncVideoInputAspectRatio ar;
245         int ret;
246
247         dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__, port->hwcfg.sourceid);
248
249         port->encoder_profile = 0;
250         port->video_format = 0;
251         port->video_resolution = 0;
252         port->audio_format = 0;
253
254         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
255                 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
256         if (ret != SAA_OK)
257                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
258
259         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
260                 EU_VIDEO_RESOLUTION_CONTROL, sizeof(u8), &port->video_resolution);
261         if (ret != SAA_OK)
262                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
263
264         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
265                 EU_VIDEO_FORMAT_CONTROL, sizeof(u8), &port->video_format);
266         if (ret != SAA_OK)
267                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
268
269         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
270                 EU_VIDEO_BIT_RATE_CONTROL, sizeof(v), &v);
271         if (ret != SAA_OK)
272                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
273
274         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
275                 EU_AUDIO_FORMAT_CONTROL, sizeof(u8), &port->audio_format);
276         if (ret != SAA_OK)
277                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
278
279         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
280                 EU_AUDIO_BIT_RATE_CONTROL, sizeof(a), &a);
281         if (ret != SAA_OK)
282                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
283
284         /* Aspect Ratio */
285         ar.width = 0;
286         ar.height = 0;
287         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
288                 EU_VIDEO_INPUT_ASPECT_CONTROL,
289                 sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
290         if (ret != SAA_OK)
291                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
292
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);
304
305         return ret;
306 }
307
308 int saa7164_api_set_aspect_ratio(struct saa7164_port *port)
309 {
310         struct saa7164_dev *dev = port->dev;
311         struct tmComResEncVideoInputAspectRatio ar;
312         int ret;
313
314         dprintk(DBGLVL_ENC, "%s(%d)\n", __func__,
315                 port->encoder_params.ctl_aspect);
316
317         switch (port->encoder_params.ctl_aspect) {
318         case V4L2_MPEG_VIDEO_ASPECT_1x1:
319                 ar.width = 1;
320                 ar.height = 1;
321                 break;
322         case V4L2_MPEG_VIDEO_ASPECT_4x3:
323                 ar.width = 4;
324                 ar.height = 3;
325                 break;
326         case V4L2_MPEG_VIDEO_ASPECT_16x9:
327                 ar.width = 16;
328                 ar.height = 9;
329                 break;
330         case V4L2_MPEG_VIDEO_ASPECT_221x100:
331                 ar.width = 221;
332                 ar.height = 100;
333                 break;
334         default:
335                 BUG();
336         }
337
338         dprintk(DBGLVL_ENC, "%s(%d) now %d:%d\n", __func__,
339                 port->encoder_params.ctl_aspect,
340                 ar.width, ar.height);
341
342         /* Aspect Ratio */
343         ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
344                 EU_VIDEO_INPUT_ASPECT_CONTROL,
345                 sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
346         if (ret != SAA_OK)
347                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
348
349         return ret;
350 }
351
352 int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl)
353 {
354         struct saa7164_dev *dev = port->dev;
355         int ret;
356         u16 val;
357
358         if (ctl == PU_BRIGHTNESS_CONTROL)
359                 val = port->ctl_brightness;
360         else
361         if (ctl == PU_CONTRAST_CONTROL)
362                 val = port->ctl_contrast;
363         else
364         if (ctl == PU_HUE_CONTROL)
365                 val = port->ctl_hue;
366         else
367         if (ctl == PU_SATURATION_CONTROL)
368                 val = port->ctl_saturation;
369         else
370         if (ctl == PU_SHARPNESS_CONTROL)
371                 val = port->ctl_sharpness;
372         else
373                 return -EINVAL;
374
375         dprintk(DBGLVL_ENC, "%s() unitid=0x%x ctl=%d, val=%d\n",
376                 __func__, port->encunit.vsourceid, ctl, val);
377
378         ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, SET_CUR,
379                 ctl, sizeof(u16), &val);
380         if (ret != SAA_OK)
381                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
382
383         return ret;
384 }
385
386 int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl)
387 {
388         struct saa7164_dev *dev = port->dev;
389         int ret;
390         u16 val;
391
392         ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, GET_CUR,
393                 ctl, sizeof(u16), &val);
394         if (ret != SAA_OK) {
395                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
396                 return ret;
397         }
398
399         dprintk(DBGLVL_ENC, "%s() ctl=%d, val=%d\n",
400                 __func__, ctl, val);
401
402         if (ctl == PU_BRIGHTNESS_CONTROL)
403                 port->ctl_brightness = val;
404         else
405         if (ctl == PU_CONTRAST_CONTROL)
406                 port->ctl_contrast = val;
407         else
408         if (ctl == PU_HUE_CONTROL)
409                 port->ctl_hue = val;
410         else
411         if (ctl == PU_SATURATION_CONTROL)
412                 port->ctl_saturation = val;
413         else
414         if (ctl == PU_SHARPNESS_CONTROL)
415                 port->ctl_sharpness = val;
416
417         return ret;
418 }
419
420 int saa7164_api_set_videomux(struct saa7164_port *port)
421 {
422         struct saa7164_dev *dev = port->dev;
423         u8 inputs[] = { 1, 2, 2, 2, 5, 5, 5 };
424         int ret;
425
426         dprintk(DBGLVL_ENC, "%s() v_mux=%d a_mux=%d\n",
427                 __func__, port->mux_input, inputs[port->mux_input - 1]);
428
429         /* Audio Mute */
430         ret = saa7164_api_audio_mute(port, 1);
431         if (ret != SAA_OK)
432                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
433
434         /* Video Mux */
435         ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, SET_CUR,
436                 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
437         if (ret != SAA_OK)
438                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
439
440         /* Audio Mux */
441         ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
442                 SU_INPUT_SELECT_CONTROL, sizeof(u8), &inputs[port->mux_input - 1]);
443         if (ret != SAA_OK)
444                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
445
446         /* Audio UnMute */
447         ret = saa7164_api_audio_mute(port, 0);
448         if (ret != SAA_OK)
449                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
450
451         return ret;
452 }
453
454 int saa7164_api_audio_mute(struct saa7164_port *port, int mute)
455 {
456         struct saa7164_dev *dev = port->dev;
457         u8 v = mute;
458         int ret;
459
460         dprintk(DBGLVL_API, "%s(%d)\n", __func__, mute);
461
462         ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
463                 MUTE_CONTROL, sizeof(u8), &v);
464         if (ret != SAA_OK)
465                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
466
467         return ret;
468 }
469
470 /* 0 = silence, 0xff = full */
471 int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level)
472 {
473         struct saa7164_dev *dev = port->dev;
474         s16 v, min, max;
475         int ret;
476
477         dprintk(DBGLVL_API, "%s(%d)\n", __func__, level);
478
479         /* Obtain the min/max ranges */
480         ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MIN,
481                 VOLUME_CONTROL, sizeof(u16), &min);
482         if (ret != SAA_OK)
483                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
484
485         ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MAX,
486                 VOLUME_CONTROL, sizeof(u16), &max);
487         if (ret != SAA_OK)
488                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
489
490         ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
491                 (0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
492         if (ret != SAA_OK)
493                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
494
495         dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
496
497         v = level;
498         if (v < min)
499                 v = min;
500         if (v > max)
501                 v = max;
502
503         /* Left */
504         ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
505                 (0x01 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
506         if (ret != SAA_OK)
507                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
508
509         /* Right */
510         ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
511                 (0x02 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
512         if (ret != SAA_OK)
513                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
514
515         ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
516                 (0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
517         if (ret != SAA_OK)
518                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
519
520         dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
521
522         return ret;
523 }
524
525 int saa7164_api_set_audio_std(struct saa7164_port *port)
526 {
527         struct saa7164_dev *dev = port->dev;
528         struct tmComResAudioDefaults lvl;
529         struct tmComResTunerStandard tvaudio;
530         int ret;
531
532         dprintk(DBGLVL_API, "%s()\n", __func__);
533
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);
543         if (ret != SAA_OK)
544                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
545
546         /* Manually select the appropriate TV audio standard */
547         if (port->encodernorm.id & V4L2_STD_NTSC) {
548                 tvaudio.std = TU_STANDARD_NTSC_M;
549                 tvaudio.country = 1;
550         } else {
551                 tvaudio.std = TU_STANDARD_PAL_I;
552                 tvaudio.country = 44;
553         }
554
555         ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
556                 TU_STANDARD_CONTROL, sizeof(tvaudio), &tvaudio);
557         if (ret != SAA_OK)
558                 printk(KERN_ERR "%s() TU_STANDARD_CONTROL error, ret = 0x%x\n", __func__, ret);
559         return ret;
560 }
561
562 int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect)
563 {
564         struct saa7164_dev *dev = port->dev;
565         struct tmComResTunerStandardAuto p;
566         int ret;
567
568         dprintk(DBGLVL_API, "%s(%d)\n", __func__, autodetect);
569
570         /* Disable TV Audio autodetect if not already set (buggy) */
571         if (autodetect)
572                 p.mode = TU_STANDARD_AUTO;
573         else
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);
577         if (ret != SAA_OK)
578                 printk(KERN_ERR "%s() TU_STANDARD_AUTO_CONTROL error, ret = 0x%x\n", __func__, ret);
579
580         return ret;
581 }
582
583 int saa7164_api_get_videomux(struct saa7164_port *port)
584 {
585         struct saa7164_dev *dev = port->dev;
586         int ret;
587
588         ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, GET_CUR,
589                 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
590         if (ret != SAA_OK)
591                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
592
593         dprintk(DBGLVL_ENC, "%s() v_mux=%d\n",
594                 __func__, port->mux_input);
595
596         return ret;
597 }
598
599 int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
600 {
601         struct saa7164_dev *dev = port->dev;
602
603         u16 len = 0;
604         u8 buf[256];
605         int ret;
606         u8 mas;
607
608         dprintk(DBGLVL_API, "%s(nr=%d type=%d val=%x)\n", __func__,
609                 port->nr, port->type, val);
610
611         if (port->nr == 0)
612                 mas = 0xd0;
613         else
614                 mas = 0xe0;
615
616         memset(buf, 0, sizeof(buf));
617
618         buf[0x00] = 0x04;
619         buf[0x01] = 0x00;
620         buf[0x02] = 0x00;
621         buf[0x03] = 0x00;
622
623         buf[0x04] = 0x04;
624         buf[0x05] = 0x00;
625         buf[0x06] = 0x00;
626         buf[0x07] = 0x00;
627
628         buf[0x08] = reg;
629         buf[0x09] = 0x26;
630         buf[0x0a] = mas;
631         buf[0x0b] = 0xb0;
632
633         buf[0x0c] = val;
634         buf[0x0d] = 0x00;
635         buf[0x0e] = 0x00;
636         buf[0x0f] = 0x00;
637
638         ret = saa7164_cmd_send(dev, port->ifunit.unitid, GET_LEN,
639                 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
640         if (ret != SAA_OK) {
641                 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
642                 return -EIO;
643         }
644
645         ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
646                 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
647         if (ret != SAA_OK)
648                 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
649
650         //saa7164_dumphex16(dev, buf, 16);
651
652         return ret == SAA_OK ? 0 : -EIO;
653 }
654
655 /* Disable the IF block AGC controls */
656 int saa7164_api_configure_dif(struct saa7164_port *port, u32 std)
657 {
658         struct saa7164_dev *dev = port->dev;
659         int ret = 0;
660         u8 agc_disable;
661
662         dprintk(DBGLVL_API, "%s(nr=%d, 0x%x)\n", __func__, port->nr, std);
663
664         if (std & V4L2_STD_NTSC) {
665                 dprintk(DBGLVL_API, " NTSC\n");
666                 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
667                 agc_disable = 0;
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 */
671                 agc_disable = 0;
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 */
675                 agc_disable = 0;
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 */
679                 agc_disable = 0;
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 */
683                 agc_disable = 0;
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 */
687                 agc_disable = 0;
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 */
691                 agc_disable = 0;
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 */
695                 agc_disable = 0;
696         } else {
697                 /* Unknown standard, assume DTV */
698                 dprintk(DBGLVL_API, " Unknown (assuming DTV)\n");
699                 saa7164_api_set_dif(port, 0x00, 0x80); /* Undefined Video Standard */
700                 agc_disable = 1;
701         }
702
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 */
707         msleep(100);
708         saa7164_api_set_dif(port, 0x04, 0x00); /* Active (again) */
709         msleep(100);
710
711         return ret;
712 }
713
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.
718  */
719 int saa7164_api_initialize_dif(struct saa7164_port *port)
720 {
721         struct saa7164_dev *dev = port->dev;
722         struct saa7164_port *p = 0;
723         int ret = -EINVAL;
724         u32 std = 0;
725
726         dprintk(DBGLVL_API, "%s(nr=%d type=%d)\n", __func__,
727                 port->nr, port->type);
728
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.
733                  */
734                 std = V4L2_STD_NTSC;
735         } else
736         if (port->type == SAA7164_MPEG_DVB) {
737                 if (port->nr == SAA7164_PORT_TS1)
738                         p = &dev->ports[SAA7164_PORT_ENC1];
739                 else
740                         p = &dev->ports[SAA7164_PORT_ENC2];
741         } else
742         if (port->type == SAA7164_MPEG_VBI) {
743                 std = V4L2_STD_NTSC;
744                 if (port->nr == SAA7164_PORT_VBI1)
745                         p = &dev->ports[SAA7164_PORT_ENC1];
746                 else
747                         p = &dev->ports[SAA7164_PORT_ENC2];
748         } else
749                 BUG();
750
751         if (p)
752                 ret = saa7164_api_configure_dif(p, std);
753
754         return ret;
755 }
756
757 int saa7164_api_transition_port(struct saa7164_port *port, u8 mode)
758 {
759         struct saa7164_dev *dev = port->dev;
760
761         int ret;
762
763         dprintk(DBGLVL_API, "%s(nr=%d unitid=0x%x,%d)\n",
764                 __func__, port->nr, port->hwcfg.unitid, mode);
765
766         ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid, SET_CUR,
767                 SAA_STATE_CONTROL, sizeof(mode), &mode);
768         if (ret != SAA_OK)
769                 printk(KERN_ERR "%s(portnr %d unitid 0x%x) error, ret = 0x%x\n",
770                         __func__, port->nr, port->hwcfg.unitid, ret);
771
772         return ret;
773 }
774
775 int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version)
776 {
777         int ret;
778
779         ret = saa7164_cmd_send(dev, 0, GET_CUR,
780                 GET_FW_VERSION_CONTROL, sizeof(u32), version);
781         if (ret != SAA_OK)
782                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
783
784         return ret;
785 }
786
787 int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen)
788 {
789         u8 reg[] = { 0x0f, 0x00 };
790
791         if (buflen < 128)
792                 return -ENOMEM;
793
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),
797                 &reg[0], 128, buf);
798 }
799
800 int saa7164_api_configure_port_vbi(struct saa7164_dev *dev,
801         struct saa7164_port *port)
802 {
803         struct tmComResVBIFormatDescrHeader *fmt = &port->vbi_fmt_ntsc;
804
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);
811
812         /* Cache the hardware configuration in the port */
813
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 +
819                 (4 * sizeof(u32)) +
820                 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
821         port->bufptr32h = port->hwcfg.BARLocation +
822                 (4 * sizeof(u32)) +
823                 (sizeof(u32) * port->hwcfg.buffercount);
824         port->bufptr64 = port->hwcfg.BARLocation +
825                 (4 * sizeof(u32)) +
826                 (sizeof(u32) * port->hwcfg.buffercount);
827         dprintk(DBGLVL_API, "   = port->hwcfg.BARLocation = 0x%x\n",
828                 port->hwcfg.BARLocation);
829
830         dprintk(DBGLVL_API, "   = VS_FORMAT_VBI (becomes dev->en[%d])\n",
831                 port->nr);
832
833         return 0;
834 }
835
836 int saa7164_api_configure_port_mpeg2ts(struct saa7164_dev *dev,
837         struct saa7164_port *port,
838         struct tmComResTSFormatDescrHeader *tsfmt)
839 {
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");
845
846         /* Cache the hardware configuration in the port */
847
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 +
853                 (4 * sizeof(u32)) +
854                 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
855         port->bufptr32h = port->hwcfg.BARLocation +
856                 (4 * sizeof(u32)) +
857                 (sizeof(u32) * port->hwcfg.buffercount);
858         port->bufptr64 = port->hwcfg.BARLocation +
859                 (4 * sizeof(u32)) +
860                 (sizeof(u32) * port->hwcfg.buffercount);
861         dprintk(DBGLVL_API, "   = port->hwcfg.BARLocation = 0x%x\n",
862                 port->hwcfg.BARLocation);
863
864         dprintk(DBGLVL_API, "   = VS_FORMAT_MPEGTS (becomes dev->ts[%d])\n",
865                 port->nr);
866
867         return 0;
868 }
869
870 int saa7164_api_configure_port_mpeg2ps(struct saa7164_dev *dev,
871         struct saa7164_port *port,
872         struct tmComResPSFormatDescrHeader *fmt)
873 {
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);
878
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 +
886                 (4 * sizeof(u32)) +
887                 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
888         port->bufptr32h = port->hwcfg.BARLocation +
889                 (4 * sizeof(u32)) +
890                 (sizeof(u32) * port->hwcfg.buffercount);
891         port->bufptr64 = port->hwcfg.BARLocation +
892                 (4 * sizeof(u32)) +
893                 (sizeof(u32) * port->hwcfg.buffercount);
894         dprintk(DBGLVL_API, "   = port->hwcfg.BARLocation = 0x%x\n",
895                 port->hwcfg.BARLocation);
896
897         dprintk(DBGLVL_API, "   = VS_FORMAT_MPEGPS (becomes dev->enc[%d])\n",
898                 port->nr);
899
900         return 0;
901 }
902
903 int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
904 {
905         struct saa7164_port *tsport = 0;
906         struct saa7164_port *encport = 0;
907         struct saa7164_port *vbiport = 0;
908         u32 idx, next_offset;
909         int i;
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;
923         u32 currpath = 0;
924
925         dprintk(DBGLVL_API,
926                 "%s(?,?,%d) sizeof(struct tmComResDescrHeader) = %d bytes\n",
927                 __func__, len, (u32)sizeof(struct tmComResDescrHeader));
928
929         for (idx = 0; idx < (len - sizeof(struct tmComResDescrHeader));) {
930
931                 hdr = (struct tmComResDescrHeader *)(buf + idx);
932
933                 if (hdr->type != CS_INTERFACE)
934                         return SAA_ERR_NOT_SUPPORTED;
935
936                 dprintk(DBGLVL_API, "@ 0x%x = \n", idx);
937                 switch (hdr->subtype) {
938                 case GENERAL_REQUEST:
939                         dprintk(DBGLVL_API, " GENERAL_REQUEST\n");
940                         break;
941                 case VC_TUNER_PATH:
942                         dprintk(DBGLVL_API, " VC_TUNER_PATH\n");
943                         pathhdr = (struct tmComResPathDescrHeader *)(buf + idx);
944                         dprintk(DBGLVL_API, "  pathid = 0x%x\n",
945                                 pathhdr->pathid);
946                         currpath = pathhdr->pathid;
947                         break;
948                 case VC_INPUT_TERMINAL:
949                         dprintk(DBGLVL_API, " VC_INPUT_TERMINAL\n");
950                         anttermhdr =
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) {
957                         case ITT_ANTENNA:
958                                 dprintk(DBGLVL_API, "   = ITT_ANTENNA\n");
959                                 break;
960                         case LINE_CONNECTOR:
961                                 dprintk(DBGLVL_API, "   = LINE_CONNECTOR\n");
962                                 break;
963                         case SPDIF_CONNECTOR:
964                                 dprintk(DBGLVL_API, "   = SPDIF_CONNECTOR\n");
965                                 break;
966                         case COMPOSITE_CONNECTOR:
967                                 dprintk(DBGLVL_API,
968                                         "   = COMPOSITE_CONNECTOR\n");
969                                 break;
970                         case SVIDEO_CONNECTOR:
971                                 dprintk(DBGLVL_API, "   = SVIDEO_CONNECTOR\n");
972                                 break;
973                         case COMPONENT_CONNECTOR:
974                                 dprintk(DBGLVL_API,
975                                         "   = COMPONENT_CONNECTOR\n");
976                                 break;
977                         case STANDARD_DMA:
978                                 dprintk(DBGLVL_API, "   = STANDARD_DMA\n");
979                                 break;
980                         default:
981                                 dprintk(DBGLVL_API, "   = undefined (0x%x)\n",
982                                         anttermhdr->terminaltype);
983                         }
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);
990                         break;
991                 case VC_OUTPUT_TERMINAL:
992                         dprintk(DBGLVL_API, " VC_OUTPUT_TERMINAL\n");
993                         vcoutputtermhdr =
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) {
1000                         case ITT_ANTENNA:
1001                                 dprintk(DBGLVL_API, "   = ITT_ANTENNA\n");
1002                                 break;
1003                         case LINE_CONNECTOR:
1004                                 dprintk(DBGLVL_API, "   = LINE_CONNECTOR\n");
1005                                 break;
1006                         case SPDIF_CONNECTOR:
1007                                 dprintk(DBGLVL_API, "   = SPDIF_CONNECTOR\n");
1008                                 break;
1009                         case COMPOSITE_CONNECTOR:
1010                                 dprintk(DBGLVL_API,
1011                                         "   = COMPOSITE_CONNECTOR\n");
1012                                 break;
1013                         case SVIDEO_CONNECTOR:
1014                                 dprintk(DBGLVL_API, "   = SVIDEO_CONNECTOR\n");
1015                                 break;
1016                         case COMPONENT_CONNECTOR:
1017                                 dprintk(DBGLVL_API,
1018                                         "   = COMPONENT_CONNECTOR\n");
1019                                 break;
1020                         case STANDARD_DMA:
1021                                 dprintk(DBGLVL_API, "   = STANDARD_DMA\n");
1022                                 break;
1023                         default:
1024                                 dprintk(DBGLVL_API, "   = undefined (0x%x)\n",
1025                                         vcoutputtermhdr->terminaltype);
1026                         }
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);
1047
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:
1056                                         tsfmt =
1057                                         (struct tmComResTSFormatDescrHeader *)t;
1058                                         if (currpath == 1)
1059                                                 tsport = &dev->ports[SAA7164_PORT_TS1];
1060                                         else
1061                                                 tsport = &dev->ports[SAA7164_PORT_TS2];
1062                                         memcpy(&tsport->hwcfg, vcoutputtermhdr,
1063                                                 sizeof(*vcoutputtermhdr));
1064                                         saa7164_api_configure_port_mpeg2ts(dev,
1065                                                 tsport, tsfmt);
1066                                         break;
1067                                 case VS_FORMAT_MPEG2PS:
1068                                         psfmt =
1069                                         (struct tmComResPSFormatDescrHeader *)t;
1070                                         if (currpath == 1)
1071                                                 encport = &dev->ports[SAA7164_PORT_ENC1];
1072                                         else
1073                                                 encport = &dev->ports[SAA7164_PORT_ENC2];
1074                                         memcpy(&encport->hwcfg, vcoutputtermhdr,
1075                                                 sizeof(*vcoutputtermhdr));
1076                                         saa7164_api_configure_port_mpeg2ps(dev,
1077                                                 encport, psfmt);
1078                                         break;
1079                                 case VS_FORMAT_VBI:
1080                                         vbifmt =
1081                                         (struct tmComResVBIFormatDescrHeader *)t;
1082                                         if (currpath == 1)
1083                                                 vbiport = &dev->ports[SAA7164_PORT_VBI1];
1084                                         else
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,
1090                                                 vbiport);
1091                                         break;
1092                                 case VS_FORMAT_RDS:
1093                                         dprintk(DBGLVL_API,
1094                                                 "   = VS_FORMAT_RDS\n");
1095                                         break;
1096                                 case VS_FORMAT_UNCOMPRESSED:
1097                                         dprintk(DBGLVL_API,
1098                                         "   = VS_FORMAT_UNCOMPRESSED\n");
1099                                         break;
1100                                 case VS_FORMAT_TYPE:
1101                                         dprintk(DBGLVL_API,
1102                                                 "   = VS_FORMAT_TYPE\n");
1103                                         break;
1104                                 default:
1105                                         dprintk(DBGLVL_API,
1106                                                 "   = undefined (0x%x)\n",
1107                                                 t->subtype);
1108                                 }
1109                                 next_offset += t->len;
1110                         }
1111
1112                         break;
1113                 case TUNER_UNIT:
1114                         dprintk(DBGLVL_API, " TUNER_UNIT\n");
1115                         tunerunithdr =
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);
1129
1130                         if (tunerunithdr->unitid == tunerunithdr->iunit) {
1131                                 if (currpath == 1)
1132                                         encport = &dev->ports[SAA7164_PORT_ENC1];
1133                                 else
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);
1138                         }
1139                         break;
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",
1144                                 psel->unitid);
1145                         dprintk(DBGLVL_API, "  nrinpins = 0x%x\n",
1146                                 psel->nrinpins);
1147                         dprintk(DBGLVL_API, "  sourceid = 0x%x\n",
1148                                 psel->sourceid);
1149                         break;
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",
1154                                 pdh->unitid);
1155                         dprintk(DBGLVL_API, "  sourceid = 0x%x\n",
1156                                 pdh->sourceid);
1157                         dprintk(DBGLVL_API, "  controlsize = 0x%x\n",
1158                                 pdh->controlsize);
1159                         if (pdh->controlsize == 0x04) {
1160                                 if (currpath == 1)
1161                                         encport = &dev->ports[SAA7164_PORT_ENC1];
1162                                 else
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);
1167                         }
1168                         break;
1169                 case FEATURE_UNIT:
1170                         afd = (struct tmComResAFeatureDescrHeader *)(buf + idx);
1171                         dprintk(DBGLVL_API, " FEATURE_UNIT\n");
1172                         dprintk(DBGLVL_API, "  unitid = 0x%x\n",
1173                                 afd->unitid);
1174                         dprintk(DBGLVL_API, "  sourceid = 0x%x\n",
1175                                 afd->sourceid);
1176                         dprintk(DBGLVL_API, "  controlsize = 0x%x\n",
1177                                 afd->controlsize);
1178                         if (currpath == 1)
1179                                 encport = &dev->ports[SAA7164_PORT_ENC1];
1180                         else
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);
1185                         break;
1186                 case ENCODER_UNIT:
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) {
1195                                 if (currpath == 1)
1196                                         encport = &dev->ports[SAA7164_PORT_ENC1];
1197                                 else
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);
1202                         }
1203                         break;
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",
1208                                 exthdr->unitid);
1209                         dprintk(DBGLVL_API, "  deviceid = 0x%x\n",
1210                                 exthdr->deviceid);
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)
1238                                 dprintk(DBGLVL_API,
1239                                         "   = VBI Decoder\n");
1240                         if (exthdr->devicetype & 0x10000)
1241                                 dprintk(DBGLVL_API,
1242                                         "   = Streaming Device\n");
1243                         if (exthdr->devicetype & 0x20000)
1244                                 dprintk(DBGLVL_API,
1245                                         "   = DRM Device\n");
1246                         if (exthdr->devicetype & 0x40000000)
1247                                 dprintk(DBGLVL_API,
1248                                         "   = Generic Device\n");
1249                         if (exthdr->devicetype & 0x80000000)
1250                                 dprintk(DBGLVL_API,
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) {
1259                                 if (currpath == 1)
1260                                         encport = &dev->ports[SAA7164_PORT_ENC1];
1261                                 else
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);
1266                         }
1267                         break;
1268                 case PVC_INFRARED_UNIT:
1269                         dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n");
1270                         break;
1271                 case DRM_UNIT:
1272                         dprintk(DBGLVL_API, " DRM_UNIT\n");
1273                         break;
1274                 default:
1275                         dprintk(DBGLVL_API, "default %d\n", hdr->subtype);
1276                 }
1277
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);
1282
1283                 idx += hdr->len;
1284         }
1285
1286         return 0;
1287 }
1288
1289 int saa7164_api_enum_subdevs(struct saa7164_dev *dev)
1290 {
1291         int ret;
1292         u32 buflen = 0;
1293         u8 *buf;
1294
1295         dprintk(DBGLVL_API, "%s()\n", __func__);
1296
1297         /* Get the total descriptor length */
1298         ret = saa7164_cmd_send(dev, 0, GET_LEN,
1299                 GET_DESCRIPTORS_CONTROL, sizeof(buflen), &buflen);
1300         if (ret != SAA_OK)
1301                 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1302
1303         dprintk(DBGLVL_API, "%s() total descriptor size = %d bytes.\n",
1304                 __func__, buflen);
1305
1306         /* Allocate enough storage for all of the descs */
1307         buf = kzalloc(buflen, GFP_KERNEL);
1308         if (buf == NULL)
1309                 return SAA_ERR_NO_RESOURCES;
1310
1311         /* Retrieve them */
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);
1316                 goto out;
1317         }
1318
1319         if (saa_debug & DBGLVL_API)
1320                 saa7164_dumphex16(dev, buf, (buflen/16)*16);
1321
1322         saa7164_api_dump_subdevs(dev, buf, buflen);
1323
1324 out:
1325         kfree(buf);
1326         return ret;
1327 }
1328
1329 int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
1330         u32 datalen, u8 *data)
1331 {
1332         struct saa7164_dev *dev = bus->dev;
1333         u16 len = 0;
1334         int unitid;
1335         u32 regval;
1336         u8 buf[256];
1337         int ret;
1338
1339         dprintk(DBGLVL_API, "%s()\n", __func__);
1340
1341         if (reglen > 4)
1342                 return -EIO;
1343
1344         if (reglen == 1)
1345                 regval = *(reg);
1346         else
1347         if (reglen == 2)
1348                 regval = ((*(reg) << 8) || *(reg+1));
1349         else
1350         if (reglen == 3)
1351                 regval = ((*(reg) << 16) | (*(reg+1) << 8) | *(reg+2));
1352         else
1353         if (reglen == 4)
1354                 regval = ((*(reg) << 24) | (*(reg+1) << 16) |
1355                         (*(reg+2) << 8) | *(reg+3));
1356
1357         /* Prepare the send buffer */
1358         /* Bytes 00-03 source register length
1359          *       04-07 source bytes to read
1360          *       08... register address
1361          */
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;
1366
1367         unitid = saa7164_i2caddr_to_unitid(bus, addr);
1368         if (unitid < 0) {
1369                 printk(KERN_ERR
1370                         "%s() error, cannot translate regaddr 0x%x to unitid\n",
1371                         __func__, addr);
1372                 return -EIO;
1373         }
1374
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);
1379                 return -EIO;
1380         }
1381
1382         dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1383
1384         if (saa_debug & DBGLVL_I2C)
1385                 saa7164_dumphex16(dev, buf, 2 * 16);
1386
1387         ret = saa7164_cmd_send(bus->dev, unitid, GET_CUR,
1388                 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1389         if (ret != SAA_OK)
1390                 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1391         else {
1392                 if (saa_debug & DBGLVL_I2C)
1393                         saa7164_dumphex16(dev, buf, sizeof(buf));
1394                 memcpy(data, (buf + 2 * sizeof(u32) + reglen), datalen);
1395         }
1396
1397         return ret == SAA_OK ? 0 : -EIO;
1398 }
1399
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,
1402         u8 *data)
1403 {
1404         struct saa7164_dev *dev = bus->dev;
1405         u16 len = 0;
1406         int unitid;
1407         int reglen;
1408         u8 buf[256];
1409         int ret;
1410
1411         dprintk(DBGLVL_API, "%s()\n", __func__);
1412
1413         if ((datalen == 0) || (datalen > 232))
1414                 return -EIO;
1415
1416         memset(buf, 0, sizeof(buf));
1417
1418         unitid = saa7164_i2caddr_to_unitid(bus, addr);
1419         if (unitid < 0) {
1420                 printk(KERN_ERR
1421                         "%s() error, cannot translate regaddr 0x%x to unitid\n",
1422                         __func__, addr);
1423                 return -EIO;
1424         }
1425
1426         reglen = saa7164_i2caddr_to_reglen(bus, addr);
1427         if (reglen < 0) {
1428                 printk(KERN_ERR
1429                         "%s() error, cannot translate regaddr to reglen\n",
1430                         __func__);
1431                 return -EIO;
1432         }
1433
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);
1438                 return -EIO;
1439         }
1440
1441         dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1442
1443         /* Prepare the send buffer */
1444         /* Bytes 00-03 dest register length
1445          *       04-07 dest bytes to write
1446          *       08... register address
1447          */
1448         *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1449         *((u32 *)(buf + 1 * sizeof(u32))) = datalen - reglen;
1450         memcpy((buf + 2 * sizeof(u32)), data, datalen);
1451
1452         if (saa_debug & DBGLVL_I2C)
1453                 saa7164_dumphex16(dev, buf, sizeof(buf));
1454
1455         ret = saa7164_cmd_send(bus->dev, unitid, SET_CUR,
1456                 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1457         if (ret != SAA_OK)
1458                 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1459
1460         return ret == SAA_OK ? 0 : -EIO;
1461 }
1462
1463 int saa7164_api_modify_gpio(struct saa7164_dev *dev, u8 unitid,
1464         u8 pin, u8 state)
1465 {
1466         int ret;
1467         struct tmComResGPIO t;
1468
1469         dprintk(DBGLVL_API, "%s(0x%x, %d, %d)\n",
1470                 __func__, unitid, pin, state);
1471
1472         if ((pin > 7) || (state > 2))
1473                 return SAA_ERR_BAD_PARAMETER;
1474
1475         t.pin = pin;
1476         t.state = state;
1477
1478         ret = saa7164_cmd_send(dev, unitid, SET_CUR,
1479                 EXU_GPIO_CONTROL, sizeof(t), &t);
1480         if (ret != SAA_OK)
1481                 printk(KERN_ERR "%s() error, ret = 0x%x\n",
1482                         __func__, ret);
1483
1484         return ret;
1485 }
1486
1487 int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid,
1488         u8 pin)
1489 {
1490         return saa7164_api_modify_gpio(dev, unitid, pin, 1);
1491 }
1492
1493 int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid,
1494         u8 pin)
1495 {
1496         return saa7164_api_modify_gpio(dev, unitid, pin, 0);
1497 }
1498