saa7164-api.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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)
  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. printk(KERN_INFO "saa7164[%d]-FWMSG: %s", dev->nr, 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. }