az6027.c 27 KB

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