az6027.c 26 KB

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