az6027.c 27 KB

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