saa7164-api.c 37 KB

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