az6027.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. static 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,
  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. static struct stb6100_config az6027_stb6100_config = {
  277. .tuner_address = 0xc0,
  278. .refclock = 27000000,
  279. };
  280. /* check for mutex FIXME */
  281. static int az6027_usb_in_op(struct dvb_usb_device *d, u8 req,
  282. u16 value, u16 index, u8 *b, int blen)
  283. {
  284. int ret = -1;
  285. if (mutex_lock_interruptible(&d->usb_mutex))
  286. return -EAGAIN;
  287. ret = usb_control_msg(d->udev,
  288. usb_rcvctrlpipe(d->udev, 0),
  289. req,
  290. USB_TYPE_VENDOR | USB_DIR_IN,
  291. value,
  292. index,
  293. b,
  294. blen,
  295. 2000);
  296. if (ret < 0) {
  297. warn("usb in operation failed. (%d)", ret);
  298. ret = -EIO;
  299. } else
  300. ret = 0;
  301. deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value, index);
  302. debug_dump(b, blen, deb_xfer);
  303. mutex_unlock(&d->usb_mutex);
  304. return ret;
  305. }
  306. static int az6027_usb_out_op(struct dvb_usb_device *d,
  307. u8 req,
  308. u16 value,
  309. u16 index,
  310. u8 *b,
  311. int blen)
  312. {
  313. int ret;
  314. deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value, index);
  315. debug_dump(b, blen, deb_xfer);
  316. if (mutex_lock_interruptible(&d->usb_mutex))
  317. return -EAGAIN;
  318. ret = usb_control_msg(d->udev,
  319. usb_sndctrlpipe(d->udev, 0),
  320. req,
  321. USB_TYPE_VENDOR | USB_DIR_OUT,
  322. value,
  323. index,
  324. b,
  325. blen,
  326. 2000);
  327. if (ret != blen) {
  328. warn("usb out operation failed. (%d)", ret);
  329. mutex_unlock(&d->usb_mutex);
  330. return -EIO;
  331. } else{
  332. mutex_unlock(&d->usb_mutex);
  333. return 0;
  334. }
  335. }
  336. static int az6027_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  337. {
  338. int ret;
  339. u8 req;
  340. u16 value;
  341. u16 index;
  342. int blen;
  343. deb_info("%s %d", __func__, onoff);
  344. req = 0xBC;
  345. value = onoff;
  346. index = 0;
  347. blen = 0;
  348. ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
  349. if (ret != 0)
  350. warn("usb out operation failed. (%d)", ret);
  351. return ret;
  352. }
  353. /* keys for the enclosed remote control */
  354. static struct rc_map_table rc_map_az6027_table[] = {
  355. { 0x01, KEY_1 },
  356. { 0x02, KEY_2 },
  357. };
  358. /* remote control stuff (does not work with my box) */
  359. static int az6027_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  360. {
  361. return 0;
  362. }
  363. /*
  364. int az6027_power_ctrl(struct dvb_usb_device *d, int onoff)
  365. {
  366. u8 v = onoff;
  367. return az6027_usb_out_op(d,0xBC,v,3,NULL,1);
  368. }
  369. */
  370. static int az6027_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
  371. int slot,
  372. int address)
  373. {
  374. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  375. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  376. int ret;
  377. u8 req;
  378. u16 value;
  379. u16 index;
  380. int blen;
  381. u8 *b;
  382. if (slot != 0)
  383. return -EINVAL;
  384. b = kmalloc(12, GFP_KERNEL);
  385. if (!b)
  386. return -ENOMEM;
  387. mutex_lock(&state->ca_mutex);
  388. req = 0xC1;
  389. value = address;
  390. index = 0;
  391. blen = 1;
  392. ret = az6027_usb_in_op(d, req, value, index, b, blen);
  393. if (ret < 0) {
  394. warn("usb in operation failed. (%d)", ret);
  395. ret = -EINVAL;
  396. } else {
  397. ret = b[0];
  398. }
  399. mutex_unlock(&state->ca_mutex);
  400. kfree(b);
  401. return ret;
  402. }
  403. static int az6027_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
  404. int slot,
  405. int address,
  406. u8 value)
  407. {
  408. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  409. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  410. int ret;
  411. u8 req;
  412. u16 value1;
  413. u16 index;
  414. int blen;
  415. deb_info("%s %d", __func__, slot);
  416. if (slot != 0)
  417. return -EINVAL;
  418. mutex_lock(&state->ca_mutex);
  419. req = 0xC2;
  420. value1 = address;
  421. index = value;
  422. blen = 0;
  423. ret = az6027_usb_out_op(d, req, value1, index, NULL, blen);
  424. if (ret != 0)
  425. warn("usb out operation failed. (%d)", ret);
  426. mutex_unlock(&state->ca_mutex);
  427. return ret;
  428. }
  429. static int az6027_ci_read_cam_control(struct dvb_ca_en50221 *ca,
  430. int slot,
  431. u8 address)
  432. {
  433. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  434. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  435. int ret;
  436. u8 req;
  437. u16 value;
  438. u16 index;
  439. int blen;
  440. u8 *b;
  441. if (slot != 0)
  442. return -EINVAL;
  443. b = kmalloc(12, GFP_KERNEL);
  444. if (!b)
  445. return -ENOMEM;
  446. mutex_lock(&state->ca_mutex);
  447. req = 0xC3;
  448. value = address;
  449. index = 0;
  450. blen = 2;
  451. ret = az6027_usb_in_op(d, req, value, index, b, blen);
  452. if (ret < 0) {
  453. warn("usb in operation failed. (%d)", ret);
  454. ret = -EINVAL;
  455. } else {
  456. if (b[0] == 0)
  457. warn("Read CI IO error");
  458. ret = b[1];
  459. deb_info("read cam data = %x from 0x%x", b[1], value);
  460. }
  461. mutex_unlock(&state->ca_mutex);
  462. kfree(b);
  463. return ret;
  464. }
  465. static int az6027_ci_write_cam_control(struct dvb_ca_en50221 *ca,
  466. int slot,
  467. u8 address,
  468. u8 value)
  469. {
  470. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  471. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  472. int ret;
  473. u8 req;
  474. u16 value1;
  475. u16 index;
  476. int blen;
  477. if (slot != 0)
  478. return -EINVAL;
  479. mutex_lock(&state->ca_mutex);
  480. req = 0xC4;
  481. value1 = address;
  482. index = value;
  483. blen = 0;
  484. ret = az6027_usb_out_op(d, req, value1, index, NULL, blen);
  485. if (ret != 0) {
  486. warn("usb out operation failed. (%d)", ret);
  487. goto failed;
  488. }
  489. failed:
  490. mutex_unlock(&state->ca_mutex);
  491. return ret;
  492. }
  493. static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot)
  494. {
  495. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  496. int ret;
  497. u8 req;
  498. u16 value;
  499. u16 index;
  500. int blen;
  501. u8 *b;
  502. b = kmalloc(12, GFP_KERNEL);
  503. if (!b)
  504. return -ENOMEM;
  505. req = 0xC8;
  506. value = 0;
  507. index = 0;
  508. blen = 1;
  509. ret = az6027_usb_in_op(d, req, value, index, b, blen);
  510. if (ret < 0) {
  511. warn("usb in operation failed. (%d)", ret);
  512. ret = -EIO;
  513. } else{
  514. ret = b[0];
  515. }
  516. kfree(b);
  517. return ret;
  518. }
  519. static int az6027_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
  520. {
  521. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  522. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  523. int ret, i;
  524. u8 req;
  525. u16 value;
  526. u16 index;
  527. int blen;
  528. mutex_lock(&state->ca_mutex);
  529. req = 0xC6;
  530. value = 1;
  531. index = 0;
  532. blen = 0;
  533. ret = az6027_usb_out_op(d, req, value, index, NULL, blen);
  534. if (ret != 0) {
  535. warn("usb out operation failed. (%d)", ret);
  536. goto failed;
  537. }
  538. msleep(500);
  539. req = 0xC6;
  540. value = 0;
  541. index = 0;
  542. blen = 0;
  543. ret = az6027_usb_out_op(d, req, value, index, NULL, blen);
  544. if (ret != 0) {
  545. warn("usb out operation failed. (%d)", ret);
  546. goto failed;
  547. }
  548. for (i = 0; i < 15; i++) {
  549. msleep(100);
  550. if (CI_CamReady(ca, slot)) {
  551. deb_info("CAM Ready");
  552. break;
  553. }
  554. }
  555. msleep(5000);
  556. failed:
  557. mutex_unlock(&state->ca_mutex);
  558. return ret;
  559. }
  560. static int az6027_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
  561. {
  562. return 0;
  563. }
  564. static int az6027_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
  565. {
  566. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  567. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  568. int ret;
  569. u8 req;
  570. u16 value;
  571. u16 index;
  572. int blen;
  573. deb_info("%s", __func__);
  574. mutex_lock(&state->ca_mutex);
  575. req = 0xC7;
  576. value = 1;
  577. index = 0;
  578. blen = 0;
  579. ret = az6027_usb_out_op(d, req, value, index, NULL, blen);
  580. if (ret != 0) {
  581. warn("usb out operation failed. (%d)", ret);
  582. goto failed;
  583. }
  584. failed:
  585. mutex_unlock(&state->ca_mutex);
  586. return ret;
  587. }
  588. static int az6027_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
  589. {
  590. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  591. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  592. int ret;
  593. u8 req;
  594. u16 value;
  595. u16 index;
  596. int blen;
  597. u8 *b;
  598. b = kmalloc(12, GFP_KERNEL);
  599. if (!b)
  600. return -ENOMEM;
  601. mutex_lock(&state->ca_mutex);
  602. req = 0xC5;
  603. value = 0;
  604. index = 0;
  605. blen = 1;
  606. ret = az6027_usb_in_op(d, req, value, index, b, blen);
  607. if (ret < 0) {
  608. warn("usb in operation failed. (%d)", ret);
  609. ret = -EIO;
  610. } else
  611. ret = 0;
  612. if (!ret && b[0] == 1) {
  613. ret = DVB_CA_EN50221_POLL_CAM_PRESENT |
  614. DVB_CA_EN50221_POLL_CAM_READY;
  615. }
  616. mutex_unlock(&state->ca_mutex);
  617. kfree(b);
  618. return ret;
  619. }
  620. static void az6027_ci_uninit(struct dvb_usb_device *d)
  621. {
  622. struct az6027_device_state *state;
  623. deb_info("%s", __func__);
  624. if (NULL == d)
  625. return;
  626. state = (struct az6027_device_state *)d->priv;
  627. if (NULL == state)
  628. return;
  629. if (NULL == state->ca.data)
  630. return;
  631. dvb_ca_en50221_release(&state->ca);
  632. memset(&state->ca, 0, sizeof(state->ca));
  633. }
  634. static int az6027_ci_init(struct dvb_usb_adapter *a)
  635. {
  636. struct dvb_usb_device *d = a->dev;
  637. struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
  638. int ret;
  639. deb_info("%s", __func__);
  640. mutex_init(&state->ca_mutex);
  641. state->ca.owner = THIS_MODULE;
  642. state->ca.read_attribute_mem = az6027_ci_read_attribute_mem;
  643. state->ca.write_attribute_mem = az6027_ci_write_attribute_mem;
  644. state->ca.read_cam_control = az6027_ci_read_cam_control;
  645. state->ca.write_cam_control = az6027_ci_write_cam_control;
  646. state->ca.slot_reset = az6027_ci_slot_reset;
  647. state->ca.slot_shutdown = az6027_ci_slot_shutdown;
  648. state->ca.slot_ts_enable = az6027_ci_slot_ts_enable;
  649. state->ca.poll_slot_status = az6027_ci_poll_slot_status;
  650. state->ca.data = d;
  651. ret = dvb_ca_en50221_init(&a->dvb_adap,
  652. &state->ca,
  653. 0, /* flags */
  654. 1);/* n_slots */
  655. if (ret != 0) {
  656. err("Cannot initialize CI: Error %d.", ret);
  657. memset(&state->ca, 0, sizeof(state->ca));
  658. return ret;
  659. }
  660. deb_info("CI initialized.");
  661. return 0;
  662. }
  663. /*
  664. static int az6027_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
  665. {
  666. az6027_usb_in_op(d, 0xb7, 6, 0, &mac[0], 6);
  667. return 0;
  668. }
  669. */
  670. static int az6027_set_voltage(struct dvb_frontend *fe,
  671. enum fe_sec_voltage voltage)
  672. {
  673. u8 buf;
  674. struct dvb_usb_adapter *adap = fe->dvb->priv;
  675. struct i2c_msg i2c_msg = {
  676. .addr = 0x99,
  677. .flags = 0,
  678. .buf = &buf,
  679. .len = 1
  680. };
  681. /*
  682. * 2 --18v
  683. * 1 --13v
  684. * 0 --off
  685. */
  686. switch (voltage) {
  687. case SEC_VOLTAGE_13:
  688. buf = 1;
  689. i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
  690. break;
  691. case SEC_VOLTAGE_18:
  692. buf = 2;
  693. i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
  694. break;
  695. case SEC_VOLTAGE_OFF:
  696. buf = 0;
  697. i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
  698. break;
  699. default:
  700. return -EINVAL;
  701. }
  702. return 0;
  703. }
  704. static int az6027_frontend_poweron(struct dvb_usb_adapter *adap)
  705. {
  706. int ret;
  707. u8 req;
  708. u16 value;
  709. u16 index;
  710. int blen;
  711. req = 0xBC;
  712. value = 1; /* power on */
  713. index = 3;
  714. blen = 0;
  715. ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
  716. if (ret != 0)
  717. return -EIO;
  718. return 0;
  719. }
  720. static int az6027_frontend_reset(struct dvb_usb_adapter *adap)
  721. {
  722. int ret;
  723. u8 req;
  724. u16 value;
  725. u16 index;
  726. int blen;
  727. /* reset demodulator */
  728. req = 0xC0;
  729. value = 1; /* high */
  730. index = 3;
  731. blen = 0;
  732. ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
  733. if (ret != 0)
  734. return -EIO;
  735. req = 0xC0;
  736. value = 0; /* low */
  737. index = 3;
  738. blen = 0;
  739. msleep_interruptible(200);
  740. ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
  741. if (ret != 0)
  742. return -EIO;
  743. msleep_interruptible(200);
  744. req = 0xC0;
  745. value = 1; /*high */
  746. index = 3;
  747. blen = 0;
  748. ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
  749. if (ret != 0)
  750. return -EIO;
  751. msleep_interruptible(200);
  752. return 0;
  753. }
  754. static int az6027_frontend_tsbypass(struct dvb_usb_adapter *adap, int onoff)
  755. {
  756. int ret;
  757. u8 req;
  758. u16 value;
  759. u16 index;
  760. int blen;
  761. /* TS passthrough */
  762. req = 0xC7;
  763. value = onoff;
  764. index = 0;
  765. blen = 0;
  766. ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
  767. if (ret != 0)
  768. return -EIO;
  769. return 0;
  770. }
  771. static int az6027_frontend_attach(struct dvb_usb_adapter *adap)
  772. {
  773. az6027_frontend_poweron(adap);
  774. az6027_frontend_reset(adap);
  775. deb_info("adap = %p, dev = %p\n", adap, adap->dev);
  776. adap->fe_adap[0].fe = stb0899_attach(&az6027_stb0899_config, &adap->dev->i2c_adap);
  777. if (adap->fe_adap[0].fe) {
  778. deb_info("found STB0899 DVB-S/DVB-S2 frontend @0x%02x", az6027_stb0899_config.demod_address);
  779. if (stb6100_attach(adap->fe_adap[0].fe, &az6027_stb6100_config, &adap->dev->i2c_adap)) {
  780. deb_info("found STB6100 DVB-S/DVB-S2 frontend @0x%02x", az6027_stb6100_config.tuner_address);
  781. adap->fe_adap[0].fe->ops.set_voltage = az6027_set_voltage;
  782. az6027_ci_init(adap);
  783. } else {
  784. adap->fe_adap[0].fe = NULL;
  785. }
  786. } else
  787. warn("no front-end attached\n");
  788. az6027_frontend_tsbypass(adap, 0);
  789. return 0;
  790. }
  791. static struct dvb_usb_device_properties az6027_properties;
  792. static void az6027_usb_disconnect(struct usb_interface *intf)
  793. {
  794. struct dvb_usb_device *d = usb_get_intfdata(intf);
  795. az6027_ci_uninit(d);
  796. dvb_usb_device_exit(intf);
  797. }
  798. static int az6027_usb_probe(struct usb_interface *intf,
  799. const struct usb_device_id *id)
  800. {
  801. return dvb_usb_device_init(intf,
  802. &az6027_properties,
  803. THIS_MODULE,
  804. NULL,
  805. adapter_nr);
  806. }
  807. /* I2C */
  808. static int az6027_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
  809. {
  810. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  811. int i = 0, j = 0, len = 0;
  812. u16 index;
  813. u16 value;
  814. int length;
  815. u8 req;
  816. u8 *data;
  817. data = kmalloc(256, GFP_KERNEL);
  818. if (!data)
  819. return -ENOMEM;
  820. if (mutex_lock_interruptible(&d->i2c_mutex) < 0) {
  821. kfree(data);
  822. return -EAGAIN;
  823. }
  824. if (num > 2)
  825. warn("more than 2 i2c messages at a time is not handled yet. TODO.");
  826. for (i = 0; i < num; i++) {
  827. if (msg[i].addr == 0x99) {
  828. req = 0xBE;
  829. index = 0;
  830. value = msg[i].buf[0] & 0x00ff;
  831. length = 1;
  832. az6027_usb_out_op(d, req, value, index, data, length);
  833. }
  834. if (msg[i].addr == 0xd0) {
  835. /* write/read request */
  836. if (i + 1 < num && (msg[i + 1].flags & I2C_M_RD)) {
  837. req = 0xB9;
  838. index = (((msg[i].buf[0] << 8) & 0xff00) | (msg[i].buf[1] & 0x00ff));
  839. value = msg[i].addr + (msg[i].len << 8);
  840. length = msg[i + 1].len + 6;
  841. az6027_usb_in_op(d, req, value, index, data, length);
  842. len = msg[i + 1].len;
  843. for (j = 0; j < len; j++)
  844. msg[i + 1].buf[j] = data[j + 5];
  845. i++;
  846. } else {
  847. /* demod 16bit addr */
  848. req = 0xBD;
  849. index = (((msg[i].buf[0] << 8) & 0xff00) | (msg[i].buf[1] & 0x00ff));
  850. value = msg[i].addr + (2 << 8);
  851. length = msg[i].len - 2;
  852. len = msg[i].len - 2;
  853. for (j = 0; j < len; j++)
  854. data[j] = msg[i].buf[j + 2];
  855. az6027_usb_out_op(d, req, value, index, data, length);
  856. }
  857. }
  858. if (msg[i].addr == 0xc0) {
  859. if (msg[i].flags & I2C_M_RD) {
  860. req = 0xB9;
  861. index = 0x0;
  862. value = msg[i].addr;
  863. length = msg[i].len + 6;
  864. az6027_usb_in_op(d, req, value, index, data, length);
  865. len = msg[i].len;
  866. for (j = 0; j < len; j++)
  867. msg[i].buf[j] = data[j + 5];
  868. } else {
  869. req = 0xBD;
  870. index = msg[i].buf[0] & 0x00FF;
  871. value = msg[i].addr + (1 << 8);
  872. length = msg[i].len - 1;
  873. len = msg[i].len - 1;
  874. for (j = 0; j < len; j++)
  875. data[j] = msg[i].buf[j + 1];
  876. az6027_usb_out_op(d, req, value, index, data, length);
  877. }
  878. }
  879. }
  880. mutex_unlock(&d->i2c_mutex);
  881. kfree(data);
  882. return i;
  883. }
  884. static u32 az6027_i2c_func(struct i2c_adapter *adapter)
  885. {
  886. return I2C_FUNC_I2C;
  887. }
  888. static struct i2c_algorithm az6027_i2c_algo = {
  889. .master_xfer = az6027_i2c_xfer,
  890. .functionality = az6027_i2c_func,
  891. };
  892. static int az6027_identify_state(struct usb_device *udev,
  893. struct dvb_usb_device_properties *props,
  894. struct dvb_usb_device_description **desc,
  895. int *cold)
  896. {
  897. u8 *b;
  898. s16 ret;
  899. b = kmalloc(16, GFP_KERNEL);
  900. if (!b)
  901. return -ENOMEM;
  902. ret = usb_control_msg(udev,
  903. usb_rcvctrlpipe(udev, 0),
  904. 0xb7,
  905. USB_TYPE_VENDOR | USB_DIR_IN,
  906. 6,
  907. 0,
  908. b,
  909. 6,
  910. USB_CTRL_GET_TIMEOUT);
  911. *cold = ret <= 0;
  912. kfree(b);
  913. deb_info("cold: %d\n", *cold);
  914. return 0;
  915. }
  916. static struct usb_device_id az6027_usb_table[] = {
  917. { USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_AZ6027) },
  918. { USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_DVBS2CI_V1) },
  919. { USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_DVBS2CI_V2) },
  920. { USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_HDCI_V1) },
  921. { USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_HDCI_V2) },
  922. { USB_DEVICE(USB_VID_ELGATO, USB_PID_ELGATO_EYETV_SAT) },
  923. { USB_DEVICE(USB_VID_ELGATO, USB_PID_ELGATO_EYETV_SAT_V2) },
  924. { },
  925. };
  926. MODULE_DEVICE_TABLE(usb, az6027_usb_table);
  927. static struct dvb_usb_device_properties az6027_properties = {
  928. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  929. .usb_ctrl = CYPRESS_FX2,
  930. .firmware = "dvb-usb-az6027-03.fw",
  931. .no_reconnect = 1,
  932. .size_of_priv = sizeof(struct az6027_device_state),
  933. .identify_state = az6027_identify_state,
  934. .num_adapters = 1,
  935. .adapter = {
  936. {
  937. .num_frontends = 1,
  938. .fe = {{
  939. .streaming_ctrl = az6027_streaming_ctrl,
  940. .frontend_attach = az6027_frontend_attach,
  941. /* parameter for the MPEG2-data transfer */
  942. .stream = {
  943. .type = USB_BULK,
  944. .count = 10,
  945. .endpoint = 0x02,
  946. .u = {
  947. .bulk = {
  948. .buffersize = 4096,
  949. }
  950. }
  951. },
  952. }},
  953. }
  954. },
  955. /*
  956. .power_ctrl = az6027_power_ctrl,
  957. .read_mac_address = az6027_read_mac_addr,
  958. */
  959. .rc.legacy = {
  960. .rc_map_table = rc_map_az6027_table,
  961. .rc_map_size = ARRAY_SIZE(rc_map_az6027_table),
  962. .rc_interval = 400,
  963. .rc_query = az6027_rc_query,
  964. },
  965. .i2c_algo = &az6027_i2c_algo,
  966. .num_device_descs = 7,
  967. .devices = {
  968. {
  969. .name = "AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)",
  970. .cold_ids = { &az6027_usb_table[0], NULL },
  971. .warm_ids = { NULL },
  972. }, {
  973. .name = "TERRATEC S7",
  974. .cold_ids = { &az6027_usb_table[1], NULL },
  975. .warm_ids = { NULL },
  976. }, {
  977. .name = "TERRATEC S7 MKII",
  978. .cold_ids = { &az6027_usb_table[2], NULL },
  979. .warm_ids = { NULL },
  980. }, {
  981. .name = "Technisat SkyStar USB 2 HD CI",
  982. .cold_ids = { &az6027_usb_table[3], NULL },
  983. .warm_ids = { NULL },
  984. }, {
  985. .name = "Technisat SkyStar USB 2 HD CI",
  986. .cold_ids = { &az6027_usb_table[4], NULL },
  987. .warm_ids = { NULL },
  988. }, {
  989. .name = "Elgato EyeTV Sat",
  990. .cold_ids = { &az6027_usb_table[5], NULL },
  991. .warm_ids = { NULL },
  992. }, {
  993. .name = "Elgato EyeTV Sat",
  994. .cold_ids = { &az6027_usb_table[6], NULL },
  995. .warm_ids = { NULL },
  996. },
  997. { NULL },
  998. }
  999. };
  1000. /* usb specific object needed to register this driver with the usb subsystem */
  1001. static struct usb_driver az6027_usb_driver = {
  1002. .name = "dvb_usb_az6027",
  1003. .probe = az6027_usb_probe,
  1004. .disconnect = az6027_usb_disconnect,
  1005. .id_table = az6027_usb_table,
  1006. };
  1007. module_usb_driver(az6027_usb_driver);
  1008. MODULE_AUTHOR("Adams Xu <Adams.xu@azwave.com.cn>");
  1009. MODULE_DESCRIPTION("Driver for AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)");
  1010. MODULE_VERSION("1.0");
  1011. MODULE_LICENSE("GPL");