saa7164-api.c 43 KB

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