saa7164-api.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. #include <linux/wait.h>
  22. #include <linux/slab.h>
  23. #include "saa7164.h"
  24. int saa7164_api_set_gop_size(struct saa7164_port *port)
  25. {
  26. struct saa7164_dev *dev = port->dev;
  27. tmComResEncVideoGopStructure_t gs;
  28. int ret;
  29. dprintk(DBGLVL_ENC, "%s()\n", __func__);
  30. gs.ucRefFrameDist = port->encoder_params.refdist;
  31. gs.ucGOPSize = port->encoder_params.gop_size;
  32. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
  33. EU_VIDEO_GOP_STRUCTURE_CONTROL,
  34. sizeof(gs), &gs);
  35. if (ret != SAA_OK)
  36. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  37. return ret;
  38. }
  39. int saa7164_api_set_encoder(struct saa7164_port *port)
  40. {
  41. struct saa7164_dev *dev = port->dev;
  42. tmComResEncVideoBitRate_t vb;
  43. tmComResEncAudioBitRate_t ab;
  44. int ret;
  45. dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
  46. port->hwcfg.sourceid);
  47. if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
  48. port->encoder_profile = EU_PROFILE_PS_DVD;
  49. else
  50. port->encoder_profile = EU_PROFILE_TS_HQ;
  51. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
  52. EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
  53. if (ret != SAA_OK)
  54. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  55. /* Resolution */
  56. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
  57. EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
  58. if (ret != SAA_OK)
  59. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  60. /* Establish video bitrates */
  61. if (port->encoder_params.bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
  62. vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_CONSTANT;
  63. else
  64. vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_VARIABLE_PEAK;
  65. vb.dwVideoBitRate = port->encoder_params.bitrate;
  66. vb.dwVideoBitRatePeak = port->encoder_params.bitrate_peak;
  67. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
  68. EU_VIDEO_BIT_RATE_CONTROL, sizeof(tmComResEncVideoBitRate_t), &vb);
  69. if (ret != SAA_OK)
  70. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  71. /* Establish audio bitrates */
  72. ab.ucAudioBitRateMode = 0;
  73. ab.dwAudioBitRate = 384000;
  74. ab.dwAudioBitRatePeak = ab.dwAudioBitRate;
  75. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
  76. EU_AUDIO_BIT_RATE_CONTROL, sizeof(tmComResEncAudioBitRate_t), &ab);
  77. if (ret != SAA_OK)
  78. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  79. saa7164_api_set_aspect_ratio(port);
  80. saa7164_api_set_gop_size(port);
  81. return ret;
  82. }
  83. int saa7164_api_get_encoder(struct saa7164_port *port)
  84. {
  85. struct saa7164_dev *dev = port->dev;
  86. tmComResEncVideoBitRate_t v;
  87. tmComResEncAudioBitRate_t a;
  88. tmComResEncVideoInputAspectRatio_t ar;
  89. int ret;
  90. dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__, port->hwcfg.sourceid);
  91. port->encoder_profile = 0;
  92. port->video_format = 0;
  93. port->video_resolution = 0;
  94. port->audio_format = 0;
  95. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
  96. EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
  97. if (ret != SAA_OK)
  98. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  99. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
  100. EU_VIDEO_RESOLUTION_CONTROL, sizeof(u8), &port->video_resolution);
  101. if (ret != SAA_OK)
  102. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  103. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
  104. EU_VIDEO_FORMAT_CONTROL, sizeof(u8), &port->video_format);
  105. if (ret != SAA_OK)
  106. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  107. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
  108. EU_VIDEO_BIT_RATE_CONTROL, sizeof(v), &v);
  109. if (ret != SAA_OK)
  110. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  111. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
  112. EU_AUDIO_FORMAT_CONTROL, sizeof(u8), &port->audio_format);
  113. if (ret != SAA_OK)
  114. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  115. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
  116. EU_AUDIO_BIT_RATE_CONTROL, sizeof(a), &a);
  117. if (ret != SAA_OK)
  118. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  119. /* Aspect Ratio */
  120. ar.width = 0;
  121. ar.height = 0;
  122. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
  123. EU_VIDEO_INPUT_ASPECT_CONTROL,
  124. sizeof(tmComResEncVideoInputAspectRatio_t), &ar);
  125. if (ret != SAA_OK)
  126. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  127. dprintk(DBGLVL_ENC, "encoder_profile = %d\n", port->encoder_profile);
  128. dprintk(DBGLVL_ENC, "video_format = %d\n", port->video_format);
  129. dprintk(DBGLVL_ENC, "audio_format = %d\n", port->audio_format);
  130. dprintk(DBGLVL_ENC, "video_resolution= %d\n", port->video_resolution);
  131. dprintk(DBGLVL_ENC, "v.ucVideoBitRateMode = %d\n", v.ucVideoBitRateMode);
  132. dprintk(DBGLVL_ENC, "v.dwVideoBitRate = %d\n", v.dwVideoBitRate);
  133. dprintk(DBGLVL_ENC, "v.dwVideoBitRatePeak = %d\n", v.dwVideoBitRatePeak);
  134. dprintk(DBGLVL_ENC, "a.ucVideoBitRateMode = %d\n", a.ucAudioBitRateMode);
  135. dprintk(DBGLVL_ENC, "a.dwVideoBitRate = %d\n", a.dwAudioBitRate);
  136. dprintk(DBGLVL_ENC, "a.dwVideoBitRatePeak = %d\n", a.dwAudioBitRatePeak);
  137. dprintk(DBGLVL_ENC, "aspect.width / height = %d:%d\n", ar.width, ar.height);
  138. return ret;
  139. }
  140. int saa7164_api_set_aspect_ratio(struct saa7164_port *port)
  141. {
  142. struct saa7164_dev *dev = port->dev;
  143. tmComResEncVideoInputAspectRatio_t ar;
  144. int ret;
  145. dprintk(DBGLVL_ENC, "%s(%d)\n", __func__,
  146. port->encoder_params.ctl_aspect);
  147. switch (port->encoder_params.ctl_aspect) {
  148. case V4L2_MPEG_VIDEO_ASPECT_1x1:
  149. ar.width = 1;
  150. ar.height = 1;
  151. break;
  152. case V4L2_MPEG_VIDEO_ASPECT_4x3:
  153. ar.width = 4;
  154. ar.height = 3;
  155. break;
  156. case V4L2_MPEG_VIDEO_ASPECT_16x9:
  157. ar.width = 16;
  158. ar.height = 9;
  159. break;
  160. case V4L2_MPEG_VIDEO_ASPECT_221x100:
  161. ar.width = 221;
  162. ar.height = 100;
  163. break;
  164. default:
  165. BUG();
  166. }
  167. dprintk(DBGLVL_ENC, "%s(%d) now %d:%d\n", __func__,
  168. port->encoder_params.ctl_aspect,
  169. ar.width, ar.height);
  170. /* Aspect Ratio */
  171. ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
  172. EU_VIDEO_INPUT_ASPECT_CONTROL,
  173. sizeof(tmComResEncVideoInputAspectRatio_t), &ar);
  174. if (ret != SAA_OK)
  175. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  176. return ret;
  177. }
  178. int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl)
  179. {
  180. struct saa7164_dev *dev = port->dev;
  181. int ret;
  182. u16 val;
  183. if (ctl == PU_BRIGHTNESS_CONTROL)
  184. val = port->ctl_brightness;
  185. else
  186. if (ctl == PU_CONTRAST_CONTROL)
  187. val = port->ctl_contrast;
  188. else
  189. if (ctl == PU_HUE_CONTROL)
  190. val = port->ctl_hue;
  191. else
  192. if (ctl == PU_SATURATION_CONTROL)
  193. val = port->ctl_saturation;
  194. else
  195. if (ctl == PU_SHARPNESS_CONTROL)
  196. val = port->ctl_sharpness;
  197. else
  198. return -EINVAL;
  199. dprintk(DBGLVL_ENC, "%s() unitid=0x%x ctl=%d, val=%d\n",
  200. __func__, port->encunit.vsourceid, ctl, val);
  201. ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, SET_CUR,
  202. ctl, sizeof(u16), &val);
  203. if (ret != SAA_OK)
  204. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  205. return ret;
  206. }
  207. int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl)
  208. {
  209. struct saa7164_dev *dev = port->dev;
  210. int ret;
  211. u16 val;
  212. ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, GET_CUR,
  213. ctl, sizeof(u16), &val);
  214. if (ret != SAA_OK) {
  215. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  216. return ret;
  217. }
  218. dprintk(DBGLVL_ENC, "%s() ctl=%d, val=%d\n",
  219. __func__, ctl, val);
  220. if (ctl == PU_BRIGHTNESS_CONTROL)
  221. port->ctl_brightness = val;
  222. else
  223. if (ctl == PU_CONTRAST_CONTROL)
  224. port->ctl_contrast = val;
  225. else
  226. if (ctl == PU_HUE_CONTROL)
  227. port->ctl_hue = val;
  228. else
  229. if (ctl == PU_SATURATION_CONTROL)
  230. port->ctl_saturation = val;
  231. else
  232. if (ctl == PU_SHARPNESS_CONTROL)
  233. port->ctl_sharpness = val;
  234. return ret;
  235. }
  236. int saa7164_api_set_videomux(struct saa7164_port *port)
  237. {
  238. struct saa7164_dev *dev = port->dev;
  239. u8 inputs[] = { 1, 2, 2, 2, 5, 5, 5 };
  240. int ret;
  241. dprintk(DBGLVL_ENC, "%s() v_mux=%d a_mux=%d\n",
  242. __func__, port->mux_input, inputs[ port->mux_input - 1 ]);
  243. /* Audio Mute */
  244. ret = saa7164_api_audio_mute(port, 1);
  245. if (ret != SAA_OK)
  246. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  247. /* Video Mux */
  248. ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, SET_CUR,
  249. SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
  250. if (ret != SAA_OK)
  251. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  252. /* Audio Mux */
  253. ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
  254. SU_INPUT_SELECT_CONTROL, sizeof(u8), &inputs[ port->mux_input - 1 ]);
  255. if (ret != SAA_OK)
  256. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  257. /* Audio UnMute */
  258. ret = saa7164_api_audio_mute(port, 0);
  259. if (ret != SAA_OK)
  260. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  261. return ret;
  262. }
  263. int saa7164_api_audio_mute(struct saa7164_port *port, int mute)
  264. {
  265. struct saa7164_dev *dev = port->dev;
  266. u8 v = mute;
  267. int ret;
  268. dprintk(DBGLVL_API, "%s(%d)\n", __func__, mute);
  269. ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
  270. MUTE_CONTROL, sizeof(u8), &v);
  271. if (ret != SAA_OK)
  272. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  273. return ret;
  274. }
  275. /* 0 = silence, 0xff = full */
  276. int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level)
  277. {
  278. struct saa7164_dev *dev = port->dev;
  279. s16 v, min, max;
  280. int ret;
  281. dprintk(DBGLVL_API, "%s(%d)\n", __func__, level);
  282. /* Obtain the min/max ranges */
  283. ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MIN,
  284. VOLUME_CONTROL, sizeof(u16), &min);
  285. if (ret != SAA_OK)
  286. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  287. ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MAX,
  288. VOLUME_CONTROL, sizeof(u16), &max);
  289. if (ret != SAA_OK)
  290. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  291. ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
  292. ( 0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
  293. if (ret != SAA_OK)
  294. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  295. dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
  296. v = level;
  297. if (v < min)
  298. v = min;
  299. if (v > max)
  300. v = max;
  301. /* Left */
  302. ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
  303. ( 0x01 << 8 ) | VOLUME_CONTROL, sizeof(s16), &v);
  304. if (ret != SAA_OK)
  305. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  306. /* Right */
  307. ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
  308. ( 0x02 << 8 ) | VOLUME_CONTROL, sizeof(s16), &v);
  309. if (ret != SAA_OK)
  310. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  311. ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
  312. ( 0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
  313. if (ret != SAA_OK)
  314. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  315. dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
  316. return ret;
  317. }
  318. int saa7164_api_set_audio_std(struct saa7164_port *port)
  319. {
  320. struct saa7164_dev *dev = port->dev;
  321. tmComResAudioDefaults_t lvl;
  322. tmComResTunerStandard_t tvaudio;
  323. int ret;
  324. dprintk(DBGLVL_API, "%s()\n", __func__);
  325. /* Establish default levels */
  326. lvl.ucDecoderLevel = TMHW_LEV_ADJ_DECLEV_DEFAULT;
  327. lvl.ucDecoderFM_Level = TMHW_LEV_ADJ_DECLEV_DEFAULT;
  328. lvl.ucMonoLevel = TMHW_LEV_ADJ_MONOLEV_DEFAULT;
  329. lvl.ucNICAM_Level = TMHW_LEV_ADJ_NICLEV_DEFAULT;
  330. lvl.ucSAP_Level = TMHW_LEV_ADJ_SAPLEV_DEFAULT;
  331. lvl.ucADC_Level = TMHW_LEV_ADJ_ADCLEV_DEFAULT;
  332. ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
  333. AUDIO_DEFAULT_CONTROL, sizeof(tmComResAudioDefaults_t), &lvl);
  334. if (ret != SAA_OK)
  335. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  336. /* Manually select the appropriate TV audio standard */
  337. if (port->encodernorm.id & V4L2_STD_NTSC) {
  338. tvaudio.std = TU_STANDARD_NTSC_M;
  339. tvaudio.country = 1;
  340. } else {
  341. tvaudio.std = TU_STANDARD_PAL_I;
  342. tvaudio.country = 44;
  343. }
  344. ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
  345. TU_STANDARD_CONTROL, sizeof(tvaudio), &tvaudio);
  346. if (ret != SAA_OK)
  347. printk(KERN_ERR "%s() TU_STANDARD_CONTROL error, ret = 0x%x\n", __func__, ret);
  348. return ret;
  349. }
  350. int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect)
  351. {
  352. struct saa7164_dev *dev = port->dev;
  353. tmComResTunerStandardAuto_t p;
  354. int ret;
  355. dprintk(DBGLVL_API, "%s(%d)\n", __func__, autodetect);
  356. /* Disable TV Audio autodetect if not already set (buggy) */
  357. if (autodetect)
  358. p.mode = TU_STANDARD_AUTO;
  359. else
  360. p.mode = TU_STANDARD_MANUAL;
  361. ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
  362. TU_STANDARD_AUTO_CONTROL, sizeof(p), &p);
  363. if (ret != SAA_OK)
  364. printk(KERN_ERR "%s() TU_STANDARD_AUTO_CONTROL error, ret = 0x%x\n", __func__, ret);
  365. return ret;
  366. }
  367. int saa7164_api_get_videomux(struct saa7164_port *port)
  368. {
  369. struct saa7164_dev *dev = port->dev;
  370. int ret;
  371. ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, GET_CUR,
  372. SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
  373. if (ret != SAA_OK)
  374. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  375. dprintk(DBGLVL_ENC, "%s() v_mux=%d\n",
  376. __func__, port->mux_input);
  377. return ret;
  378. }
  379. int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
  380. {
  381. struct saa7164_dev *dev = port->dev;
  382. u16 len = 0;
  383. u8 buf[256];
  384. int ret;
  385. u8 mas;
  386. dprintk(DBGLVL_API, "%s()\n", __func__);
  387. if (port->nr == 0)
  388. mas = 0xd0;
  389. else
  390. mas = 0xe0;
  391. memset(buf, 0, sizeof(buf));
  392. buf[0x00] = 0x04;
  393. buf[0x01] = 0x00;
  394. buf[0x02] = 0x00;
  395. buf[0x03] = 0x00;
  396. buf[0x04] = 0x04;
  397. buf[0x05] = 0x00;
  398. buf[0x06] = 0x00;
  399. buf[0x07] = 0x00;
  400. buf[0x08] = reg;
  401. buf[0x09] = 0x26;
  402. buf[0x0a] = mas;
  403. buf[0x0b] = 0xb0;
  404. buf[0x0c] = val;
  405. buf[0x0d] = 0x00;
  406. buf[0x0e] = 0x00;
  407. buf[0x0f] = 0x00;
  408. ret = saa7164_cmd_send(dev, port->ifunit.unitid, GET_LEN,
  409. EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
  410. if (ret != SAA_OK) {
  411. printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
  412. return -EIO;
  413. }
  414. ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
  415. EXU_REGISTER_ACCESS_CONTROL, len, &buf);
  416. if (ret != SAA_OK)
  417. printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
  418. //saa7164_dumphex16(dev, buf, 16);
  419. return ret == SAA_OK ? 0 : -EIO;
  420. }
  421. /* Disable the IF block AGC controls */
  422. int saa7164_api_configure_dif(struct saa7164_port *port, u32 std)
  423. {
  424. struct saa7164_dev *dev = port->dev;
  425. int ret = 0;
  426. u8 agc_disable;
  427. dprintk(DBGLVL_API, "%s(%p, 0x%x)\n", __func__, port, std);
  428. if (std & V4L2_STD_NTSC) {
  429. dprintk(DBGLVL_API, " NTSC\n");
  430. saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
  431. agc_disable = 0;
  432. } else if (std & V4L2_STD_PAL_I) {
  433. dprintk(DBGLVL_API, " PAL-I\n");
  434. saa7164_api_set_dif(port, 0x00, 0x08); /* Video Standard */
  435. agc_disable = 0;
  436. } else if (std & V4L2_STD_PAL_M) {
  437. dprintk(DBGLVL_API, " PAL-M\n");
  438. saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
  439. agc_disable = 0;
  440. } else if (std & V4L2_STD_PAL_N) {
  441. dprintk(DBGLVL_API, " PAL-N\n");
  442. saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
  443. agc_disable = 0;
  444. } else if (std & V4L2_STD_PAL_Nc) {
  445. dprintk(DBGLVL_API, " PAL-Nc\n");
  446. saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
  447. agc_disable = 0;
  448. } else if (std & V4L2_STD_PAL_B) {
  449. dprintk(DBGLVL_API, " PAL-B\n");
  450. saa7164_api_set_dif(port, 0x00, 0x02); /* Video Standard */
  451. agc_disable = 0;
  452. } else if (std & V4L2_STD_PAL_DK) {
  453. dprintk(DBGLVL_API, " PAL-DK\n");
  454. saa7164_api_set_dif(port, 0x00, 0x10); /* Video Standard */
  455. agc_disable = 0;
  456. } else if (std & V4L2_STD_SECAM_L) {
  457. dprintk(DBGLVL_API, " SECAM-L\n");
  458. saa7164_api_set_dif(port, 0x00, 0x20); /* Video Standard */
  459. agc_disable = 0;
  460. } else {
  461. /* Unknown standard, assume DTV */
  462. dprintk(DBGLVL_API, " Unknown (assuming DTV)\n");
  463. saa7164_api_set_dif(port, 0x00, 0x80); /* Undefined Video Standard */
  464. agc_disable = 1;
  465. }
  466. saa7164_api_set_dif(port, 0x48, 0xa0); /* AGC Functions 1 */
  467. saa7164_api_set_dif(port, 0xc0, agc_disable); /* AGC Output Disable */
  468. saa7164_api_set_dif(port, 0x7c, 0x04); /* CVBS EQ */
  469. saa7164_api_set_dif(port, 0x04, 0x01); /* Active */
  470. msleep(100);
  471. saa7164_api_set_dif(port, 0x04, 0x00); /* Active (again) */
  472. msleep(100);
  473. return ret;
  474. }
  475. /* Ensure the dif is in the correct state for the operating mode
  476. * (analog / dtv). We only configure the diff through the analog encoder
  477. * so when we're in digital mode we need to find the appropriate encoder
  478. * and use it to configure the DIF.
  479. */
  480. int saa7164_api_initialize_dif(struct saa7164_port *port)
  481. {
  482. struct saa7164_dev *dev = port->dev;
  483. struct saa7164_port *p = 0;
  484. int ret = -EINVAL;
  485. u32 std = 0;
  486. if (port->type == SAA7164_MPEG_ENCODER) {
  487. /* Pick any analog standard to init the diff.
  488. * we'll come back during encoder_init'
  489. * and set the correct standard if requried.
  490. */
  491. std = V4L2_STD_NTSC;
  492. } else
  493. if (port->type == SAA7164_MPEG_DVB) {
  494. if (port->nr == SAA7164_PORT_TS1)
  495. p = &dev->ports[ SAA7164_PORT_ENC1 ];
  496. else
  497. p = &dev->ports[ SAA7164_PORT_ENC2 ];
  498. } else
  499. BUG();
  500. if (p)
  501. ret = saa7164_api_configure_dif(p, std);
  502. return ret;
  503. }
  504. int saa7164_api_transition_port(struct saa7164_port *port, u8 mode)
  505. {
  506. int ret;
  507. ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid, SET_CUR,
  508. SAA_STATE_CONTROL, sizeof(mode), &mode);
  509. if (ret != SAA_OK)
  510. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  511. return ret;
  512. }
  513. int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version)
  514. {
  515. int ret;
  516. ret = saa7164_cmd_send(dev, 0, GET_CUR,
  517. GET_FW_VERSION_CONTROL, sizeof(u32), version);
  518. if (ret != SAA_OK)
  519. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  520. return ret;
  521. }
  522. int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen)
  523. {
  524. u8 reg[] = { 0x0f, 0x00 };
  525. if (buflen < 128)
  526. return -ENOMEM;
  527. /* Assumption: Hauppauge eeprom is at 0xa0 on on bus 0 */
  528. /* TODO: Pull the details from the boards struct */
  529. return saa7164_api_i2c_read(&dev->i2c_bus[0], 0xa0 >> 1, sizeof(reg),
  530. &reg[0], 128, buf);
  531. }
  532. int saa7164_api_configure_port_mpeg2ts(struct saa7164_dev *dev,
  533. struct saa7164_port *port,
  534. tmComResTSFormatDescrHeader_t *tsfmt)
  535. {
  536. dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", tsfmt->bFormatIndex);
  537. dprintk(DBGLVL_API, " bDataOffset = 0x%x\n", tsfmt->bDataOffset);
  538. dprintk(DBGLVL_API, " bPacketLength= 0x%x\n", tsfmt->bPacketLength);
  539. dprintk(DBGLVL_API, " bStrideLength= 0x%x\n", tsfmt->bStrideLength);
  540. dprintk(DBGLVL_API, " bguid = (....)\n");
  541. /* Cache the hardware configuration in the port */
  542. port->bufcounter = port->hwcfg.BARLocation;
  543. port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
  544. port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
  545. port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
  546. port->bufptr32l = port->hwcfg.BARLocation +
  547. (4 * sizeof(u32)) +
  548. (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
  549. port->bufptr32h = port->hwcfg.BARLocation +
  550. (4 * sizeof(u32)) +
  551. (sizeof(u32) * port->hwcfg.buffercount);
  552. port->bufptr64 = port->hwcfg.BARLocation +
  553. (4 * sizeof(u32)) +
  554. (sizeof(u32) * port->hwcfg.buffercount);
  555. dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
  556. port->hwcfg.BARLocation);
  557. dprintk(DBGLVL_API, " = VS_FORMAT_MPEGTS (becomes dev->ts[%d])\n",
  558. port->nr);
  559. return 0;
  560. }
  561. int saa7164_api_configure_port_mpeg2ps(struct saa7164_dev *dev,
  562. struct saa7164_port *port,
  563. tmComResPSFormatDescrHeader_t *fmt)
  564. {
  565. dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
  566. dprintk(DBGLVL_API, " wPacketLength= 0x%x\n", fmt->wPacketLength);
  567. dprintk(DBGLVL_API, " wPackLength= 0x%x\n", fmt->wPackLength);
  568. dprintk(DBGLVL_API, " bPackDataType= 0x%x\n", fmt->bPackDataType);
  569. /* Cache the hardware configuration in the port */
  570. /* TODO: CHECK THIS in the port config */
  571. port->bufcounter = port->hwcfg.BARLocation;
  572. port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
  573. port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
  574. port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
  575. port->bufptr32l = port->hwcfg.BARLocation +
  576. (4 * sizeof(u32)) +
  577. (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
  578. port->bufptr32h = port->hwcfg.BARLocation +
  579. (4 * sizeof(u32)) +
  580. (sizeof(u32) * port->hwcfg.buffercount);
  581. port->bufptr64 = port->hwcfg.BARLocation +
  582. (4 * sizeof(u32)) +
  583. (sizeof(u32) * port->hwcfg.buffercount);
  584. dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
  585. port->hwcfg.BARLocation);
  586. dprintk(DBGLVL_API, " = VS_FORMAT_MPEGPS (becomes dev->enc[%d])\n",
  587. port->nr);
  588. return 0;
  589. }
  590. int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
  591. {
  592. struct saa7164_port *tsport = 0;
  593. struct saa7164_port *encport = 0;
  594. u32 idx, next_offset;
  595. int i;
  596. tmComResDescrHeader_t *hdr, *t;
  597. tmComResExtDevDescrHeader_t *exthdr;
  598. tmComResPathDescrHeader_t *pathhdr;
  599. tmComResAntTermDescrHeader_t *anttermhdr;
  600. tmComResTunerDescrHeader_t *tunerunithdr;
  601. tmComResDMATermDescrHeader_t *vcoutputtermhdr;
  602. tmComResTSFormatDescrHeader_t *tsfmt;
  603. tmComResPSFormatDescrHeader_t *psfmt;
  604. tmComResSelDescrHeader_t *psel;
  605. tmComResProcDescrHeader_t *pdh;
  606. tmComResAFeatureDescrHeader_t *afd;
  607. tmComResEncoderDescrHeader_t *edh;
  608. u32 currpath = 0;
  609. dprintk(DBGLVL_API,
  610. "%s(?,?,%d) sizeof(tmComResDescrHeader_t) = %d bytes\n",
  611. __func__, len, (u32)sizeof(tmComResDescrHeader_t));
  612. for (idx = 0; idx < (len - sizeof(tmComResDescrHeader_t)); ) {
  613. hdr = (tmComResDescrHeader_t *)(buf + idx);
  614. if (hdr->type != CS_INTERFACE)
  615. return SAA_ERR_NOT_SUPPORTED;
  616. dprintk(DBGLVL_API, "@ 0x%x = \n", idx);
  617. switch (hdr->subtype) {
  618. case GENERAL_REQUEST:
  619. dprintk(DBGLVL_API, " GENERAL_REQUEST\n");
  620. break;
  621. case VC_TUNER_PATH:
  622. dprintk(DBGLVL_API, " VC_TUNER_PATH\n");
  623. pathhdr = (tmComResPathDescrHeader_t *)(buf + idx);
  624. dprintk(DBGLVL_API, " pathid = 0x%x\n",
  625. pathhdr->pathid);
  626. currpath = pathhdr->pathid;
  627. break;
  628. case VC_INPUT_TERMINAL:
  629. dprintk(DBGLVL_API, " VC_INPUT_TERMINAL\n");
  630. anttermhdr =
  631. (tmComResAntTermDescrHeader_t *)(buf + idx);
  632. dprintk(DBGLVL_API, " terminalid = 0x%x\n",
  633. anttermhdr->terminalid);
  634. dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
  635. anttermhdr->terminaltype);
  636. switch (anttermhdr->terminaltype) {
  637. case ITT_ANTENNA:
  638. dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
  639. break;
  640. case LINE_CONNECTOR:
  641. dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
  642. break;
  643. case SPDIF_CONNECTOR:
  644. dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
  645. break;
  646. case COMPOSITE_CONNECTOR:
  647. dprintk(DBGLVL_API,
  648. " = COMPOSITE_CONNECTOR\n");
  649. break;
  650. case SVIDEO_CONNECTOR:
  651. dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
  652. break;
  653. case COMPONENT_CONNECTOR:
  654. dprintk(DBGLVL_API,
  655. " = COMPONENT_CONNECTOR\n");
  656. break;
  657. case STANDARD_DMA:
  658. dprintk(DBGLVL_API, " = STANDARD_DMA\n");
  659. break;
  660. default:
  661. dprintk(DBGLVL_API, " = undefined (0x%x)\n",
  662. anttermhdr->terminaltype);
  663. }
  664. dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
  665. anttermhdr->assocterminal);
  666. dprintk(DBGLVL_API, " iterminal = 0x%x\n",
  667. anttermhdr->iterminal);
  668. dprintk(DBGLVL_API, " controlsize = 0x%x\n",
  669. anttermhdr->controlsize);
  670. break;
  671. case VC_OUTPUT_TERMINAL:
  672. dprintk(DBGLVL_API, " VC_OUTPUT_TERMINAL\n");
  673. vcoutputtermhdr =
  674. (tmComResDMATermDescrHeader_t *)(buf + idx);
  675. dprintk(DBGLVL_API, " unitid = 0x%x\n",
  676. vcoutputtermhdr->unitid);
  677. dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
  678. vcoutputtermhdr->terminaltype);
  679. switch (vcoutputtermhdr->terminaltype) {
  680. case ITT_ANTENNA:
  681. dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
  682. break;
  683. case LINE_CONNECTOR:
  684. dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
  685. break;
  686. case SPDIF_CONNECTOR:
  687. dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
  688. break;
  689. case COMPOSITE_CONNECTOR:
  690. dprintk(DBGLVL_API,
  691. " = COMPOSITE_CONNECTOR\n");
  692. break;
  693. case SVIDEO_CONNECTOR:
  694. dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
  695. break;
  696. case COMPONENT_CONNECTOR:
  697. dprintk(DBGLVL_API,
  698. " = COMPONENT_CONNECTOR\n");
  699. break;
  700. case STANDARD_DMA:
  701. dprintk(DBGLVL_API, " = STANDARD_DMA\n");
  702. break;
  703. default:
  704. dprintk(DBGLVL_API, " = undefined (0x%x)\n",
  705. vcoutputtermhdr->terminaltype);
  706. }
  707. dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
  708. vcoutputtermhdr->assocterminal);
  709. dprintk(DBGLVL_API, " sourceid = 0x%x\n",
  710. vcoutputtermhdr->sourceid);
  711. dprintk(DBGLVL_API, " iterminal = 0x%x\n",
  712. vcoutputtermhdr->iterminal);
  713. dprintk(DBGLVL_API, " BARLocation = 0x%x\n",
  714. vcoutputtermhdr->BARLocation);
  715. dprintk(DBGLVL_API, " flags = 0x%x\n",
  716. vcoutputtermhdr->flags);
  717. dprintk(DBGLVL_API, " interruptid = 0x%x\n",
  718. vcoutputtermhdr->interruptid);
  719. dprintk(DBGLVL_API, " buffercount = 0x%x\n",
  720. vcoutputtermhdr->buffercount);
  721. dprintk(DBGLVL_API, " metadatasize = 0x%x\n",
  722. vcoutputtermhdr->metadatasize);
  723. dprintk(DBGLVL_API, " controlsize = 0x%x\n",
  724. vcoutputtermhdr->controlsize);
  725. dprintk(DBGLVL_API, " numformats = 0x%x\n",
  726. vcoutputtermhdr->numformats);
  727. t = (tmComResDescrHeader_t *)
  728. ((tmComResDMATermDescrHeader_t *)(buf + idx));
  729. next_offset = idx + (vcoutputtermhdr->len);
  730. for (i = 0; i < vcoutputtermhdr->numformats; i++) {
  731. t = (tmComResDescrHeader_t *)
  732. (buf + next_offset);
  733. switch (t->subtype) {
  734. case VS_FORMAT_MPEG2TS:
  735. tsfmt =
  736. (tmComResTSFormatDescrHeader_t *)t;
  737. if (currpath == 1)
  738. tsport = &dev->ports[ SAA7164_PORT_TS1 ];
  739. else
  740. tsport = &dev->ports[ SAA7164_PORT_TS2 ];
  741. memcpy(&tsport->hwcfg, vcoutputtermhdr,
  742. sizeof(*vcoutputtermhdr));
  743. saa7164_api_configure_port_mpeg2ts(dev,
  744. tsport, tsfmt);
  745. break;
  746. case VS_FORMAT_MPEG2PS:
  747. psfmt =
  748. (tmComResPSFormatDescrHeader_t *)t;
  749. if (currpath == 1)
  750. encport = &dev->ports[ SAA7164_PORT_ENC1 ];
  751. else
  752. encport = &dev->ports[ SAA7164_PORT_ENC2 ];
  753. memcpy(&encport->hwcfg, vcoutputtermhdr,
  754. sizeof(*vcoutputtermhdr));
  755. saa7164_api_configure_port_mpeg2ps(dev,
  756. encport, psfmt);
  757. break;
  758. case VS_FORMAT_VBI:
  759. dprintk(DBGLVL_API,
  760. " = VS_FORMAT_VBI\n");
  761. break;
  762. case VS_FORMAT_RDS:
  763. dprintk(DBGLVL_API,
  764. " = VS_FORMAT_RDS\n");
  765. break;
  766. case VS_FORMAT_UNCOMPRESSED:
  767. dprintk(DBGLVL_API,
  768. " = VS_FORMAT_UNCOMPRESSED\n");
  769. break;
  770. case VS_FORMAT_TYPE:
  771. dprintk(DBGLVL_API,
  772. " = VS_FORMAT_TYPE\n");
  773. break;
  774. default:
  775. dprintk(DBGLVL_API,
  776. " = undefined (0x%x)\n",
  777. t->subtype);
  778. }
  779. next_offset += t->len;
  780. }
  781. break;
  782. case TUNER_UNIT:
  783. dprintk(DBGLVL_API, " TUNER_UNIT\n");
  784. tunerunithdr =
  785. (tmComResTunerDescrHeader_t *)(buf + idx);
  786. dprintk(DBGLVL_API, " unitid = 0x%x\n",
  787. tunerunithdr->unitid);
  788. dprintk(DBGLVL_API, " sourceid = 0x%x\n",
  789. tunerunithdr->sourceid);
  790. dprintk(DBGLVL_API, " iunit = 0x%x\n",
  791. tunerunithdr->iunit);
  792. dprintk(DBGLVL_API, " tuningstandards = 0x%x\n",
  793. tunerunithdr->tuningstandards);
  794. dprintk(DBGLVL_API, " controlsize = 0x%x\n",
  795. tunerunithdr->controlsize);
  796. dprintk(DBGLVL_API, " controls = 0x%x\n",
  797. tunerunithdr->controls);
  798. if (tunerunithdr->unitid == tunerunithdr->iunit) {
  799. if (currpath == 1)
  800. encport = &dev->ports[ SAA7164_PORT_ENC1 ];
  801. else
  802. encport = &dev->ports[ SAA7164_PORT_ENC2 ];
  803. memcpy(&encport->tunerunit, tunerunithdr,
  804. sizeof(tmComResTunerDescrHeader_t));
  805. dprintk(DBGLVL_API, " (becomes dev->enc[%d] tuner)\n", encport->nr);
  806. }
  807. break;
  808. case VC_SELECTOR_UNIT:
  809. psel = (tmComResSelDescrHeader_t *)(buf + idx);
  810. dprintk(DBGLVL_API, " VC_SELECTOR_UNIT\n");
  811. dprintk(DBGLVL_API, " unitid = 0x%x\n",
  812. psel->unitid);
  813. dprintk(DBGLVL_API, " nrinpins = 0x%x\n",
  814. psel->nrinpins);
  815. dprintk(DBGLVL_API, " sourceid = 0x%x\n",
  816. psel->sourceid);
  817. break;
  818. case VC_PROCESSING_UNIT:
  819. pdh = (tmComResProcDescrHeader_t *)(buf + idx);
  820. dprintk(DBGLVL_API, " VC_PROCESSING_UNIT\n");
  821. dprintk(DBGLVL_API, " unitid = 0x%x\n",
  822. pdh->unitid);
  823. dprintk(DBGLVL_API, " sourceid = 0x%x\n",
  824. pdh->sourceid);
  825. dprintk(DBGLVL_API, " controlsize = 0x%x\n",
  826. pdh->controlsize);
  827. if (pdh->controlsize == 0x04) {
  828. if (currpath == 1)
  829. encport = &dev->ports[ SAA7164_PORT_ENC1 ];
  830. else
  831. encport = &dev->ports[ SAA7164_PORT_ENC2 ];
  832. memcpy(&encport->vidproc, pdh,
  833. sizeof(tmComResProcDescrHeader_t));
  834. dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
  835. }
  836. break;
  837. case FEATURE_UNIT:
  838. afd = (tmComResAFeatureDescrHeader_t *)(buf + idx);
  839. dprintk(DBGLVL_API, " FEATURE_UNIT\n");
  840. dprintk(DBGLVL_API, " unitid = 0x%x\n",
  841. afd->unitid);
  842. dprintk(DBGLVL_API, " sourceid = 0x%x\n",
  843. afd->sourceid);
  844. dprintk(DBGLVL_API, " controlsize = 0x%x\n",
  845. afd->controlsize);
  846. if (currpath == 1)
  847. encport = &dev->ports[ SAA7164_PORT_ENC1 ];
  848. else
  849. encport = &dev->ports[ SAA7164_PORT_ENC2 ];
  850. memcpy(&encport->audfeat, afd,
  851. sizeof(tmComResAFeatureDescrHeader_t));
  852. dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
  853. break;
  854. case ENCODER_UNIT:
  855. edh = (tmComResEncoderDescrHeader_t *)(buf + idx);
  856. dprintk(DBGLVL_API, " ENCODER_UNIT\n");
  857. dprintk(DBGLVL_API, " subtype = 0x%x\n", edh->subtype);
  858. dprintk(DBGLVL_API, " unitid = 0x%x\n", edh->unitid);
  859. dprintk(DBGLVL_API, " vsourceid = 0x%x\n", edh->vsourceid);
  860. dprintk(DBGLVL_API, " asourceid = 0x%x\n", edh->asourceid);
  861. dprintk(DBGLVL_API, " iunit = 0x%x\n", edh->iunit);
  862. if (edh->iunit == edh->unitid) {
  863. if (currpath == 1)
  864. encport = &dev->ports[ SAA7164_PORT_ENC1 ];
  865. else
  866. encport = &dev->ports[ SAA7164_PORT_ENC2 ];
  867. memcpy(&encport->encunit, edh,
  868. sizeof(tmComResEncoderDescrHeader_t));
  869. dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
  870. }
  871. break;
  872. case EXTENSION_UNIT:
  873. dprintk(DBGLVL_API, " EXTENSION_UNIT\n");
  874. exthdr = (tmComResExtDevDescrHeader_t *)(buf + idx);
  875. dprintk(DBGLVL_API, " unitid = 0x%x\n",
  876. exthdr->unitid);
  877. dprintk(DBGLVL_API, " deviceid = 0x%x\n",
  878. exthdr->deviceid);
  879. dprintk(DBGLVL_API, " devicetype = 0x%x\n",
  880. exthdr->devicetype);
  881. if (exthdr->devicetype & 0x1)
  882. dprintk(DBGLVL_API, " = Decoder Device\n");
  883. if (exthdr->devicetype & 0x2)
  884. dprintk(DBGLVL_API, " = GPIO Source\n");
  885. if (exthdr->devicetype & 0x4)
  886. dprintk(DBGLVL_API, " = Video Decoder\n");
  887. if (exthdr->devicetype & 0x8)
  888. dprintk(DBGLVL_API, " = Audio Decoder\n");
  889. if (exthdr->devicetype & 0x20)
  890. dprintk(DBGLVL_API, " = Crossbar\n");
  891. if (exthdr->devicetype & 0x40)
  892. dprintk(DBGLVL_API, " = Tuner\n");
  893. if (exthdr->devicetype & 0x80)
  894. dprintk(DBGLVL_API, " = IF PLL\n");
  895. if (exthdr->devicetype & 0x100)
  896. dprintk(DBGLVL_API, " = Demodulator\n");
  897. if (exthdr->devicetype & 0x200)
  898. dprintk(DBGLVL_API, " = RDS Decoder\n");
  899. if (exthdr->devicetype & 0x400)
  900. dprintk(DBGLVL_API, " = Encoder\n");
  901. if (exthdr->devicetype & 0x800)
  902. dprintk(DBGLVL_API, " = IR Decoder\n");
  903. if (exthdr->devicetype & 0x1000)
  904. dprintk(DBGLVL_API, " = EEPROM\n");
  905. if (exthdr->devicetype & 0x2000)
  906. dprintk(DBGLVL_API,
  907. " = VBI Decoder\n");
  908. if (exthdr->devicetype & 0x10000)
  909. dprintk(DBGLVL_API,
  910. " = Streaming Device\n");
  911. if (exthdr->devicetype & 0x20000)
  912. dprintk(DBGLVL_API,
  913. " = DRM Device\n");
  914. if (exthdr->devicetype & 0x40000000)
  915. dprintk(DBGLVL_API,
  916. " = Generic Device\n");
  917. if (exthdr->devicetype & 0x80000000)
  918. dprintk(DBGLVL_API,
  919. " = Config Space Device\n");
  920. dprintk(DBGLVL_API, " numgpiopins = 0x%x\n",
  921. exthdr->numgpiopins);
  922. dprintk(DBGLVL_API, " numgpiogroups = 0x%x\n",
  923. exthdr->numgpiogroups);
  924. dprintk(DBGLVL_API, " controlsize = 0x%x\n",
  925. exthdr->controlsize);
  926. if (exthdr->devicetype & 0x80) {
  927. if (currpath == 1)
  928. encport = &dev->ports[ SAA7164_PORT_ENC1 ];
  929. else
  930. encport = &dev->ports[ SAA7164_PORT_ENC2 ];
  931. memcpy(&encport->ifunit, exthdr,
  932. sizeof(tmComResExtDevDescrHeader_t));
  933. dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
  934. }
  935. break;
  936. case PVC_INFRARED_UNIT:
  937. dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n");
  938. break;
  939. case DRM_UNIT:
  940. dprintk(DBGLVL_API, " DRM_UNIT\n");
  941. break;
  942. default:
  943. dprintk(DBGLVL_API, "default %d\n", hdr->subtype);
  944. }
  945. dprintk(DBGLVL_API, " 1.%x\n", hdr->len);
  946. dprintk(DBGLVL_API, " 2.%x\n", hdr->type);
  947. dprintk(DBGLVL_API, " 3.%x\n", hdr->subtype);
  948. dprintk(DBGLVL_API, " 4.%x\n", hdr->unitid);
  949. idx += hdr->len;
  950. }
  951. return 0;
  952. }
  953. int saa7164_api_enum_subdevs(struct saa7164_dev *dev)
  954. {
  955. int ret;
  956. u32 buflen = 0;
  957. u8 *buf;
  958. dprintk(DBGLVL_API, "%s()\n", __func__);
  959. /* Get the total descriptor length */
  960. ret = saa7164_cmd_send(dev, 0, GET_LEN,
  961. GET_DESCRIPTORS_CONTROL, sizeof(buflen), &buflen);
  962. if (ret != SAA_OK)
  963. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  964. dprintk(DBGLVL_API, "%s() total descriptor size = %d bytes.\n",
  965. __func__, buflen);
  966. /* Allocate enough storage for all of the descs */
  967. buf = kzalloc(buflen, GFP_KERNEL);
  968. if (buf == NULL)
  969. return SAA_ERR_NO_RESOURCES;
  970. /* Retrieve them */
  971. ret = saa7164_cmd_send(dev, 0, GET_CUR,
  972. GET_DESCRIPTORS_CONTROL, buflen, buf);
  973. if (ret != SAA_OK) {
  974. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
  975. goto out;
  976. }
  977. if (saa_debug & DBGLVL_API)
  978. saa7164_dumphex16(dev, buf, (buflen/16)*16);
  979. saa7164_api_dump_subdevs(dev, buf, buflen);
  980. out:
  981. kfree(buf);
  982. return ret;
  983. }
  984. int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
  985. u32 datalen, u8 *data)
  986. {
  987. struct saa7164_dev *dev = bus->dev;
  988. u16 len = 0;
  989. int unitid;
  990. u32 regval;
  991. u8 buf[256];
  992. int ret;
  993. dprintk(DBGLVL_API, "%s()\n", __func__);
  994. if (reglen > 4)
  995. return -EIO;
  996. if (reglen == 1)
  997. regval = *(reg);
  998. else
  999. if (reglen == 2)
  1000. regval = ((*(reg) << 8) || *(reg+1));
  1001. else
  1002. if (reglen == 3)
  1003. regval = ((*(reg) << 16) | (*(reg+1) << 8) | *(reg+2));
  1004. else
  1005. if (reglen == 4)
  1006. regval = ((*(reg) << 24) | (*(reg+1) << 16) |
  1007. (*(reg+2) << 8) | *(reg+3));
  1008. /* Prepare the send buffer */
  1009. /* Bytes 00-03 source register length
  1010. * 04-07 source bytes to read
  1011. * 08... register address
  1012. */
  1013. memset(buf, 0, sizeof(buf));
  1014. memcpy((buf + 2 * sizeof(u32) + 0), reg, reglen);
  1015. *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
  1016. *((u32 *)(buf + 1 * sizeof(u32))) = datalen;
  1017. unitid = saa7164_i2caddr_to_unitid(bus, addr);
  1018. if (unitid < 0) {
  1019. printk(KERN_ERR
  1020. "%s() error, cannot translate regaddr 0x%x to unitid\n",
  1021. __func__, addr);
  1022. return -EIO;
  1023. }
  1024. ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
  1025. EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
  1026. if (ret != SAA_OK) {
  1027. printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
  1028. return -EIO;
  1029. }
  1030. dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
  1031. if (saa_debug & DBGLVL_I2C)
  1032. saa7164_dumphex16(dev, buf, 2 * 16);
  1033. ret = saa7164_cmd_send(bus->dev, unitid, GET_CUR,
  1034. EXU_REGISTER_ACCESS_CONTROL, len, &buf);
  1035. if (ret != SAA_OK)
  1036. printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
  1037. else {
  1038. if (saa_debug & DBGLVL_I2C)
  1039. saa7164_dumphex16(dev, buf, sizeof(buf));
  1040. memcpy(data, (buf + 2 * sizeof(u32) + reglen), datalen);
  1041. }
  1042. return ret == SAA_OK ? 0 : -EIO;
  1043. }
  1044. /* For a given 8 bit i2c address device, write the buffer */
  1045. int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr, u32 datalen,
  1046. u8 *data)
  1047. {
  1048. struct saa7164_dev *dev = bus->dev;
  1049. u16 len = 0;
  1050. int unitid;
  1051. int reglen;
  1052. u8 buf[256];
  1053. int ret;
  1054. dprintk(DBGLVL_API, "%s()\n", __func__);
  1055. if ((datalen == 0) || (datalen > 232))
  1056. return -EIO;
  1057. memset(buf, 0, sizeof(buf));
  1058. unitid = saa7164_i2caddr_to_unitid(bus, addr);
  1059. if (unitid < 0) {
  1060. printk(KERN_ERR
  1061. "%s() error, cannot translate regaddr 0x%x to unitid\n",
  1062. __func__, addr);
  1063. return -EIO;
  1064. }
  1065. reglen = saa7164_i2caddr_to_reglen(bus, addr);
  1066. if (reglen < 0) {
  1067. printk(KERN_ERR
  1068. "%s() error, cannot translate regaddr to reglen\n",
  1069. __func__);
  1070. return -EIO;
  1071. }
  1072. ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
  1073. EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
  1074. if (ret != SAA_OK) {
  1075. printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
  1076. return -EIO;
  1077. }
  1078. dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
  1079. /* Prepare the send buffer */
  1080. /* Bytes 00-03 dest register length
  1081. * 04-07 dest bytes to write
  1082. * 08... register address
  1083. */
  1084. *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
  1085. *((u32 *)(buf + 1 * sizeof(u32))) = datalen - reglen;
  1086. memcpy((buf + 2 * sizeof(u32)), data, datalen);
  1087. if (saa_debug & DBGLVL_I2C)
  1088. saa7164_dumphex16(dev, buf, sizeof(buf));
  1089. ret = saa7164_cmd_send(bus->dev, unitid, SET_CUR,
  1090. EXU_REGISTER_ACCESS_CONTROL, len, &buf);
  1091. if (ret != SAA_OK)
  1092. printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
  1093. return ret == SAA_OK ? 0 : -EIO;
  1094. }
  1095. int saa7164_api_modify_gpio(struct saa7164_dev *dev, u8 unitid,
  1096. u8 pin, u8 state)
  1097. {
  1098. int ret;
  1099. tmComResGPIO_t t;
  1100. dprintk(DBGLVL_API, "%s(0x%x, %d, %d)\n",
  1101. __func__, unitid, pin, state);
  1102. if ((pin > 7) || (state > 2))
  1103. return SAA_ERR_BAD_PARAMETER;
  1104. t.pin = pin;
  1105. t.state = state;
  1106. ret = saa7164_cmd_send(dev, unitid, SET_CUR,
  1107. EXU_GPIO_CONTROL, sizeof(t), &t);
  1108. if (ret != SAA_OK)
  1109. printk(KERN_ERR "%s() error, ret = 0x%x\n",
  1110. __func__, ret);
  1111. return ret;
  1112. }
  1113. int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid,
  1114. u8 pin)
  1115. {
  1116. return saa7164_api_modify_gpio(dev, unitid, pin, 1);
  1117. }
  1118. int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid,
  1119. u8 pin)
  1120. {
  1121. return saa7164_api_modify_gpio(dev, unitid, pin, 0);
  1122. }