blob: 62c58288469f5936af6937067f2994e54dd0d793 [file] [log] [blame]
Adams Xu76f9a822010-02-14 14:34:07 -03001/* 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"
Manu Abraham5f0e1cf2010-02-14 16:58:10 -030013
Adams Xu76f9a822010-02-14 14:34:07 -030014#include "stb0899_drv.h"
15#include "stb0899_reg.h"
16#include "stb0899_cfg.h"
Manu Abraham5f0e1cf2010-02-14 16:58:10 -030017
Adams Xu76f9a822010-02-14 14:34:07 -030018#include "stb6100.h"
19#include "stb6100_cfg.h"
20#include "dvb_ca_en50221.h"
21
Adams Xu76f9a822010-02-14 14:34:07 -030022int dvb_usb_az6027_debug;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -030023module_param_named(debug, dvb_usb_az6027_debug, int, 0644);
Adams Xu76f9a822010-02-14 14:34:07 -030024MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
25
26DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
27
28struct az6027_device_state {
29 struct dvb_ca_en50221 ca;
30 struct mutex ca_mutex;
31 u8 power_state;
32};
33
34static const struct stb0899_s1_reg az6027_stb0899_s1_init_1[] = {
35
Manu Abraham5f0e1cf2010-02-14 16:58:10 -030036 /* 0x0000000b, SYSREG */
Adams Xu76f9a822010-02-14 14:34:07 -030037 { STB0899_DEV_ID , 0x30 },
38 { STB0899_DISCNTRL1 , 0x32 },
39 { STB0899_DISCNTRL2 , 0x80 },
40 { STB0899_DISRX_ST0 , 0x04 },
41 { STB0899_DISRX_ST1 , 0x00 },
42 { STB0899_DISPARITY , 0x00 },
43 { STB0899_DISFIFO , 0x00 },
44 { STB0899_DISSTATUS , 0x20 },
45 { STB0899_DISF22 , 0x99 },
46 { STB0899_DISF22RX , 0xa8 },
Manu Abraham5f0e1cf2010-02-14 16:58:10 -030047 /* SYSREG ? */
Adams Xu76f9a822010-02-14 14:34:07 -030048 { STB0899_ACRPRESC , 0x11 },
49 { STB0899_ACRDIV1 , 0x0a },
50 { STB0899_ACRDIV2 , 0x05 },
51 { STB0899_DACR1 , 0x00 },
52 { STB0899_DACR2 , 0x00 },
53 { STB0899_OUTCFG , 0x00 },
54 { STB0899_MODECFG , 0x00 },
55 { STB0899_IRQSTATUS_3 , 0xfe },
56 { STB0899_IRQSTATUS_2 , 0x03 },
57 { STB0899_IRQSTATUS_1 , 0x7c },
58 { STB0899_IRQSTATUS_0 , 0xf4 },
59 { STB0899_IRQMSK_3 , 0xf3 },
60 { STB0899_IRQMSK_2 , 0xfc },
61 { STB0899_IRQMSK_1 , 0xff },
62 { STB0899_IRQMSK_0 , 0xff },
63 { STB0899_IRQCFG , 0x00 },
64 { STB0899_I2CCFG , 0x88 },
65 { STB0899_I2CRPT , 0x58 },
66 { STB0899_IOPVALUE5 , 0x00 },
67 { STB0899_IOPVALUE4 , 0x33 },
68 { STB0899_IOPVALUE3 , 0x6d },
69 { STB0899_IOPVALUE2 , 0x90 },
70 { STB0899_IOPVALUE1 , 0x60 },
71 { STB0899_IOPVALUE0 , 0x00 },
72 { STB0899_GPIO00CFG , 0x82 },
73 { STB0899_GPIO01CFG , 0x82 },
74 { STB0899_GPIO02CFG , 0x82 },
75 { STB0899_GPIO03CFG , 0x82 },
76 { STB0899_GPIO04CFG , 0x82 },
77 { STB0899_GPIO05CFG , 0x82 },
78 { STB0899_GPIO06CFG , 0x82 },
79 { STB0899_GPIO07CFG , 0x82 },
80 { STB0899_GPIO08CFG , 0x82 },
81 { STB0899_GPIO09CFG , 0x82 },
82 { STB0899_GPIO10CFG , 0x82 },
83 { STB0899_GPIO11CFG , 0x82 },
84 { STB0899_GPIO12CFG , 0x82 },
85 { STB0899_GPIO13CFG , 0x82 },
86 { STB0899_GPIO14CFG , 0x82 },
87 { STB0899_GPIO15CFG , 0x82 },
88 { STB0899_GPIO16CFG , 0x82 },
89 { STB0899_GPIO17CFG , 0x82 },
90 { STB0899_GPIO18CFG , 0x82 },
91 { STB0899_GPIO19CFG , 0x82 },
92 { STB0899_GPIO20CFG , 0x82 },
93 { STB0899_SDATCFG , 0xb8 },
94 { STB0899_SCLTCFG , 0xba },
95 { STB0899_AGCRFCFG , 0x1c }, /* 0x11 */
96 { STB0899_GPIO22 , 0x82 }, /* AGCBB2CFG */
97 { STB0899_GPIO21 , 0x91 }, /* AGCBB1CFG */
98 { STB0899_DIRCLKCFG , 0x82 },
99 { STB0899_CLKOUT27CFG , 0x7e },
100 { STB0899_STDBYCFG , 0x82 },
101 { STB0899_CS0CFG , 0x82 },
102 { STB0899_CS1CFG , 0x82 },
103 { STB0899_DISEQCOCFG , 0x20 },
104 { STB0899_GPIO32CFG , 0x82 },
105 { STB0899_GPIO33CFG , 0x82 },
106 { STB0899_GPIO34CFG , 0x82 },
107 { STB0899_GPIO35CFG , 0x82 },
108 { STB0899_GPIO36CFG , 0x82 },
109 { STB0899_GPIO37CFG , 0x82 },
110 { STB0899_GPIO38CFG , 0x82 },
111 { STB0899_GPIO39CFG , 0x82 },
112 { STB0899_NCOARSE , 0x17 }, /* 0x15 = 27 Mhz Clock, F/3 = 198MHz, F/6 = 99MHz */
113 { STB0899_SYNTCTRL , 0x02 }, /* 0x00 = CLK from CLKI, 0x02 = CLK from XTALI */
114 { STB0899_FILTCTRL , 0x00 },
115 { STB0899_SYSCTRL , 0x01 },
116 { STB0899_STOPCLK1 , 0x20 },
117 { STB0899_STOPCLK2 , 0x00 },
118 { STB0899_INTBUFSTATUS , 0x00 },
119 { STB0899_INTBUFCTRL , 0x0a },
120 { 0xffff , 0xff },
121};
122
123static const struct stb0899_s1_reg az6027_stb0899_s1_init_3[] = {
124 { STB0899_DEMOD , 0x00 },
125 { STB0899_RCOMPC , 0xc9 },
126 { STB0899_AGC1CN , 0x01 },
127 { STB0899_AGC1REF , 0x10 },
Mauro Carvalho Chehab000f64e2010-03-11 01:58:12 -0300128 { STB0899_RTC , 0x23 },
Adams Xu76f9a822010-02-14 14:34:07 -0300129 { STB0899_TMGCFG , 0x4e },
130 { STB0899_AGC2REF , 0x34 },
131 { STB0899_TLSR , 0x84 },
132 { STB0899_CFD , 0xf7 },
Mauro Carvalho Chehab000f64e2010-03-11 01:58:12 -0300133 { STB0899_ACLC , 0x87 },
Adams Xu76f9a822010-02-14 14:34:07 -0300134 { STB0899_BCLC , 0x94 },
135 { STB0899_EQON , 0x41 },
136 { STB0899_LDT , 0xf1 },
137 { STB0899_LDT2 , 0xe3 },
138 { STB0899_EQUALREF , 0xb4 },
139 { STB0899_TMGRAMP , 0x10 },
140 { STB0899_TMGTHD , 0x30 },
141 { STB0899_IDCCOMP , 0xfd },
142 { STB0899_QDCCOMP , 0xff },
143 { STB0899_POWERI , 0x0c },
144 { STB0899_POWERQ , 0x0f },
145 { STB0899_RCOMP , 0x6c },
146 { STB0899_AGCIQIN , 0x80 },
147 { STB0899_AGC2I1 , 0x06 },
148 { STB0899_AGC2I2 , 0x00 },
149 { STB0899_TLIR , 0x30 },
150 { STB0899_RTF , 0x7f },
151 { STB0899_DSTATUS , 0x00 },
152 { STB0899_LDI , 0xbc },
153 { STB0899_CFRM , 0xea },
154 { STB0899_CFRL , 0x31 },
155 { STB0899_NIRM , 0x2b },
156 { STB0899_NIRL , 0x80 },
157 { STB0899_ISYMB , 0x1d },
158 { STB0899_QSYMB , 0xa6 },
159 { STB0899_SFRH , 0x2f },
160 { STB0899_SFRM , 0x68 },
161 { STB0899_SFRL , 0x40 },
162 { STB0899_SFRUPH , 0x2f },
163 { STB0899_SFRUPM , 0x68 },
164 { STB0899_SFRUPL , 0x40 },
165 { STB0899_EQUAI1 , 0x02 },
166 { STB0899_EQUAQ1 , 0xff },
167 { STB0899_EQUAI2 , 0x04 },
168 { STB0899_EQUAQ2 , 0x05 },
169 { STB0899_EQUAI3 , 0x02 },
170 { STB0899_EQUAQ3 , 0xfd },
171 { STB0899_EQUAI4 , 0x03 },
172 { STB0899_EQUAQ4 , 0x07 },
173 { STB0899_EQUAI5 , 0x08 },
174 { STB0899_EQUAQ5 , 0xf5 },
175 { STB0899_DSTATUS2 , 0x00 },
176 { STB0899_VSTATUS , 0x00 },
177 { STB0899_VERROR , 0x86 },
178 { STB0899_IQSWAP , 0x2a },
179 { STB0899_ECNT1M , 0x00 },
180 { STB0899_ECNT1L , 0x00 },
181 { STB0899_ECNT2M , 0x00 },
182 { STB0899_ECNT2L , 0x00 },
183 { STB0899_ECNT3M , 0x0a },
184 { STB0899_ECNT3L , 0xad },
185 { STB0899_FECAUTO1 , 0x06 },
Mauro Carvalho Chehab000f64e2010-03-11 01:58:12 -0300186 { STB0899_FECM , 0x01 },
Adams Xu76f9a822010-02-14 14:34:07 -0300187 { STB0899_VTH12 , 0xb0 },
188 { STB0899_VTH23 , 0x7a },
Mauro Carvalho Chehab000f64e2010-03-11 01:58:12 -0300189 { STB0899_VTH34 , 0x58 },
Adams Xu76f9a822010-02-14 14:34:07 -0300190 { STB0899_VTH56 , 0x38 },
191 { STB0899_VTH67 , 0x34 },
192 { STB0899_VTH78 , 0x24 },
193 { STB0899_PRVIT , 0xff },
194 { STB0899_VITSYNC , 0x19 },
195 { STB0899_RSULC , 0xb1 }, /* DVB = 0xb1, DSS = 0xa1 */
196 { STB0899_TSULC , 0x42 },
197 { STB0899_RSLLC , 0x41 },
Mauro Carvalho Chehab000f64e2010-03-11 01:58:12 -0300198 { STB0899_TSLPL , 0x12 },
Adams Xu76f9a822010-02-14 14:34:07 -0300199 { STB0899_TSCFGH , 0x0c },
200 { STB0899_TSCFGM , 0x00 },
201 { STB0899_TSCFGL , 0x00 },
202 { STB0899_TSOUT , 0x69 }, /* 0x0d for CAM */
203 { STB0899_RSSYNCDEL , 0x00 },
204 { STB0899_TSINHDELH , 0x02 },
205 { STB0899_TSINHDELM , 0x00 },
206 { STB0899_TSINHDELL , 0x00 },
207 { STB0899_TSLLSTKM , 0x1b },
208 { STB0899_TSLLSTKL , 0xb3 },
209 { STB0899_TSULSTKM , 0x00 },
210 { STB0899_TSULSTKL , 0x00 },
211 { STB0899_PCKLENUL , 0xbc },
212 { STB0899_PCKLENLL , 0xcc },
213 { STB0899_RSPCKLEN , 0xbd },
214 { STB0899_TSSTATUS , 0x90 },
215 { STB0899_ERRCTRL1 , 0xb6 },
216 { STB0899_ERRCTRL2 , 0x95 },
217 { STB0899_ERRCTRL3 , 0x8d },
218 { STB0899_DMONMSK1 , 0x27 },
219 { STB0899_DMONMSK0 , 0x03 },
220 { STB0899_DEMAPVIT , 0x5c },
221 { STB0899_PLPARM , 0x19 },
222 { STB0899_PDELCTRL , 0x48 },
223 { STB0899_PDELCTRL2 , 0x00 },
224 { STB0899_BBHCTRL1 , 0x00 },
225 { STB0899_BBHCTRL2 , 0x00 },
226 { STB0899_HYSTTHRESH , 0x77 },
227 { STB0899_MATCSTM , 0x00 },
228 { STB0899_MATCSTL , 0x00 },
229 { STB0899_UPLCSTM , 0x00 },
230 { STB0899_UPLCSTL , 0x00 },
231 { STB0899_DFLCSTM , 0x00 },
232 { STB0899_DFLCSTL , 0x00 },
233 { STB0899_SYNCCST , 0x00 },
234 { STB0899_SYNCDCSTM , 0x00 },
235 { STB0899_SYNCDCSTL , 0x00 },
236 { STB0899_ISI_ENTRY , 0x00 },
237 { STB0899_ISI_BIT_EN , 0x00 },
238 { STB0899_MATSTRM , 0xf0 },
239 { STB0899_MATSTRL , 0x02 },
240 { STB0899_UPLSTRM , 0x45 },
241 { STB0899_UPLSTRL , 0x60 },
242 { STB0899_DFLSTRM , 0xe3 },
243 { STB0899_DFLSTRL , 0x00 },
244 { STB0899_SYNCSTR , 0x47 },
245 { STB0899_SYNCDSTRM , 0x05 },
246 { STB0899_SYNCDSTRL , 0x18 },
247 { STB0899_CFGPDELSTATUS1 , 0x19 },
248 { STB0899_CFGPDELSTATUS2 , 0x2b },
249 { STB0899_BBFERRORM , 0x00 },
250 { STB0899_BBFERRORL , 0x01 },
251 { STB0899_UPKTERRORM , 0x00 },
252 { STB0899_UPKTERRORL , 0x00 },
253 { 0xffff , 0xff },
254};
255
256
257
258struct stb0899_config az6027_stb0899_config = {
259 .init_dev = az6027_stb0899_s1_init_1,
260 .init_s2_demod = stb0899_s2_init_2,
261 .init_s1_demod = az6027_stb0899_s1_init_3,
262 .init_s2_fec = stb0899_s2_init_4,
263 .init_tst = stb0899_s1_init_5,
264
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300265 .demod_address = 0xd0, /* 0x68, 0xd0 >> 1 */
Adams Xu76f9a822010-02-14 14:34:07 -0300266
267 .xtal_freq = 27000000,
268 .inversion = IQ_SWAP_ON, /* 1 */
269
270 .lo_clk = 76500000,
271 .hi_clk = 99000000,
272
273 .esno_ave = STB0899_DVBS2_ESNO_AVE,
274 .esno_quant = STB0899_DVBS2_ESNO_QUANT,
275 .avframes_coarse = STB0899_DVBS2_AVFRAMES_COARSE,
276 .avframes_fine = STB0899_DVBS2_AVFRAMES_FINE,
277 .miss_threshold = STB0899_DVBS2_MISS_THRESHOLD,
278 .uwp_threshold_acq = STB0899_DVBS2_UWP_THRESHOLD_ACQ,
279 .uwp_threshold_track = STB0899_DVBS2_UWP_THRESHOLD_TRACK,
280 .uwp_threshold_sof = STB0899_DVBS2_UWP_THRESHOLD_SOF,
281 .sof_search_timeout = STB0899_DVBS2_SOF_SEARCH_TIMEOUT,
282
283 .btr_nco_bits = STB0899_DVBS2_BTR_NCO_BITS,
284 .btr_gain_shift_offset = STB0899_DVBS2_BTR_GAIN_SHIFT_OFFSET,
285 .crl_nco_bits = STB0899_DVBS2_CRL_NCO_BITS,
286 .ldpc_max_iter = STB0899_DVBS2_LDPC_MAX_ITER,
287
288 .tuner_get_frequency = stb6100_get_frequency,
289 .tuner_set_frequency = stb6100_set_frequency,
290 .tuner_set_bandwidth = stb6100_set_bandwidth,
291 .tuner_get_bandwidth = stb6100_get_bandwidth,
292 .tuner_set_rfsiggain = NULL,
293};
294
295struct stb6100_config az6027_stb6100_config = {
296 .tuner_address = 0xc0,
297 .refclock = 27000000,
298};
299
300
301/* check for mutex FIXME */
302int az6027_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
303{
304 int ret = -1;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300305 if (mutex_lock_interruptible(&d->usb_mutex))
Adams Xu76f9a822010-02-14 14:34:07 -0300306 return -EAGAIN;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300307
Adams Xu76f9a822010-02-14 14:34:07 -0300308 ret = usb_control_msg(d->udev,
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300309 usb_rcvctrlpipe(d->udev, 0),
310 req,
311 USB_TYPE_VENDOR | USB_DIR_IN,
312 value,
313 index,
314 b,
315 blen,
316 2000);
Adams Xu76f9a822010-02-14 14:34:07 -0300317
318 if (ret < 0) {
319 warn("usb in operation failed. (%d)", ret);
320 ret = -EIO;
321 } else
322 ret = 0;
323
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300324 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value, index);
325 debug_dump(b, blen, deb_xfer);
Adams Xu76f9a822010-02-14 14:34:07 -0300326
Adams Xu76f9a822010-02-14 14:34:07 -0300327 mutex_unlock(&d->usb_mutex);
328 return ret;
329}
330
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300331static int az6027_usb_out_op(struct dvb_usb_device *d,
332 u8 req,
333 u16 value,
334 u16 index,
335 u8 *b,
336 int blen)
Adams Xu76f9a822010-02-14 14:34:07 -0300337{
338 int ret;
Adams Xu76f9a822010-02-14 14:34:07 -0300339
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300340 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value, index);
341 debug_dump(b, blen, deb_xfer);
342
343 if (mutex_lock_interruptible(&d->usb_mutex))
344 return -EAGAIN;
345
346 ret = usb_control_msg(d->udev,
347 usb_sndctrlpipe(d->udev, 0),
348 req,
349 USB_TYPE_VENDOR | USB_DIR_OUT,
350 value,
351 index,
352 b,
353 blen,
354 2000);
355
356 if (ret != blen) {
357 warn("usb out operation failed. (%d)", ret);
Adams Xu76f9a822010-02-14 14:34:07 -0300358 mutex_unlock(&d->usb_mutex);
359 return -EIO;
360 } else{
361 mutex_unlock(&d->usb_mutex);
362 return 0;
363 }
364}
365
366static int az6027_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
367{
368 int ret;
369 u8 req;
370 u16 value;
371 u16 index;
372 int blen;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300373
374 deb_info("%s %d", __func__, onoff);
375
Adams Xu76f9a822010-02-14 14:34:07 -0300376 req = 0xBC;
377 value = onoff;
378 index = 0;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300379 blen = 0;
380
381 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
382 if (ret != 0)
383 warn("usb out operation failed. (%d)", ret);
384
Adams Xu76f9a822010-02-14 14:34:07 -0300385 return ret;
386}
387
388/* keys for the enclosed remote control */
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -0300389static struct ir_scancode ir_codes_az6027_table[] = {
Manu Abraham4105ed52010-02-14 17:07:28 -0300390 { 0x01, KEY_1 },
391 { 0x02, KEY_2 },
Adams Xu76f9a822010-02-14 14:34:07 -0300392};
393
394/* remote control stuff (does not work with my box) */
395static int az6027_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
396{
397 return 0;
398}
399
400/*
401int az6027_power_ctrl(struct dvb_usb_device *d, int onoff)
402{
403 u8 v = onoff;
404 return az6027_usb_out_op(d,0xBC,v,3,NULL,1);
405}
406*/
407
408static int az6027_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300409 int slot,
410 int address)
Adams Xu76f9a822010-02-14 14:34:07 -0300411{
412 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
413 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
414
415 int ret;
416 u8 req;
417 u16 value;
418 u16 index;
419 int blen;
Dan Carpenter32ec4572010-05-04 09:14:29 -0300420 u8 *b;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300421
422 if (slot != 0)
Adams Xu76f9a822010-02-14 14:34:07 -0300423 return -EINVAL;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300424
Dan Carpenter32ec4572010-05-04 09:14:29 -0300425 b = kmalloc(12, GFP_KERNEL);
426 if (!b)
427 return -ENOMEM;
428
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300429 mutex_lock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300430
431 req = 0xC1;
432 value = address;
433 index = 0;
434 blen = 1;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300435
436 ret = az6027_usb_in_op(d, req, value, index, b, blen);
Adams Xu76f9a822010-02-14 14:34:07 -0300437 if (ret < 0) {
438 warn("usb in operation failed. (%d)", ret);
439 ret = -EINVAL;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300440 } else {
Adams Xu76f9a822010-02-14 14:34:07 -0300441 ret = b[0];
Adams Xu76f9a822010-02-14 14:34:07 -0300442 }
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300443
444 mutex_unlock(&state->ca_mutex);
Dan Carpenter32ec4572010-05-04 09:14:29 -0300445 kfree(b);
Adams Xu76f9a822010-02-14 14:34:07 -0300446 return ret;
447}
448
449static int az6027_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300450 int slot,
451 int address,
452 u8 value)
Adams Xu76f9a822010-02-14 14:34:07 -0300453{
454 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
455 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300456
Adams Xu76f9a822010-02-14 14:34:07 -0300457 int ret;
458 u8 req;
459 u16 value1;
460 u16 index;
461 int blen;
462
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300463 deb_info("%s %d", __func__, slot);
464 if (slot != 0)
Adams Xu76f9a822010-02-14 14:34:07 -0300465 return -EINVAL;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300466
467 mutex_lock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300468 req = 0xC2;
469 value1 = address;
470 index = value;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300471 blen = 0;
Adams Xu76f9a822010-02-14 14:34:07 -0300472
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300473 ret = az6027_usb_out_op(d, req, value1, index, NULL, blen);
474 if (ret != 0)
475 warn("usb out operation failed. (%d)", ret);
476
477 mutex_unlock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300478 return ret;
479}
480
481static int az6027_ci_read_cam_control(struct dvb_ca_en50221 *ca,
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300482 int slot,
483 u8 address)
Adams Xu76f9a822010-02-14 14:34:07 -0300484{
485 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
486 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
487
488 int ret;
489 u8 req;
490 u16 value;
491 u16 index;
492 int blen;
Dan Carpenter32ec4572010-05-04 09:14:29 -0300493 u8 *b;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300494
495 if (slot != 0)
Adams Xu76f9a822010-02-14 14:34:07 -0300496 return -EINVAL;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300497
Dan Carpenter32ec4572010-05-04 09:14:29 -0300498 b = kmalloc(12, GFP_KERNEL);
499 if (!b)
500 return -ENOMEM;
501
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300502 mutex_lock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300503
504 req = 0xC3;
505 value = address;
506 index = 0;
507 blen = 2;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300508
509 ret = az6027_usb_in_op(d, req, value, index, b, blen);
Adams Xu76f9a822010-02-14 14:34:07 -0300510 if (ret < 0) {
511 warn("usb in operation failed. (%d)", ret);
512 ret = -EINVAL;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300513 } else {
514 if (b[0] == 0)
Adams Xu76f9a822010-02-14 14:34:07 -0300515 warn("Read CI IO error");
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300516
Adams Xu76f9a822010-02-14 14:34:07 -0300517 ret = b[1];
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300518 deb_info("read cam data = %x from 0x%x", b[1], value);
Adams Xu76f9a822010-02-14 14:34:07 -0300519 }
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300520
521 mutex_unlock(&state->ca_mutex);
Dan Carpenter32ec4572010-05-04 09:14:29 -0300522 kfree(b);
Adams Xu76f9a822010-02-14 14:34:07 -0300523 return ret;
524}
525
526static int az6027_ci_write_cam_control(struct dvb_ca_en50221 *ca,
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300527 int slot,
528 u8 address,
529 u8 value)
Adams Xu76f9a822010-02-14 14:34:07 -0300530{
531 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
532 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300533
Adams Xu76f9a822010-02-14 14:34:07 -0300534 int ret;
535 u8 req;
536 u16 value1;
537 u16 index;
538 int blen;
539
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300540 if (slot != 0)
Adams Xu76f9a822010-02-14 14:34:07 -0300541 return -EINVAL;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300542
543 mutex_lock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300544 req = 0xC4;
545 value1 = address;
546 index = value;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300547 blen = 0;
548
549 ret = az6027_usb_out_op(d, req, value1, index, NULL, blen);
550 if (ret != 0) {
551 warn("usb out operation failed. (%d)", ret);
Adams Xu76f9a822010-02-14 14:34:07 -0300552 goto failed;
553 }
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300554
555failed:
556 mutex_unlock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300557 return ret;
558}
559
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300560static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot)
Adams Xu76f9a822010-02-14 14:34:07 -0300561{
562 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300563
Adams Xu76f9a822010-02-14 14:34:07 -0300564 int ret;
565 u8 req;
566 u16 value;
567 u16 index;
568 int blen;
Dan Carpenter32ec4572010-05-04 09:14:29 -0300569 u8 *b;
570
571 b = kmalloc(12, GFP_KERNEL);
572 if (!b)
573 return -ENOMEM;
Adams Xu76f9a822010-02-14 14:34:07 -0300574
575 req = 0xC8;
576 value = 0;
577 index = 0;
578 blen = 1;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300579
580 ret = az6027_usb_in_op(d, req, value, index, b, blen);
Adams Xu76f9a822010-02-14 14:34:07 -0300581 if (ret < 0) {
582 warn("usb in operation failed. (%d)", ret);
583 ret = -EIO;
584 } else{
585 ret = b[0];
586 }
Dan Carpenter32ec4572010-05-04 09:14:29 -0300587 kfree(b);
Adams Xu76f9a822010-02-14 14:34:07 -0300588 return ret;
589}
590
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300591static int az6027_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
Adams Xu76f9a822010-02-14 14:34:07 -0300592{
593 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
594 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300595
596 int ret, i;
Adams Xu76f9a822010-02-14 14:34:07 -0300597 u8 req;
598 u16 value;
599 u16 index;
600 int blen;
601
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300602 mutex_lock(&state->ca_mutex);
603
Adams Xu76f9a822010-02-14 14:34:07 -0300604 req = 0xC6;
605 value = 1;
606 index = 0;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300607 blen = 0;
608
609 ret = az6027_usb_out_op(d, req, value, index, NULL, blen);
610 if (ret != 0) {
611 warn("usb out operation failed. (%d)", ret);
Adams Xu76f9a822010-02-14 14:34:07 -0300612 goto failed;
613 }
614
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300615 msleep(500);
616 req = 0xC6;
617 value = 0;
618 index = 0;
619 blen = 0;
620
621 ret = az6027_usb_out_op(d, req, value, index, NULL, blen);
622 if (ret != 0) {
623 warn("usb out operation failed. (%d)", ret);
624 goto failed;
625 }
626
627 for (i = 0; i < 15; i++) {
Adams Xu76f9a822010-02-14 14:34:07 -0300628 msleep(100);
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300629
630 if (CI_CamReady(ca, slot)) {
Adams Xu76f9a822010-02-14 14:34:07 -0300631 deb_info("CAM Ready");
632 break;
633 }
634 }
635 msleep(5000);
636
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300637failed:
638 mutex_unlock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300639 return ret;
640}
641
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300642static int az6027_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
Adams Xu76f9a822010-02-14 14:34:07 -0300643{
Adams Xu76f9a822010-02-14 14:34:07 -0300644 return 0;
645}
646
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300647static int az6027_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
Adams Xu76f9a822010-02-14 14:34:07 -0300648{
649 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
650 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300651
Adams Xu76f9a822010-02-14 14:34:07 -0300652 int ret;
653 u8 req;
654 u16 value;
655 u16 index;
656 int blen;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300657
Adams Xu76f9a822010-02-14 14:34:07 -0300658 deb_info("%s", __func__);
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300659 mutex_lock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300660 req = 0xC7;
661 value = 1;
662 index = 0;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300663 blen = 0;
664
665 ret = az6027_usb_out_op(d, req, value, index, NULL, blen);
666 if (ret != 0) {
667 warn("usb out operation failed. (%d)", ret);
Adams Xu76f9a822010-02-14 14:34:07 -0300668 goto failed;
669 }
Adams Xu76f9a822010-02-14 14:34:07 -0300670
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300671failed:
672 mutex_unlock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300673 return ret;
674}
675
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300676static int az6027_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
Adams Xu76f9a822010-02-14 14:34:07 -0300677{
678 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
679 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
680 int ret;
681 u8 req;
682 u16 value;
683 u16 index;
684 int blen;
Dan Carpenter32ec4572010-05-04 09:14:29 -0300685 u8 *b;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300686
Dan Carpenter32ec4572010-05-04 09:14:29 -0300687 b = kmalloc(12, GFP_KERNEL);
688 if (!b)
689 return -ENOMEM;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300690 mutex_lock(&state->ca_mutex);
Adams Xu76f9a822010-02-14 14:34:07 -0300691
692 req = 0xC5;
693 value = 0;
694 index = 0;
695 blen = 1;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300696
697 ret = az6027_usb_in_op(d, req, value, index, b, blen);
Adams Xu76f9a822010-02-14 14:34:07 -0300698 if (ret < 0) {
699 warn("usb in operation failed. (%d)", ret);
700 ret = -EIO;
701 } else
702 ret = 0;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300703
Dan Carpentera18e6592010-05-04 09:31:13 -0300704 if (!ret && b[0] == 1) {
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300705 ret = DVB_CA_EN50221_POLL_CAM_PRESENT |
706 DVB_CA_EN50221_POLL_CAM_READY;
Adams Xu76f9a822010-02-14 14:34:07 -0300707 }
708
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300709 mutex_unlock(&state->ca_mutex);
Dan Carpenter32ec4572010-05-04 09:14:29 -0300710 kfree(b);
Adams Xu76f9a822010-02-14 14:34:07 -0300711 return ret;
712}
713
714
715static void az6027_ci_uninit(struct dvb_usb_device *d)
716{
717 struct az6027_device_state *state;
718
719 deb_info("%s", __func__);
720
721 if (NULL == d)
722 return;
723
724 state = (struct az6027_device_state *)d->priv;
725 if (NULL == state)
726 return;
727
728 if (NULL == state->ca.data)
729 return;
730
731 dvb_ca_en50221_release(&state->ca);
732
733 memset(&state->ca, 0, sizeof(state->ca));
734}
735
736
737static int az6027_ci_init(struct dvb_usb_adapter *a)
738{
739 struct dvb_usb_device *d = a->dev;
740 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
741 int ret;
742
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300743 deb_info("%s", __func__);
Adams Xu76f9a822010-02-14 14:34:07 -0300744
745 mutex_init(&state->ca_mutex);
746
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300747 state->ca.owner = THIS_MODULE;
748 state->ca.read_attribute_mem = az6027_ci_read_attribute_mem;
749 state->ca.write_attribute_mem = az6027_ci_write_attribute_mem;
750 state->ca.read_cam_control = az6027_ci_read_cam_control;
751 state->ca.write_cam_control = az6027_ci_write_cam_control;
752 state->ca.slot_reset = az6027_ci_slot_reset;
753 state->ca.slot_shutdown = az6027_ci_slot_shutdown;
754 state->ca.slot_ts_enable = az6027_ci_slot_ts_enable;
755 state->ca.poll_slot_status = az6027_ci_poll_slot_status;
756 state->ca.data = d;
Adams Xu76f9a822010-02-14 14:34:07 -0300757
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300758 ret = dvb_ca_en50221_init(&a->dvb_adap,
759 &state->ca,
760 0, /* flags */
761 1);/* n_slots */
762 if (ret != 0) {
763 err("Cannot initialize CI: Error %d.", ret);
764 memset(&state->ca, 0, sizeof(state->ca));
Adams Xu76f9a822010-02-14 14:34:07 -0300765 return ret;
766 }
767
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300768 deb_info("CI initialized.");
Adams Xu76f9a822010-02-14 14:34:07 -0300769
770 return 0;
771}
772
Manu Abraham4105ed52010-02-14 17:07:28 -0300773/*
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300774static int az6027_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Adams Xu76f9a822010-02-14 14:34:07 -0300775{
776 az6027_usb_in_op(d, 0xb7, 6, 0, &mac[0], 6);
777 return 0;
778}
Manu Abraham4105ed52010-02-14 17:07:28 -0300779*/
Adams Xu76f9a822010-02-14 14:34:07 -0300780
781static int az6027_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
782{
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300783
Adams Xu76f9a822010-02-14 14:34:07 -0300784 u8 buf;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300785 int ret;
786 struct dvb_usb_adapter *adap = fe->dvb->priv;
787
Adams Xu76f9a822010-02-14 14:34:07 -0300788 struct i2c_msg i2c_msg = {
789 .addr = 0x99,
790 .flags = 0,
791 .buf = &buf,
792 .len = 1
793 };
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300794
795 /*
796 * 2 --18v
797 * 1 --13v
798 * 0 --off
799 */
Adams Xu76f9a822010-02-14 14:34:07 -0300800 switch (voltage) {
801 case SEC_VOLTAGE_13:
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300802 buf = 1;
Adams Xu76f9a822010-02-14 14:34:07 -0300803 ret = i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
804 break;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300805
Adams Xu76f9a822010-02-14 14:34:07 -0300806 case SEC_VOLTAGE_18:
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300807 buf = 2;
Adams Xu76f9a822010-02-14 14:34:07 -0300808 ret = i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
809 break;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300810
Adams Xu76f9a822010-02-14 14:34:07 -0300811 case SEC_VOLTAGE_OFF:
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300812 buf = 0;
Adams Xu76f9a822010-02-14 14:34:07 -0300813 ret = i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
814 break;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300815
Adams Xu76f9a822010-02-14 14:34:07 -0300816 default:
817 return -EINVAL;
818 }
819 return 0;
820}
821
822
823static int az6027_frontend_poweron(struct dvb_usb_adapter *adap)
824{
825 int ret;
826 u8 req;
827 u16 value;
828 u16 index;
829 int blen;
830
831 req = 0xBC;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300832 value = 1; /* power on */
Adams Xu76f9a822010-02-14 14:34:07 -0300833 index = 3;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300834 blen = 0;
Adams Xu76f9a822010-02-14 14:34:07 -0300835
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300836 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
837 if (ret != 0)
838 return -EIO;
839
Adams Xu76f9a822010-02-14 14:34:07 -0300840 return 0;
841}
842static int az6027_frontend_reset(struct dvb_usb_adapter *adap)
843{
844 int ret;
845 u8 req;
846 u16 value;
847 u16 index;
848 int blen;
849
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300850 /* reset demodulator */
Adams Xu76f9a822010-02-14 14:34:07 -0300851 req = 0xC0;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300852 value = 1; /* high */
Adams Xu76f9a822010-02-14 14:34:07 -0300853 index = 3;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300854 blen = 0;
Adams Xu76f9a822010-02-14 14:34:07 -0300855
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300856 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
857 if (ret != 0)
858 return -EIO;
Adams Xu76f9a822010-02-14 14:34:07 -0300859
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300860 req = 0xC0;
861 value = 0; /* low */
862 index = 3;
863 blen = 0;
864 msleep_interruptible(200);
865
866 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
867 if (ret != 0)
868 return -EIO;
869
870 msleep_interruptible(200);
871
872 req = 0xC0;
873 value = 1; /*high */
874 index = 3;
875 blen = 0;
876
877 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
878 if (ret != 0)
879 return -EIO;
880
Adams Xu76f9a822010-02-14 14:34:07 -0300881 msleep_interruptible(200);
882 return 0;
883}
884
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300885static int az6027_frontend_tsbypass(struct dvb_usb_adapter *adap, int onoff)
Adams Xu76f9a822010-02-14 14:34:07 -0300886{
887 int ret;
888 u8 req;
889 u16 value;
890 u16 index;
891 int blen;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300892
893 /* TS passthrough */
Adams Xu76f9a822010-02-14 14:34:07 -0300894 req = 0xC7;
895 value = onoff;
896 index = 0;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300897 blen = 0;
Adams Xu76f9a822010-02-14 14:34:07 -0300898
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300899 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
900 if (ret != 0)
901 return -EIO;
902
Adams Xu76f9a822010-02-14 14:34:07 -0300903 return 0;
904}
905
906static int az6027_frontend_attach(struct dvb_usb_adapter *adap)
907{
908
909 az6027_frontend_poweron(adap);
910 az6027_frontend_reset(adap);
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300911
912 deb_info("adap = %p, dev = %p\n", adap, adap->dev);
Adams Xu76f9a822010-02-14 14:34:07 -0300913 adap->fe = stb0899_attach(&az6027_stb0899_config, &adap->dev->i2c_adap);
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300914
Adams Xu76f9a822010-02-14 14:34:07 -0300915 if (adap->fe) {
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300916 deb_info("found STB0899 DVB-S/DVB-S2 frontend @0x%02x", az6027_stb0899_config.demod_address);
Adams Xu76f9a822010-02-14 14:34:07 -0300917 if (stb6100_attach(adap->fe, &az6027_stb6100_config, &adap->dev->i2c_adap)) {
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300918 deb_info("found STB6100 DVB-S/DVB-S2 frontend @0x%02x", az6027_stb6100_config.tuner_address);
Adams Xu76f9a822010-02-14 14:34:07 -0300919 adap->fe->ops.set_voltage = az6027_set_voltage;
920 az6027_ci_init(adap);
921 } else {
922 adap->fe = NULL;
923 }
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300924 } else
Adams Xu76f9a822010-02-14 14:34:07 -0300925 warn("no front-end attached\n");
926
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300927 az6027_frontend_tsbypass(adap, 0);
Adams Xu76f9a822010-02-14 14:34:07 -0300928
929 return 0;
930}
931
932static struct dvb_usb_device_properties az6027_properties;
933
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300934static void az6027_usb_disconnect(struct usb_interface *intf)
Adams Xu76f9a822010-02-14 14:34:07 -0300935{
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300936 struct dvb_usb_device *d = usb_get_intfdata(intf);
937 az6027_ci_uninit(d);
938 dvb_usb_device_exit(intf);
Adams Xu76f9a822010-02-14 14:34:07 -0300939}
940
941
942static int az6027_usb_probe(struct usb_interface *intf,
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300943 const struct usb_device_id *id)
Adams Xu76f9a822010-02-14 14:34:07 -0300944{
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300945 return dvb_usb_device_init(intf,
946 &az6027_properties,
947 THIS_MODULE,
948 NULL,
949 adapter_nr);
Adams Xu76f9a822010-02-14 14:34:07 -0300950}
951
952/* I2C */
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300953static int az6027_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
Adams Xu76f9a822010-02-14 14:34:07 -0300954{
955 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300956 int i = 0, j = 0, len = 0;
Adams Xu76f9a822010-02-14 14:34:07 -0300957 int ret;
958 u16 index;
959 u16 value;
960 int length;
961 u8 req;
Dan Carpenter32ec4572010-05-04 09:14:29 -0300962 u8 *data;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300963
Dan Carpenter32ec4572010-05-04 09:14:29 -0300964 data = kmalloc(256, GFP_KERNEL);
965 if (!data)
966 return -ENOMEM;
967
968 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) {
969 kfree(data);
Adams Xu76f9a822010-02-14 14:34:07 -0300970 return -EAGAIN;
Dan Carpenter32ec4572010-05-04 09:14:29 -0300971 }
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300972
Adams Xu76f9a822010-02-14 14:34:07 -0300973 if (num > 2)
974 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
975
976 for (i = 0; i < num; i++) {
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300977
978 if (msg[i].addr == 0x99) {
Adams Xu76f9a822010-02-14 14:34:07 -0300979 req = 0xBE;
980 index = 0;
981 value = msg[i].buf[0] & 0x00ff;
982 length = 1;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300983 az6027_usb_out_op(d, req, value, index, data, length);
Adams Xu76f9a822010-02-14 14:34:07 -0300984 }
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300985
986 if (msg[i].addr == 0xd0) {
Adams Xu76f9a822010-02-14 14:34:07 -0300987 /* write/read request */
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300988 if (i + 1 < num && (msg[i + 1].flags & I2C_M_RD)) {
Adams Xu76f9a822010-02-14 14:34:07 -0300989 req = 0xB9;
990 index = (((msg[i].buf[0] << 8) & 0xff00) | (msg[i].buf[1] & 0x00ff));
991 value = msg[i].addr + (msg[i].len << 8);
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300992 length = msg[i + 1].len + 6;
993 ret = az6027_usb_in_op(d, req, value, index, data, length);
994 len = msg[i + 1].len;
995 for (j = 0; j < len; j++)
996 msg[i + 1].buf[j] = data[j + 5];
997
Adams Xu76f9a822010-02-14 14:34:07 -0300998 i++;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -0300999 } else {
1000
Márton Németh61207922010-03-01 04:10:52 -03001001 /* demod 16bit addr */
1002 req = 0xBD;
1003 index = (((msg[i].buf[0] << 8) & 0xff00) | (msg[i].buf[1] & 0x00ff));
1004 value = msg[i].addr + (2 << 8);
1005 length = msg[i].len - 2;
1006 len = msg[i].len - 2;
1007 for (j = 0; j < len; j++)
1008 data[j] = msg[i].buf[j + 2];
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001009 az6027_usb_out_op(d, req, value, index, data, length);
Adams Xu76f9a822010-02-14 14:34:07 -03001010 }
1011 }
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001012
1013 if (msg[i].addr == 0xc0) {
Adams Xu76f9a822010-02-14 14:34:07 -03001014 if (msg[i].flags & I2C_M_RD) {
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001015
Adams Xu76f9a822010-02-14 14:34:07 -03001016 req = 0xB9;
1017 index = 0x0;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001018 value = msg[i].addr;
Adams Xu76f9a822010-02-14 14:34:07 -03001019 length = msg[i].len + 6;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001020 ret = az6027_usb_in_op(d, req, value, index, data, length);
Adams Xu76f9a822010-02-14 14:34:07 -03001021 len = msg[i].len;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001022 for (j = 0; j < len; j++)
1023 msg[i].buf[j] = data[j + 5];
1024
1025 } else {
1026
Adams Xu76f9a822010-02-14 14:34:07 -03001027 req = 0xBD;
1028 index = msg[i].buf[0] & 0x00FF;
1029 value = msg[i].addr + (1 << 8);
1030 length = msg[i].len - 1;
1031 len = msg[i].len - 1;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001032
1033 for (j = 0; j < len; j++)
1034 data[j] = msg[i].buf[j + 1];
1035
1036 az6027_usb_out_op(d, req, value, index, data, length);
Adams Xu76f9a822010-02-14 14:34:07 -03001037 }
1038 }
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001039 }
Adams Xu76f9a822010-02-14 14:34:07 -03001040 mutex_unlock(&d->i2c_mutex);
Dan Carpenter32ec4572010-05-04 09:14:29 -03001041 kfree(data);
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001042
Adams Xu76f9a822010-02-14 14:34:07 -03001043 return i;
1044}
1045
1046
1047static u32 az6027_i2c_func(struct i2c_adapter *adapter)
1048{
1049 return I2C_FUNC_I2C;
1050}
1051
1052static struct i2c_algorithm az6027_i2c_algo = {
1053 .master_xfer = az6027_i2c_xfer,
1054 .functionality = az6027_i2c_func,
1055};
1056
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001057int az6027_identify_state(struct usb_device *udev,
1058 struct dvb_usb_device_properties *props,
1059 struct dvb_usb_device_description **desc,
1060 int *cold)
Adams Xu76f9a822010-02-14 14:34:07 -03001061{
Dan Carpenter32ec4572010-05-04 09:14:29 -03001062 u8 *b;
1063 s16 ret;
1064
1065 b = kmalloc(16, GFP_KERNEL);
1066 if (!b)
1067 return -ENOMEM;
1068
1069 ret = usb_control_msg(udev,
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001070 usb_rcvctrlpipe(udev, 0),
1071 0xb7,
1072 USB_TYPE_VENDOR | USB_DIR_IN,
1073 6,
1074 0,
1075 b,
1076 6,
1077 USB_CTRL_GET_TIMEOUT);
Adams Xu76f9a822010-02-14 14:34:07 -03001078
1079 *cold = ret <= 0;
Dan Carpenter32ec4572010-05-04 09:14:29 -03001080 kfree(b);
Adams Xu76f9a822010-02-14 14:34:07 -03001081 deb_info("cold: %d\n", *cold);
1082 return 0;
1083}
1084
1085
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001086static struct usb_device_id az6027_usb_table[] = {
Manu Abraham3c3233e2010-02-14 15:10:54 -03001087 { USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_AZ6027) },
Oleg Roitburd20d2c562010-02-26 09:47:16 -03001088 { USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_DVBS2CI_V1) },
1089 { USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_DVBS2CI_V2) },
Oleg Roitburd823b63b2010-02-25 17:19:31 -03001090 { USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_HDCI_V1) },
1091 { USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_HDCI_V2) },
Manu Abraham3c3233e2010-02-14 15:10:54 -03001092 { },
Adams Xu76f9a822010-02-14 14:34:07 -03001093};
1094
1095MODULE_DEVICE_TABLE(usb, az6027_usb_table);
1096
1097static struct dvb_usb_device_properties az6027_properties = {
1098 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1099 .usb_ctrl = CYPRESS_FX2,
Adams Xu76f9a822010-02-14 14:34:07 -03001100 .firmware = "dvb-usb-az6027-03.fw",
1101 .no_reconnect = 1,
1102
1103 .size_of_priv = sizeof(struct az6027_device_state),
1104 .identify_state = az6027_identify_state,
1105 .num_adapters = 1,
1106 .adapter = {
1107 {
Adams Xu76f9a822010-02-14 14:34:07 -03001108 .streaming_ctrl = az6027_streaming_ctrl,
1109 .frontend_attach = az6027_frontend_attach,
1110
1111 /* parameter for the MPEG2-data transfer */
1112 .stream = {
1113 .type = USB_BULK,
1114 .count = 10,
1115 .endpoint = 0x02,
1116 .u = {
1117 .bulk = {
1118 .buffersize = 4096,
1119 }
1120 }
1121 },
1122 }
1123 },
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001124/*
1125 .power_ctrl = az6027_power_ctrl,
1126 .read_mac_address = az6027_read_mac_addr,
1127 */
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001128 .rc.legacy = {
1129 .rc_key_map = ir_codes_az6027_table,
1130 .rc_key_map_size = ARRAY_SIZE(ir_codes_az6027_table),
1131 .rc_interval = 400,
1132 .rc_query = az6027_rc_query,
1133 },
1134
Adams Xu76f9a822010-02-14 14:34:07 -03001135 .i2c_algo = &az6027_i2c_algo,
1136
Oleg Roitburd20d2c562010-02-26 09:47:16 -03001137 .num_device_descs = 5,
Adams Xu76f9a822010-02-14 14:34:07 -03001138 .devices = {
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001139 {
1140 .name = "AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)",
1141 .cold_ids = { &az6027_usb_table[0], NULL },
1142 .warm_ids = { NULL },
Oleg Roitburd20d2c562010-02-26 09:47:16 -03001143 }, {
1144 .name = "TERRATEC S7",
1145 .cold_ids = { &az6027_usb_table[1], NULL },
1146 .warm_ids = { NULL },
1147 }, {
1148 .name = "TERRATEC S7 MKII",
1149 .cold_ids = { &az6027_usb_table[2], NULL },
1150 .warm_ids = { NULL },
1151 }, {
1152 .name = "Technisat SkyStar USB 2 HD CI",
1153 .cold_ids = { &az6027_usb_table[3], NULL },
1154 .warm_ids = { NULL },
1155 }, {
1156 .name = "Technisat SkyStar USB 2 HD CI",
1157 .cold_ids = { &az6027_usb_table[4], NULL },
1158 .warm_ids = { NULL },
Adams Xu76f9a822010-02-14 14:34:07 -03001159 },
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001160 { NULL },
Adams Xu76f9a822010-02-14 14:34:07 -03001161 }
1162};
1163
1164/* usb specific object needed to register this driver with the usb subsystem */
1165static struct usb_driver az6027_usb_driver = {
1166 .name = "dvb_usb_az6027",
1167 .probe = az6027_usb_probe,
Adams Xu76f9a822010-02-14 14:34:07 -03001168 .disconnect = az6027_usb_disconnect,
1169 .id_table = az6027_usb_table,
1170};
1171
1172/* module stuff */
1173static int __init az6027_usb_module_init(void)
1174{
1175 int result;
Manu Abraham5f0e1cf2010-02-14 16:58:10 -03001176
1177 result = usb_register(&az6027_usb_driver);
1178 if (result) {
1179 err("usb_register failed. (%d)", result);
Adams Xu76f9a822010-02-14 14:34:07 -03001180 return result;
1181 }
1182
1183 return 0;
1184}
1185
1186static void __exit az6027_usb_module_exit(void)
1187{
1188 /* deregister this driver from the USB subsystem */
1189 usb_deregister(&az6027_usb_driver);
1190}
1191
1192module_init(az6027_usb_module_init);
1193module_exit(az6027_usb_module_exit);
1194
1195MODULE_AUTHOR("Adams Xu <Adams.xu@azwave.com.cn>");
1196MODULE_DESCRIPTION("Driver for AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)");
1197MODULE_VERSION("1.0");
1198MODULE_LICENSE("GPL");