az6027.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /* DVB USB compliant Linux driver for the AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)
  2. * receiver.
  3. *
  4. * Copyright (C) 2009 Adams.Xu <adams.xu@azwave.com.cn>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation, version 2.
  9. *
  10. * see Documentation/dvb/README.dvb-usb for more information
  11. */
  12. #include "az6027.h"
  13. /* Demodulator */
  14. #include "stb0899_drv.h"
  15. #include "stb0899_reg.h"
  16. #include "stb0899_cfg.h"
  17. /* Tuner */
  18. #include "stb6100.h"
  19. #include "stb6100_cfg.h"
  20. #include "dvb_ca_en50221.h"
  21. /* debug */
  22. int dvb_usb_az6027_debug;
  23. module_param_named(debug,dvb_usb_az6027_debug, int, 0644);
  24. MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
  25. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  26. struct az6027_device_state {
  27. struct dvb_ca_en50221 ca;
  28. struct mutex ca_mutex;
  29. u8 power_state;
  30. };
  31. static const struct stb0899_s1_reg az6027_stb0899_s1_init_1[] = {
  32. // 0x0000000b , /* SYSREG */
  33. { STB0899_DEV_ID , 0x30 },
  34. { STB0899_DISCNTRL1 , 0x32 },
  35. { STB0899_DISCNTRL2 , 0x80 },
  36. { STB0899_DISRX_ST0 , 0x04 },
  37. { STB0899_DISRX_ST1 , 0x00 },
  38. { STB0899_DISPARITY , 0x00 },
  39. { STB0899_DISFIFO , 0x00 },
  40. { STB0899_DISSTATUS , 0x20 },
  41. { STB0899_DISF22 , 0x99 },
  42. { STB0899_DISF22RX , 0xa8 },
  43. //SYSREG ?
  44. { STB0899_ACRPRESC , 0x11 },
  45. { STB0899_ACRDIV1 , 0x0a },
  46. { STB0899_ACRDIV2 , 0x05 },
  47. { STB0899_DACR1 , 0x00 },
  48. { STB0899_DACR2 , 0x00 },
  49. { STB0899_OUTCFG , 0x00 },
  50. { STB0899_MODECFG , 0x00 },
  51. { STB0899_IRQSTATUS_3 , 0xfe },
  52. { STB0899_IRQSTATUS_2 , 0x03 },
  53. { STB0899_IRQSTATUS_1 , 0x7c },
  54. { STB0899_IRQSTATUS_0 , 0xf4 },
  55. { STB0899_IRQMSK_3 , 0xf3 },
  56. { STB0899_IRQMSK_2 , 0xfc },
  57. { STB0899_IRQMSK_1 , 0xff },
  58. { STB0899_IRQMSK_0 , 0xff },
  59. { STB0899_IRQCFG , 0x00 },
  60. { STB0899_I2CCFG , 0x88 },
  61. { STB0899_I2CRPT , 0x58 },
  62. { STB0899_IOPVALUE5 , 0x00 },
  63. { STB0899_IOPVALUE4 , 0x33 },
  64. { STB0899_IOPVALUE3 , 0x6d },
  65. { STB0899_IOPVALUE2 , 0x90 },
  66. { STB0899_IOPVALUE1 , 0x60 },
  67. { STB0899_IOPVALUE0 , 0x00 },
  68. { STB0899_GPIO00CFG , 0x82 },
  69. { STB0899_GPIO01CFG , 0x82 },
  70. { STB0899_GPIO02CFG , 0x82 },
  71. { STB0899_GPIO03CFG , 0x82 },
  72. { STB0899_GPIO04CFG , 0x82 },
  73. { STB0899_GPIO05CFG , 0x82 },
  74. { STB0899_GPIO06CFG , 0x82 },
  75. { STB0899_GPIO07CFG , 0x82 },
  76. { STB0899_GPIO08CFG , 0x82 },
  77. { STB0899_GPIO09CFG , 0x82 },
  78. { STB0899_GPIO10CFG , 0x82 },
  79. { STB0899_GPIO11CFG , 0x82 },
  80. { STB0899_GPIO12CFG , 0x82 },
  81. { STB0899_GPIO13CFG , 0x82 },
  82. { STB0899_GPIO14CFG , 0x82 },
  83. { STB0899_GPIO15CFG , 0x82 },
  84. { STB0899_GPIO16CFG , 0x82 },
  85. { STB0899_GPIO17CFG , 0x82 },
  86. { STB0899_GPIO18CFG , 0x82 },
  87. { STB0899_GPIO19CFG , 0x82 },
  88. { STB0899_GPIO20CFG , 0x82 },
  89. { STB0899_SDATCFG , 0xb8 },
  90. { STB0899_SCLTCFG , 0xba },
  91. { STB0899_AGCRFCFG , 0x1c }, /* 0x11 */
  92. { STB0899_GPIO22 , 0x82 }, /* AGCBB2CFG */
  93. { STB0899_GPIO21 , 0x91 }, /* AGCBB1CFG */
  94. { STB0899_DIRCLKCFG , 0x82 },
  95. { STB0899_CLKOUT27CFG , 0x7e },
  96. { STB0899_STDBYCFG , 0x82 },
  97. { STB0899_CS0CFG , 0x82 },
  98. { STB0899_CS1CFG , 0x82 },
  99. { STB0899_DISEQCOCFG , 0x20 },
  100. { STB0899_GPIO32CFG , 0x82 },
  101. { STB0899_GPIO33CFG , 0x82 },
  102. { STB0899_GPIO34CFG , 0x82 },
  103. { STB0899_GPIO35CFG , 0x82 },
  104. { STB0899_GPIO36CFG , 0x82 },
  105. { STB0899_GPIO37CFG , 0x82 },
  106. { STB0899_GPIO38CFG , 0x82 },
  107. { STB0899_GPIO39CFG , 0x82 },
  108. { STB0899_NCOARSE , 0x17 }, /* 0x15 = 27 Mhz Clock, F/3 = 198MHz, F/6 = 99MHz */
  109. { STB0899_SYNTCTRL , 0x02 }, /* 0x00 = CLK from CLKI, 0x02 = CLK from XTALI */
  110. { STB0899_FILTCTRL , 0x00 },
  111. { STB0899_SYSCTRL , 0x01 },
  112. { STB0899_STOPCLK1 , 0x20 },
  113. { STB0899_STOPCLK2 , 0x00 },
  114. { STB0899_INTBUFSTATUS , 0x00 },
  115. { STB0899_INTBUFCTRL , 0x0a },
  116. { 0xffff , 0xff },
  117. };
  118. static const struct stb0899_s1_reg az6027_stb0899_s1_init_3[] = {
  119. { STB0899_DEMOD , 0x00 },
  120. { STB0899_RCOMPC , 0xc9 },
  121. { STB0899_AGC1CN , 0x01 },
  122. { STB0899_AGC1REF , 0x10 },
  123. { STB0899_RTC , 0x23 },
  124. { STB0899_TMGCFG , 0x4e },
  125. { STB0899_AGC2REF , 0x34 },
  126. { STB0899_TLSR , 0x84 },
  127. { STB0899_CFD , 0xf7 },
  128. { STB0899_ACLC , 0x87 },
  129. { STB0899_BCLC , 0x94 },
  130. { STB0899_EQON , 0x41 },
  131. { STB0899_LDT , 0xf1 },
  132. { STB0899_LDT2 , 0xe3 },
  133. { STB0899_EQUALREF , 0xb4 },
  134. { STB0899_TMGRAMP , 0x10 },
  135. { STB0899_TMGTHD , 0x30 },
  136. { STB0899_IDCCOMP , 0xfd },
  137. { STB0899_QDCCOMP , 0xff },
  138. { STB0899_POWERI , 0x0c },
  139. { STB0899_POWERQ , 0x0f },
  140. { STB0899_RCOMP , 0x6c },
  141. { STB0899_AGCIQIN , 0x80 },
  142. { STB0899_AGC2I1 , 0x06 },
  143. { STB0899_AGC2I2 , 0x00 },
  144. { STB0899_TLIR , 0x30 },
  145. { STB0899_RTF , 0x7f },
  146. { STB0899_DSTATUS , 0x00 },
  147. { STB0899_LDI , 0xbc },
  148. { STB0899_CFRM , 0xea },
  149. { STB0899_CFRL , 0x31 },
  150. { STB0899_NIRM , 0x2b },
  151. { STB0899_NIRL , 0x80 },
  152. { STB0899_ISYMB , 0x1d },
  153. { STB0899_QSYMB , 0xa6 },
  154. { STB0899_SFRH , 0x2f },
  155. { STB0899_SFRM , 0x68 },
  156. { STB0899_SFRL , 0x40 },
  157. { STB0899_SFRUPH , 0x2f },
  158. { STB0899_SFRUPM , 0x68 },
  159. { STB0899_SFRUPL , 0x40 },
  160. { STB0899_EQUAI1 , 0x02 },
  161. { STB0899_EQUAQ1 , 0xff },
  162. { STB0899_EQUAI2 , 0x04 },
  163. { STB0899_EQUAQ2 , 0x05 },
  164. { STB0899_EQUAI3 , 0x02 },
  165. { STB0899_EQUAQ3 , 0xfd },
  166. { STB0899_EQUAI4 , 0x03 },
  167. { STB0899_EQUAQ4 , 0x07 },
  168. { STB0899_EQUAI5 , 0x08 },
  169. { STB0899_EQUAQ5 , 0xf5 },
  170. { STB0899_DSTATUS2 , 0x00 },
  171. { STB0899_VSTATUS , 0x00 },
  172. { STB0899_VERROR , 0x86 },
  173. { STB0899_IQSWAP , 0x2a },
  174. { STB0899_ECNT1M , 0x00 },
  175. { STB0899_ECNT1L , 0x00 },
  176. { STB0899_ECNT2M , 0x00 },
  177. { STB0899_ECNT2L , 0x00 },
  178. { STB0899_ECNT3M , 0x0a },
  179. { STB0899_ECNT3L , 0xad },
  180. { STB0899_FECAUTO1 , 0x06 },
  181. { STB0899_FECM , 0x01 },
  182. { STB0899_VTH12 , 0xb0 },
  183. { STB0899_VTH23 , 0x7a },
  184. { STB0899_VTH34 , 0x58 },
  185. { STB0899_VTH56 , 0x38 },
  186. { STB0899_VTH67 , 0x34 },
  187. { STB0899_VTH78 , 0x24 },
  188. { STB0899_PRVIT , 0xff },
  189. { STB0899_VITSYNC , 0x19 },
  190. { STB0899_RSULC , 0xb1 }, /* DVB = 0xb1, DSS = 0xa1 */
  191. { STB0899_TSULC , 0x42 },
  192. { STB0899_RSLLC , 0x41 },
  193. { STB0899_TSLPL , 0x12 },
  194. { STB0899_TSCFGH , 0x0c },
  195. { STB0899_TSCFGM , 0x00 },
  196. { STB0899_TSCFGL , 0x00 },
  197. { STB0899_TSOUT , 0x69 }, /* 0x0d for CAM */
  198. { STB0899_RSSYNCDEL , 0x00 },
  199. { STB0899_TSINHDELH , 0x02 },
  200. { STB0899_TSINHDELM , 0x00 },
  201. { STB0899_TSINHDELL , 0x00 },
  202. { STB0899_TSLLSTKM , 0x1b },
  203. { STB0899_TSLLSTKL , 0xb3 },
  204. { STB0899_TSULSTKM , 0x00 },
  205. { STB0899_TSULSTKL , 0x00 },
  206. { STB0899_PCKLENUL , 0xbc },
  207. { STB0899_PCKLENLL , 0xcc },
  208. { STB0899_RSPCKLEN , 0xbd },
  209. { STB0899_TSSTATUS , 0x90 },
  210. { STB0899_ERRCTRL1 , 0xb6 },
  211. { STB0899_ERRCTRL2 , 0x95 },
  212. { STB0899_ERRCTRL3 , 0x8d },
  213. { STB0899_DMONMSK1 , 0x27 },
  214. { STB0899_DMONMSK0 , 0x03 },
  215. { STB0899_DEMAPVIT , 0x5c },
  216. { STB0899_PLPARM , 0x19 },
  217. { STB0899_PDELCTRL , 0x48 },
  218. { STB0899_PDELCTRL2 , 0x00 },
  219. { STB0899_BBHCTRL1 , 0x00 },
  220. { STB0899_BBHCTRL2 , 0x00 },
  221. { STB0899_HYSTTHRESH , 0x77 },
  222. { STB0899_MATCSTM , 0x00 },
  223. { STB0899_MATCSTL , 0x00 },
  224. { STB0899_UPLCSTM , 0x00 },
  225. { STB0899_UPLCSTL , 0x00 },
  226. { STB0899_DFLCSTM , 0x00 },
  227. { STB0899_DFLCSTL , 0x00 },
  228. { STB0899_SYNCCST , 0x00 },
  229. { STB0899_SYNCDCSTM , 0x00 },
  230. { STB0899_SYNCDCSTL , 0x00 },
  231. { STB0899_ISI_ENTRY , 0x00 },
  232. { STB0899_ISI_BIT_EN , 0x00 },
  233. { STB0899_MATSTRM , 0xf0 },
  234. { STB0899_MATSTRL , 0x02 },
  235. { STB0899_UPLSTRM , 0x45 },
  236. { STB0899_UPLSTRL , 0x60 },
  237. { STB0899_DFLSTRM , 0xe3 },
  238. { STB0899_DFLSTRL , 0x00 },
  239. { STB0899_SYNCSTR , 0x47 },
  240. { STB0899_SYNCDSTRM , 0x05 },
  241. { STB0899_SYNCDSTRL , 0x18 },
  242. { STB0899_CFGPDELSTATUS1 , 0x19 },
  243. { STB0899_CFGPDELSTATUS2 , 0x2b },
  244. { STB0899_BBFERRORM , 0x00 },
  245. { STB0899_BBFERRORL , 0x01 },
  246. { STB0899_UPKTERRORM , 0x00 },
  247. { STB0899_UPKTERRORL , 0x00 },
  248. { 0xffff , 0xff },
  249. };
  250. struct stb0899_config az6027_stb0899_config = {
  251. .init_dev = az6027_stb0899_s1_init_1,
  252. .init_s2_demod = stb0899_s2_init_2,
  253. .init_s1_demod = az6027_stb0899_s1_init_3,
  254. .init_s2_fec = stb0899_s2_init_4,
  255. .init_tst = stb0899_s1_init_5,
  256. .demod_address = 0xd0,//0x68, /* 0xd0 >> 1 */
  257. .xtal_freq = 27000000,
  258. .inversion = IQ_SWAP_ON, /* 1 */
  259. .lo_clk = 76500000,
  260. .hi_clk = 99000000,
  261. .esno_ave = STB0899_DVBS2_ESNO_AVE,
  262. .esno_quant = STB0899_DVBS2_ESNO_QUANT,
  263. .avframes_coarse = STB0899_DVBS2_AVFRAMES_COARSE,
  264. .avframes_fine = STB0899_DVBS2_AVFRAMES_FINE,
  265. .miss_threshold = STB0899_DVBS2_MISS_THRESHOLD,
  266. .uwp_threshold_acq = STB0899_DVBS2_UWP_THRESHOLD_ACQ,
  267. .uwp_threshold_track = STB0899_DVBS2_UWP_THRESHOLD_TRACK,
  268. .uwp_threshold_sof = STB0899_DVBS2_UWP_THRESHOLD_SOF,
  269. .sof_search_timeout = STB0899_DVBS2_SOF_SEARCH_TIMEOUT,
  270. .btr_nco_bits = STB0899_DVBS2_BTR_NCO_BITS,
  271. .btr_gain_shift_offset = STB0899_DVBS2_BTR_GAIN_SHIFT_OFFSET,
  272. .crl_nco_bits = STB0899_DVBS2_CRL_NCO_BITS,
  273. .ldpc_max_iter = STB0899_DVBS2_LDPC_MAX_ITER,
  274. .tuner_get_frequency = stb6100_get_frequency,
  275. .tuner_set_frequency = stb6100_set_frequency,
  276. .tuner_set_bandwidth = stb6100_set_bandwidth,
  277. .tuner_get_bandwidth = stb6100_get_bandwidth,
  278. .tuner_set_rfsiggain = NULL,
  279. };
  280. struct stb6100_config az6027_stb6100_config = {
  281. .tuner_address = 0xc0,
  282. .refclock = 27000000,
  283. };
  284. /* check for mutex FIXME */
  285. int az6027_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
  286. {
  287. int ret = -1;
  288. if(mutex_lock_interruptible(&d->usb_mutex)) {
  289. return -EAGAIN;
  290. }
  291. ret = usb_control_msg(d->udev,
  292. usb_rcvctrlpipe(d->udev,0),
  293. req,
  294. USB_TYPE_VENDOR | USB_DIR_IN,
  295. value,index,b,blen,
  296. 2000);
  297. if (ret < 0) {
  298. warn("usb in operation failed. (%d)", ret);
  299. ret = -EIO;
  300. } else
  301. ret = 0;
  302. deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
  303. debug_dump(b,blen,deb_xfer);
  304. mutex_unlock(&d->usb_mutex);
  305. return ret;
  306. }
  307. static int az6027_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
  308. u16 index, u8 *b, int blen)
  309. {
  310. int ret;
  311. deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
  312. debug_dump(b,blen,deb_xfer);
  313. if(mutex_lock_interruptible(&d->usb_mutex)) {
  314. return -EAGAIN;
  315. }
  316. if ((ret = usb_control_msg(d->udev,
  317. usb_sndctrlpipe(d->udev,0),
  318. req,
  319. USB_TYPE_VENDOR | USB_DIR_OUT,
  320. value,index,b,blen,
  321. 2000)) != blen) {
  322. warn("usb out operation failed. (%d)",ret);
  323. mutex_unlock(&d->usb_mutex);
  324. return -EIO;
  325. } else{
  326. mutex_unlock(&d->usb_mutex);
  327. return 0;
  328. }
  329. }
  330. static int az6027_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  331. {
  332. int ret;
  333. u8 req;
  334. u16 value;
  335. u16 index;
  336. int blen;
  337. deb_info("%s %d",__func__,onoff);
  338. req = 0xBC;
  339. value = onoff;
  340. index = 0;
  341. blen =0;
  342. if((ret = az6027_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0) {
  343. warn("usb out operation failed. (%d)",ret);
  344. }
  345. //info("write CA
  346. return ret;
  347. }
  348. /* keys for the enclosed remote control */
  349. static struct dvb_usb_rc_key az6027_rc_keys[] = {
  350. { 0x00, 0x01, KEY_1 },
  351. { 0x00, 0x02, KEY_2 },
  352. };
  353. /* remote control stuff (does not work with my box) */
  354. static int az6027_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  355. {
  356. return 0;
  357. }
  358. /*
  359. int az6027_power_ctrl(struct dvb_usb_device *d, int onoff)
  360. {
  361. u8 v = onoff;
  362. return az6027_usb_out_op(d,0xBC,v,3,NULL,1);
  363. }
  364. */
  365. static int az6027_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
  366. int slot,
  367. int address)
  368. {
  369. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  370. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  371. int ret;
  372. u8 req;
  373. u16 value;
  374. u16 index;
  375. int blen;
  376. u8 b[12];
  377. //info ("%s %d", __func__, slot);
  378. if (0 != slot)
  379. return -EINVAL;
  380. mutex_lock (&state->ca_mutex);
  381. req = 0xC1;
  382. value = address;
  383. index = 0;
  384. blen = 1;
  385. ret = az6027_usb_in_op(d,req,value,index,b,blen);
  386. if (ret < 0) {
  387. warn("usb in operation failed. (%d)", ret);
  388. ret = -EINVAL;
  389. } else{
  390. ret = b[0];
  391. //info("CI MEM 0x%x = %x",address,b[0]);
  392. }
  393. //mdelay(10);
  394. mutex_unlock (&state->ca_mutex);
  395. return ret;
  396. }
  397. static int az6027_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
  398. int slot,
  399. int address,
  400. u8 value)
  401. {
  402. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  403. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  404. int ret;
  405. u8 req;
  406. u16 value1;
  407. u16 index;
  408. int blen;
  409. deb_info ("%s %d", __func__, slot);
  410. if (0 != slot)
  411. return -EINVAL;
  412. mutex_lock (&state->ca_mutex);
  413. req = 0xC2;
  414. value1 = address;
  415. index = value;
  416. blen =0;
  417. if((ret = az6027_usb_out_op(d,req,value1,index,NULL,blen)) != 0) {
  418. warn("usb out operation failed. (%d)",ret);
  419. }
  420. //mdelay(10);
  421. mutex_unlock (&state->ca_mutex);
  422. return ret;
  423. }
  424. static int az6027_ci_read_cam_control(struct dvb_ca_en50221 *ca,
  425. int slot,
  426. u8 address)
  427. {
  428. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  429. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  430. int ret;
  431. u8 req;
  432. u16 value;
  433. u16 index;
  434. int blen;
  435. u8 b[12];
  436. //info ("%s %d", __func__, slot);
  437. if (0 != slot)
  438. return -EINVAL;
  439. mutex_lock (&state->ca_mutex);
  440. req = 0xC3;
  441. value = address;
  442. index = 0;
  443. blen = 2;
  444. ret = az6027_usb_in_op(d,req,value,index,b,blen);
  445. if (ret < 0) {
  446. warn("usb in operation failed. (%d)", ret);
  447. ret = -EINVAL;
  448. } else{
  449. if(b[0] == 0)
  450. warn("Read CI IO error");
  451. ret = b[1];
  452. deb_info("read cam data = %x from 0x%x",b[1],value);
  453. }
  454. //udelay(350);
  455. mutex_unlock (&state->ca_mutex);
  456. return ret;
  457. }
  458. static int az6027_ci_write_cam_control(struct dvb_ca_en50221 *ca,
  459. int slot,
  460. u8 address,
  461. u8 value)
  462. {
  463. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  464. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  465. int ret;
  466. u8 req;
  467. u16 value1;
  468. u16 index;
  469. int blen;
  470. //info ("%s %d", __func__, slot);
  471. if (0 != slot)
  472. return -EINVAL;
  473. mutex_lock (&state->ca_mutex);
  474. req = 0xC4;
  475. value1 = address;
  476. index = value;
  477. blen =0;
  478. if((ret = az6027_usb_out_op(d,req,value1,index,NULL,blen)) != 0) {
  479. warn("usb out operation failed. (%d)",ret);
  480. goto failed;
  481. }
  482. //info("write CAM data(%x) to 0x%x ",index, value1);
  483. //udelay(350);
  484. failed:
  485. mutex_unlock (&state->ca_mutex);
  486. return ret;
  487. }
  488. static int CI_CamReady(struct dvb_ca_en50221 *ca,
  489. int slot)
  490. {
  491. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  492. int ret;
  493. u8 req;
  494. u16 value;
  495. u16 index;
  496. int blen;
  497. u8 b[12];
  498. req = 0xC8;
  499. value = 0;
  500. index = 0;
  501. blen = 1;
  502. ret = az6027_usb_in_op(d,req,value,index,b,blen);
  503. if (ret < 0) {
  504. warn("usb in operation failed. (%d)", ret);
  505. ret = -EIO;
  506. } else{
  507. ret = b[0];
  508. }
  509. return ret;
  510. }
  511. static int az6027_ci_slot_reset(struct dvb_ca_en50221 *ca,
  512. int slot)
  513. {
  514. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  515. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  516. int ret,i;
  517. u8 req;
  518. u16 value;
  519. u16 index;
  520. int blen;
  521. mutex_lock (&state->ca_mutex);
  522. req = 0xC6;
  523. value = 1;
  524. index = 0;
  525. blen =0;
  526. if((ret = az6027_usb_out_op(d,req,value,index,NULL,blen)) != 0) {
  527. warn("usb out operation failed. (%d)",ret);
  528. goto failed;
  529. }
  530. msleep (500);
  531. req = 0xC6;
  532. value = 0;
  533. index = 0;
  534. blen =0;
  535. if((ret = az6027_usb_out_op(d,req,value,index,NULL,blen)) != 0) {
  536. warn("usb out operation failed. (%d)",ret);
  537. goto failed;
  538. }
  539. for(i = 0;i < 15;i++)
  540. {
  541. msleep(100);
  542. if(CI_CamReady(ca,slot))
  543. {
  544. deb_info("CAM Ready");
  545. break;
  546. }
  547. }
  548. msleep(5000);
  549. failed:
  550. mutex_unlock (&state->ca_mutex);
  551. return ret;
  552. }
  553. static int az6027_ci_slot_shutdown(struct dvb_ca_en50221 *ca,
  554. int slot)
  555. {
  556. // do nothing here.
  557. return 0;
  558. }
  559. static int az6027_ci_slot_ts_enable(struct dvb_ca_en50221 *ca,
  560. int slot)
  561. {
  562. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  563. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  564. int ret;
  565. u8 req;
  566. u16 value;
  567. u16 index;
  568. int blen;
  569. deb_info("%s", __func__);
  570. mutex_lock (&state->ca_mutex);
  571. req = 0xC7;
  572. value = 1;
  573. index = 0;
  574. blen =0;
  575. if((ret = az6027_usb_out_op(d,req,value,index,NULL,blen)) != 0)
  576. {
  577. warn("usb out operation failed. (%d)",ret);
  578. goto failed;
  579. }
  580. failed:
  581. mutex_unlock (&state->ca_mutex);
  582. return ret;
  583. }
  584. static int az6027_ci_poll_slot_status(struct dvb_ca_en50221 *ca,
  585. int slot,
  586. int open)
  587. {
  588. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  589. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  590. int ret;
  591. u8 req;
  592. u16 value;
  593. u16 index;
  594. int blen;
  595. u8 b[12];
  596. mutex_lock (&state->ca_mutex);
  597. req = 0xC5;
  598. value = 0;
  599. index = 0;
  600. blen = 1;
  601. ret = az6027_usb_in_op(d,req,value,index,b,blen);
  602. if (ret < 0) {
  603. warn("usb in operation failed. (%d)", ret);
  604. ret = -EIO;
  605. } else
  606. ret = 0;
  607. if(b[0] == 0)
  608. ret = 0;
  609. else if(b[0] == 1){
  610. ret = (DVB_CA_EN50221_POLL_CAM_PRESENT |
  611. DVB_CA_EN50221_POLL_CAM_READY);
  612. //info("CAM IN slot = %d", slot);
  613. }
  614. mutex_unlock (&state->ca_mutex);
  615. return ret;
  616. }
  617. static void az6027_ci_uninit(struct dvb_usb_device *d)
  618. {
  619. struct az6027_device_state *state;
  620. deb_info("%s", __func__);
  621. if (NULL == d)
  622. return;
  623. state = (struct az6027_device_state *)d->priv;
  624. if (NULL == state)
  625. return;
  626. if (NULL == state->ca.data)
  627. return;
  628. dvb_ca_en50221_release(&state->ca);
  629. memset(&state->ca, 0, sizeof(state->ca));
  630. }
  631. static int az6027_ci_init(struct dvb_usb_adapter *a)
  632. {
  633. struct dvb_usb_device *d = a->dev;
  634. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  635. int ret;
  636. deb_info ("%s", __func__);
  637. mutex_init(&state->ca_mutex);
  638. state->ca.owner = THIS_MODULE;
  639. state->ca.read_attribute_mem = az6027_ci_read_attribute_mem;
  640. state->ca.write_attribute_mem = az6027_ci_write_attribute_mem;
  641. state->ca.read_cam_control = az6027_ci_read_cam_control;
  642. state->ca.write_cam_control = az6027_ci_write_cam_control;
  643. state->ca.slot_reset = az6027_ci_slot_reset;
  644. state->ca.slot_shutdown = az6027_ci_slot_shutdown;
  645. state->ca.slot_ts_enable = az6027_ci_slot_ts_enable;
  646. state->ca.poll_slot_status = az6027_ci_poll_slot_status;
  647. state->ca.data = d;
  648. ret = dvb_ca_en50221_init (&a->dvb_adap,
  649. &state->ca,
  650. /* flags */ 0,
  651. /* n_slots */ 1);
  652. if (0 != ret) {
  653. err ("Cannot initialize CI: Error %d.", ret);
  654. memset (&state->ca, 0, sizeof (state->ca));
  655. return ret;
  656. }
  657. deb_info ("CI initialized.");
  658. return 0;
  659. }
  660. static int az6027_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
  661. {
  662. az6027_usb_in_op(d, 0xb7, 6, 0, &mac[0], 6);
  663. return 0;
  664. }
  665. static int az6027_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
  666. {
  667. struct dvb_usb_adapter *adap = fe->dvb->priv;
  668. u8 buf;
  669. struct i2c_msg i2c_msg = {
  670. .addr = 0x99,
  671. .flags = 0,
  672. .buf = &buf,
  673. .len = 1
  674. };
  675. int ret;
  676. switch (voltage) {
  677. case SEC_VOLTAGE_13:
  678. buf = 1;// 2 --18v 1 --13v 0 --off
  679. ret = i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
  680. break;
  681. case SEC_VOLTAGE_18:
  682. buf = 2;// 2 --18v 1 --13v 0 --off
  683. ret = i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
  684. break;
  685. case SEC_VOLTAGE_OFF:
  686. buf = 0;// 2 --18v 1 --13v 0 --off
  687. ret = i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
  688. break;
  689. default:
  690. return -EINVAL;
  691. }
  692. return 0;
  693. }
  694. static int az6027_frontend_poweron(struct dvb_usb_adapter *adap)
  695. {
  696. int ret;
  697. u8 req;
  698. u16 value;
  699. u16 index;
  700. int blen;
  701. req = 0xBC;
  702. value = 1;//power on
  703. index = 3;
  704. blen =0;
  705. if((ret = az6027_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0)
  706. return -EIO;
  707. return 0;
  708. }
  709. static int az6027_frontend_reset(struct dvb_usb_adapter *adap)
  710. {
  711. int ret;
  712. u8 req;
  713. u16 value;
  714. u16 index;
  715. int blen;
  716. //reset demodulator
  717. req = 0xC0;
  718. value = 1;//high
  719. index = 3;
  720. blen =0;
  721. if((ret = az6027_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0)
  722. return -EIO;
  723. req = 0xC0;
  724. value = 0;//low
  725. index = 3;
  726. blen =0;
  727. msleep_interruptible(200);
  728. if((ret = az6027_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0)
  729. return -EIO;
  730. msleep_interruptible(200);
  731. req = 0xC0;
  732. value = 1;//high
  733. index = 3;
  734. blen =0;
  735. if((ret = az6027_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0)
  736. return -EIO;
  737. msleep_interruptible(200);
  738. return 0;
  739. }
  740. static int az6027_frontend_tsbypass(struct dvb_usb_adapter *adap,int onoff)
  741. {
  742. int ret;
  743. u8 req;
  744. u16 value;
  745. u16 index;
  746. int blen;
  747. //TS through
  748. req = 0xC7;
  749. value = onoff;
  750. index = 0;
  751. blen =0;
  752. if((ret = az6027_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0)
  753. return -EIO;
  754. return 0;
  755. }
  756. static int az6027_frontend_attach(struct dvb_usb_adapter *adap)
  757. {
  758. az6027_frontend_poweron(adap);
  759. az6027_frontend_reset(adap);
  760. deb_info("adap = %p,dev = %p\n",adap, adap->dev);
  761. adap->fe = stb0899_attach(&az6027_stb0899_config, &adap->dev->i2c_adap);
  762. if (adap->fe) {
  763. deb_info("found STB0899 DVB-S/DVB-S2 frontend @0x%02x",az6027_stb0899_config.demod_address);
  764. if (stb6100_attach(adap->fe, &az6027_stb6100_config, &adap->dev->i2c_adap)) {
  765. deb_info("found STB6100 DVB-S/DVB-S2 frontend @0x%02x",az6027_stb6100_config.tuner_address);
  766. adap->fe->ops.set_voltage = az6027_set_voltage;
  767. az6027_ci_init(adap);
  768. } else {
  769. adap->fe = NULL;
  770. }
  771. }
  772. else
  773. warn("no front-end attached\n");
  774. az6027_frontend_tsbypass(adap,0);
  775. return 0;
  776. }
  777. static struct dvb_usb_device_properties az6027_properties;
  778. static void
  779. az6027_usb_disconnect(struct usb_interface *intf)
  780. {
  781. struct dvb_usb_device *d = usb_get_intfdata (intf);
  782. az6027_ci_uninit (d);
  783. dvb_usb_device_exit (intf);
  784. }
  785. static int az6027_usb_probe(struct usb_interface *intf,
  786. const struct usb_device_id *id)
  787. {
  788. return dvb_usb_device_init(intf, &az6027_properties,
  789. THIS_MODULE, NULL, adapter_nr);
  790. }
  791. /* I2C */
  792. static int az6027_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
  793. {
  794. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  795. int i=0,j=0,len=0;
  796. int ret;
  797. u16 index;
  798. u16 value;
  799. int length;
  800. u8 req;
  801. u8 data[256];
  802. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  803. return -EAGAIN;
  804. //err("i2c func called\n");
  805. if (num > 2)
  806. warn("more than 2 i2c messages at a time is not handled yet. TODO.");
  807. for (i = 0; i < num; i++) {
  808. if(msg[i].addr == 0x99) {
  809. req = 0xBE;
  810. index = 0;
  811. value = msg[i].buf[0] & 0x00ff;
  812. length = 1;
  813. az6027_usb_out_op(d,req,value,index,data,length);
  814. }
  815. if(msg[i].addr == 0xd0){
  816. /* write/read request */
  817. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  818. /*
  819. err("i2c read ");
  820. err("addr0 = 0x%x",msg[i].addr);
  821. err("buf0 = 0x%02x 0x%02x",msg[i].buf[0],msg[i].buf[1]);
  822. err("len0 = %d",msg[i].len);
  823. err("addr1 = 0x%x",msg[i+1].addr);
  824. err("buf1 = 0x%02x 0x%02x",msg[i+1].buf[0], *msg[i+1].buf);
  825. err("len1 = %d",msg[i+1].len);
  826. */
  827. req = 0xB9;
  828. index = (((msg[i].buf[0] << 8) & 0xff00) | (msg[i].buf[1] & 0x00ff));
  829. value = msg[i].addr + (msg[i].len << 8);
  830. length = msg[i+1].len + 6;
  831. //err("in: req. %02x, val: %04x, ind: %04x\n",req,value,index);
  832. ret = az6027_usb_in_op(d,req,value,index,data,length);
  833. //err("ret = %d data = %02x %02x",ret,data[5],data[6]);
  834. len = msg[i+1].len;
  835. for (j=0; j<len; j++)
  836. {
  837. msg[i+1].buf[j] = data[j+5];
  838. }
  839. i++;
  840. //err("11111");
  841. }
  842. else
  843. {
  844. //err("i2c write ");
  845. //err("addr = 0x%x\n",msg[i].addr);
  846. //err("buf = 0x%02x 0x%02x\n",msg[i].buf[0],msg[i].buf[1]);
  847. //err("len = %d",msg[i].len);
  848. if(msg[i].addr == 0xd0)//demod 16bit addr
  849. {
  850. req = 0xBD;
  851. index = (((msg[i].buf[0] << 8) & 0xff00) | (msg[i].buf[1] & 0x00ff));//(msg[i].buf[0]<< 8) + msg[i].buf[1];
  852. value = msg[i].addr + (2 << 8);
  853. length = msg[i].len - 2;
  854. len = msg[i].len - 2;
  855. for(j=0;j<len;j++)
  856. {
  857. data[j] = msg[i].buf[j+2];
  858. }
  859. }
  860. //err("out: req. %02x, val: %04x, ind: %04x\n",req,value,index);
  861. az6027_usb_out_op(d,req,value,index,data,length);
  862. //err("22222");
  863. }
  864. }
  865. if(msg[i].addr == 0xc0){
  866. if (msg[i].flags & I2C_M_RD) {
  867. //err("i2c read ");
  868. //err("addr0 = 0x%x",msg[i].addr);
  869. //err("len0 = %d",msg[i].len);
  870. req = 0xB9;
  871. index = 0x0;
  872. value = msg[i].addr ;//+ (1 << 8);
  873. length = msg[i].len + 6;
  874. //err("in: req. %02x, val: %04x, ind: %04x\n",req,value,index);
  875. ret = az6027_usb_in_op(d,req,value,index,data,length);
  876. //err("ret = %d data = %02x %02x",ret,data[5],data[6]);
  877. len = msg[i].len;
  878. for (j=0; j<len; j++)
  879. {
  880. msg[i].buf[j] = data[j+5];
  881. }
  882. }
  883. else{
  884. //err("i2c write ");
  885. //err("addr0 = 0x%x",msg[i].addr);
  886. //err("buf0 = 0x%x",msg[i].buf[0]);
  887. //err("len0 = %d",msg[i].len);
  888. req = 0xBD;
  889. index = msg[i].buf[0] & 0x00FF;
  890. value = msg[i].addr + (1 << 8);
  891. length = msg[i].len - 1;
  892. len = msg[i].len - 1;
  893. for(j=0;j<len;j++)
  894. {
  895. data[j] = msg[i].buf[j+1];
  896. }
  897. //err("out: req. %02x, val: %04x, ind: %04x\n",req,value,index);
  898. az6027_usb_out_op(d,req,value,index,data,length);
  899. }
  900. }
  901. }
  902. mutex_unlock(&d->i2c_mutex);
  903. return i;
  904. }
  905. static u32 az6027_i2c_func(struct i2c_adapter *adapter)
  906. {
  907. return I2C_FUNC_I2C;
  908. }
  909. static struct i2c_algorithm az6027_i2c_algo = {
  910. .master_xfer = az6027_i2c_xfer,
  911. .functionality = az6027_i2c_func,
  912. };
  913. int az6027_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
  914. struct dvb_usb_device_description **desc, int *cold)
  915. {
  916. u8 b[16];
  917. s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev,0),
  918. 0xb7, USB_TYPE_VENDOR | USB_DIR_IN, 6, 0, b, 6, USB_CTRL_GET_TIMEOUT);
  919. //info("FW GET_VERSION length: %d\n",ret);
  920. *cold = ret <= 0;
  921. deb_info("cold: %d\n", *cold);
  922. return 0;
  923. }
  924. static struct usb_device_id az6027_usb_table [] = {
  925. { USB_DEVICE(USB_VID_TERRATEC, USB_PID_AZUREWAVE_AZ6027) },
  926. // { USB_DEVICE(0x0fd9, 0x002a) },
  927. { 0 },
  928. };
  929. MODULE_DEVICE_TABLE(usb, az6027_usb_table);
  930. static struct dvb_usb_device_properties az6027_properties = {
  931. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  932. .usb_ctrl = CYPRESS_FX2,
  933. //.download_firmware = az6027_download_firmware,
  934. .firmware = "dvb-usb-az6027-03.fw",
  935. .no_reconnect = 1,
  936. .size_of_priv = sizeof(struct az6027_device_state),
  937. .identify_state = az6027_identify_state,
  938. .num_adapters = 1,
  939. .adapter = {
  940. {
  941. //.caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS,
  942. .streaming_ctrl = az6027_streaming_ctrl,
  943. .frontend_attach = az6027_frontend_attach,
  944. /* parameter for the MPEG2-data transfer */
  945. .stream = {
  946. .type = USB_BULK,
  947. .count = 10,
  948. .endpoint = 0x02,
  949. .u = {
  950. .bulk = {
  951. .buffersize = 4096,
  952. }
  953. }
  954. },
  955. }
  956. },
  957. //.power_ctrl = az6027_power_ctrl,
  958. //.read_mac_address = az6027_read_mac_addr,
  959. .rc_key_map = az6027_rc_keys,
  960. .rc_key_map_size = ARRAY_SIZE(az6027_rc_keys),
  961. .rc_interval = 400,
  962. .rc_query = az6027_rc_query,
  963. .i2c_algo = &az6027_i2c_algo,
  964. .num_device_descs = 1,
  965. .devices = {
  966. { .name = "AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)",
  967. .cold_ids = { &az6027_usb_table[0], NULL },
  968. .warm_ids = { NULL },
  969. },
  970. { NULL },
  971. }
  972. };
  973. /* usb specific object needed to register this driver with the usb subsystem */
  974. static struct usb_driver az6027_usb_driver = {
  975. .name = "dvb_usb_az6027",
  976. .probe = az6027_usb_probe,
  977. //.disconnect = dvb_usb_device_exit,
  978. .disconnect = az6027_usb_disconnect,
  979. .id_table = az6027_usb_table,
  980. };
  981. /* module stuff */
  982. static int __init az6027_usb_module_init(void)
  983. {
  984. int result;
  985. if ((result = usb_register(&az6027_usb_driver))) {
  986. err("usb_register failed. (%d)",result);
  987. return result;
  988. }
  989. return 0;
  990. }
  991. static void __exit az6027_usb_module_exit(void)
  992. {
  993. /* deregister this driver from the USB subsystem */
  994. usb_deregister(&az6027_usb_driver);
  995. }
  996. module_init(az6027_usb_module_init);
  997. module_exit(az6027_usb_module_exit);
  998. MODULE_AUTHOR("Adams Xu <Adams.xu@azwave.com.cn>");
  999. MODULE_DESCRIPTION("Driver for AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)");
  1000. MODULE_VERSION("1.0");
  1001. MODULE_LICENSE("GPL");