Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / dvb / dvb-usb / az6027.c
blobd7290b2c091397005ad81496c06041e730db41d9
1 /* DVB USB compliant Linux driver for the AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)
2 * receiver.
4 * Copyright (C) 2009 Adams.Xu <adams.xu@azwave.com.cn>
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.
10 * see Documentation/dvb/README.dvb-usb for more information
12 #include "az6027.h"
14 #include "stb0899_drv.h"
15 #include "stb0899_reg.h"
16 #include "stb0899_cfg.h"
18 #include "stb6100.h"
19 #include "stb6100_cfg.h"
20 #include "dvb_ca_en50221.h"
22 int dvb_usb_az6027_debug;
23 module_param_named(debug, dvb_usb_az6027_debug, int, 0644);
24 MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
26 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
28 struct az6027_device_state {
29 struct dvb_ca_en50221 ca;
30 struct mutex ca_mutex;
31 u8 power_state;
34 static const struct stb0899_s1_reg az6027_stb0899_s1_init_1[] = {
36 /* 0x0000000b, SYSREG */
37 { 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 },
47 /* SYSREG ? */
48 { 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 },
123 static 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 },
128 { STB0899_RTC , 0x23 },
129 { STB0899_TMGCFG , 0x4e },
130 { STB0899_AGC2REF , 0x34 },
131 { STB0899_TLSR , 0x84 },
132 { STB0899_CFD , 0xf7 },
133 { STB0899_ACLC , 0x87 },
134 { 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 },
186 { STB0899_FECM , 0x01 },
187 { STB0899_VTH12 , 0xb0 },
188 { STB0899_VTH23 , 0x7a },
189 { STB0899_VTH34 , 0x58 },
190 { 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 },
198 { STB0899_TSLPL , 0x12 },
199 { 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 },
258 struct 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,
265 .demod_address = 0xd0, /* 0x68, 0xd0 >> 1 */
267 .xtal_freq = 27000000,
268 .inversion = IQ_SWAP_ON, /* 1 */
270 .lo_clk = 76500000,
271 .hi_clk = 99000000,
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,
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,
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,
295 struct stb6100_config az6027_stb6100_config = {
296 .tuner_address = 0xc0,
297 .refclock = 27000000,
301 /* check for mutex FIXME */
302 int az6027_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
304 int ret = -1;
305 if (mutex_lock_interruptible(&d->usb_mutex))
306 return -EAGAIN;
308 ret = usb_control_msg(d->udev,
309 usb_rcvctrlpipe(d->udev, 0),
310 req,
311 USB_TYPE_VENDOR | USB_DIR_IN,
312 value,
313 index,
315 blen,
316 2000);
318 if (ret < 0) {
319 warn("usb in operation failed. (%d)", ret);
320 ret = -EIO;
321 } else
322 ret = 0;
324 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value, index);
325 debug_dump(b, blen, deb_xfer);
327 mutex_unlock(&d->usb_mutex);
328 return ret;
331 static 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)
338 int ret;
340 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value, index);
341 debug_dump(b, blen, deb_xfer);
343 if (mutex_lock_interruptible(&d->usb_mutex))
344 return -EAGAIN;
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,
353 blen,
354 2000);
356 if (ret != blen) {
357 warn("usb out operation failed. (%d)", ret);
358 mutex_unlock(&d->usb_mutex);
359 return -EIO;
360 } else{
361 mutex_unlock(&d->usb_mutex);
362 return 0;
366 static int az6027_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
368 int ret;
369 u8 req;
370 u16 value;
371 u16 index;
372 int blen;
374 deb_info("%s %d", __func__, onoff);
376 req = 0xBC;
377 value = onoff;
378 index = 0;
379 blen = 0;
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);
385 return ret;
388 /* keys for the enclosed remote control */
389 static struct dvb_usb_rc_key az6027_rc_keys[] = {
390 { 0x01, KEY_1 },
391 { 0x02, KEY_2 },
394 /* remote control stuff (does not work with my box) */
395 static int az6027_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
397 return 0;
401 int az6027_power_ctrl(struct dvb_usb_device *d, int onoff)
403 u8 v = onoff;
404 return az6027_usb_out_op(d,0xBC,v,3,NULL,1);
408 static int az6027_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
409 int slot,
410 int address)
412 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
413 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
415 int ret;
416 u8 req;
417 u16 value;
418 u16 index;
419 int blen;
420 u8 b[12];
422 if (slot != 0)
423 return -EINVAL;
425 mutex_lock(&state->ca_mutex);
427 req = 0xC1;
428 value = address;
429 index = 0;
430 blen = 1;
432 ret = az6027_usb_in_op(d, req, value, index, b, blen);
433 if (ret < 0) {
434 warn("usb in operation failed. (%d)", ret);
435 ret = -EINVAL;
436 } else {
437 ret = b[0];
440 mutex_unlock(&state->ca_mutex);
441 return ret;
444 static int az6027_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
445 int slot,
446 int address,
447 u8 value)
449 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
450 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
452 int ret;
453 u8 req;
454 u16 value1;
455 u16 index;
456 int blen;
458 deb_info("%s %d", __func__, slot);
459 if (slot != 0)
460 return -EINVAL;
462 mutex_lock(&state->ca_mutex);
463 req = 0xC2;
464 value1 = address;
465 index = value;
466 blen = 0;
468 ret = az6027_usb_out_op(d, req, value1, index, NULL, blen);
469 if (ret != 0)
470 warn("usb out operation failed. (%d)", ret);
472 mutex_unlock(&state->ca_mutex);
473 return ret;
476 static int az6027_ci_read_cam_control(struct dvb_ca_en50221 *ca,
477 int slot,
478 u8 address)
480 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
481 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
483 int ret;
484 u8 req;
485 u16 value;
486 u16 index;
487 int blen;
488 u8 b[12];
490 if (slot != 0)
491 return -EINVAL;
493 mutex_lock(&state->ca_mutex);
495 req = 0xC3;
496 value = address;
497 index = 0;
498 blen = 2;
500 ret = az6027_usb_in_op(d, req, value, index, b, blen);
501 if (ret < 0) {
502 warn("usb in operation failed. (%d)", ret);
503 ret = -EINVAL;
504 } else {
505 if (b[0] == 0)
506 warn("Read CI IO error");
508 ret = b[1];
509 deb_info("read cam data = %x from 0x%x", b[1], value);
512 mutex_unlock(&state->ca_mutex);
513 return ret;
516 static int az6027_ci_write_cam_control(struct dvb_ca_en50221 *ca,
517 int slot,
518 u8 address,
519 u8 value)
521 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
522 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
524 int ret;
525 u8 req;
526 u16 value1;
527 u16 index;
528 int blen;
530 if (slot != 0)
531 return -EINVAL;
533 mutex_lock(&state->ca_mutex);
534 req = 0xC4;
535 value1 = address;
536 index = value;
537 blen = 0;
539 ret = az6027_usb_out_op(d, req, value1, index, NULL, blen);
540 if (ret != 0) {
541 warn("usb out operation failed. (%d)", ret);
542 goto failed;
545 failed:
546 mutex_unlock(&state->ca_mutex);
547 return ret;
550 static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot)
552 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
554 int ret;
555 u8 req;
556 u16 value;
557 u16 index;
558 int blen;
559 u8 b[12];
561 req = 0xC8;
562 value = 0;
563 index = 0;
564 blen = 1;
566 ret = az6027_usb_in_op(d, req, value, index, b, blen);
567 if (ret < 0) {
568 warn("usb in operation failed. (%d)", ret);
569 ret = -EIO;
570 } else{
571 ret = b[0];
573 return ret;
576 static int az6027_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
578 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
579 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
581 int ret, i;
582 u8 req;
583 u16 value;
584 u16 index;
585 int blen;
587 mutex_lock(&state->ca_mutex);
589 req = 0xC6;
590 value = 1;
591 index = 0;
592 blen = 0;
594 ret = az6027_usb_out_op(d, req, value, index, NULL, blen);
595 if (ret != 0) {
596 warn("usb out operation failed. (%d)", ret);
597 goto failed;
600 msleep(500);
601 req = 0xC6;
602 value = 0;
603 index = 0;
604 blen = 0;
606 ret = az6027_usb_out_op(d, req, value, index, NULL, blen);
607 if (ret != 0) {
608 warn("usb out operation failed. (%d)", ret);
609 goto failed;
612 for (i = 0; i < 15; i++) {
613 msleep(100);
615 if (CI_CamReady(ca, slot)) {
616 deb_info("CAM Ready");
617 break;
620 msleep(5000);
622 failed:
623 mutex_unlock(&state->ca_mutex);
624 return ret;
627 static int az6027_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
629 return 0;
632 static int az6027_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
634 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
635 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
637 int ret;
638 u8 req;
639 u16 value;
640 u16 index;
641 int blen;
643 deb_info("%s", __func__);
644 mutex_lock(&state->ca_mutex);
645 req = 0xC7;
646 value = 1;
647 index = 0;
648 blen = 0;
650 ret = az6027_usb_out_op(d, req, value, index, NULL, blen);
651 if (ret != 0) {
652 warn("usb out operation failed. (%d)", ret);
653 goto failed;
656 failed:
657 mutex_unlock(&state->ca_mutex);
658 return ret;
661 static int az6027_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
663 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
664 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
665 int ret;
666 u8 req;
667 u16 value;
668 u16 index;
669 int blen;
670 u8 b[12];
672 mutex_lock(&state->ca_mutex);
674 req = 0xC5;
675 value = 0;
676 index = 0;
677 blen = 1;
679 ret = az6027_usb_in_op(d, req, value, index, b, blen);
680 if (ret < 0) {
681 warn("usb in operation failed. (%d)", ret);
682 ret = -EIO;
683 } else
684 ret = 0;
686 if (b[0] == 0) {
687 ret = 0;
689 } else if (b[0] == 1) {
690 ret = DVB_CA_EN50221_POLL_CAM_PRESENT |
691 DVB_CA_EN50221_POLL_CAM_READY;
694 mutex_unlock(&state->ca_mutex);
695 return ret;
699 static void az6027_ci_uninit(struct dvb_usb_device *d)
701 struct az6027_device_state *state;
703 deb_info("%s", __func__);
705 if (NULL == d)
706 return;
708 state = (struct az6027_device_state *)d->priv;
709 if (NULL == state)
710 return;
712 if (NULL == state->ca.data)
713 return;
715 dvb_ca_en50221_release(&state->ca);
717 memset(&state->ca, 0, sizeof(state->ca));
721 static int az6027_ci_init(struct dvb_usb_adapter *a)
723 struct dvb_usb_device *d = a->dev;
724 struct az6027_device_state *state = (struct az6027_device_state *)d->priv;
725 int ret;
727 deb_info("%s", __func__);
729 mutex_init(&state->ca_mutex);
731 state->ca.owner = THIS_MODULE;
732 state->ca.read_attribute_mem = az6027_ci_read_attribute_mem;
733 state->ca.write_attribute_mem = az6027_ci_write_attribute_mem;
734 state->ca.read_cam_control = az6027_ci_read_cam_control;
735 state->ca.write_cam_control = az6027_ci_write_cam_control;
736 state->ca.slot_reset = az6027_ci_slot_reset;
737 state->ca.slot_shutdown = az6027_ci_slot_shutdown;
738 state->ca.slot_ts_enable = az6027_ci_slot_ts_enable;
739 state->ca.poll_slot_status = az6027_ci_poll_slot_status;
740 state->ca.data = d;
742 ret = dvb_ca_en50221_init(&a->dvb_adap,
743 &state->ca,
744 0, /* flags */
745 1);/* n_slots */
746 if (ret != 0) {
747 err("Cannot initialize CI: Error %d.", ret);
748 memset(&state->ca, 0, sizeof(state->ca));
749 return ret;
752 deb_info("CI initialized.");
754 return 0;
758 static int az6027_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
760 az6027_usb_in_op(d, 0xb7, 6, 0, &mac[0], 6);
761 return 0;
765 static int az6027_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
768 u8 buf;
769 int ret;
770 struct dvb_usb_adapter *adap = fe->dvb->priv;
772 struct i2c_msg i2c_msg = {
773 .addr = 0x99,
774 .flags = 0,
775 .buf = &buf,
776 .len = 1
780 * 2 --18v
781 * 1 --13v
782 * 0 --off
784 switch (voltage) {
785 case SEC_VOLTAGE_13:
786 buf = 1;
787 ret = i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
788 break;
790 case SEC_VOLTAGE_18:
791 buf = 2;
792 ret = i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
793 break;
795 case SEC_VOLTAGE_OFF:
796 buf = 0;
797 ret = i2c_transfer(&adap->dev->i2c_adap, &i2c_msg, 1);
798 break;
800 default:
801 return -EINVAL;
803 return 0;
807 static int az6027_frontend_poweron(struct dvb_usb_adapter *adap)
809 int ret;
810 u8 req;
811 u16 value;
812 u16 index;
813 int blen;
815 req = 0xBC;
816 value = 1; /* power on */
817 index = 3;
818 blen = 0;
820 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
821 if (ret != 0)
822 return -EIO;
824 return 0;
826 static int az6027_frontend_reset(struct dvb_usb_adapter *adap)
828 int ret;
829 u8 req;
830 u16 value;
831 u16 index;
832 int blen;
834 /* reset demodulator */
835 req = 0xC0;
836 value = 1; /* high */
837 index = 3;
838 blen = 0;
840 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
841 if (ret != 0)
842 return -EIO;
844 req = 0xC0;
845 value = 0; /* low */
846 index = 3;
847 blen = 0;
848 msleep_interruptible(200);
850 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
851 if (ret != 0)
852 return -EIO;
854 msleep_interruptible(200);
856 req = 0xC0;
857 value = 1; /*high */
858 index = 3;
859 blen = 0;
861 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
862 if (ret != 0)
863 return -EIO;
865 msleep_interruptible(200);
866 return 0;
869 static int az6027_frontend_tsbypass(struct dvb_usb_adapter *adap, int onoff)
871 int ret;
872 u8 req;
873 u16 value;
874 u16 index;
875 int blen;
877 /* TS passthrough */
878 req = 0xC7;
879 value = onoff;
880 index = 0;
881 blen = 0;
883 ret = az6027_usb_out_op(adap->dev, req, value, index, NULL, blen);
884 if (ret != 0)
885 return -EIO;
887 return 0;
890 static int az6027_frontend_attach(struct dvb_usb_adapter *adap)
893 az6027_frontend_poweron(adap);
894 az6027_frontend_reset(adap);
896 deb_info("adap = %p, dev = %p\n", adap, adap->dev);
897 adap->fe = stb0899_attach(&az6027_stb0899_config, &adap->dev->i2c_adap);
899 if (adap->fe) {
900 deb_info("found STB0899 DVB-S/DVB-S2 frontend @0x%02x", az6027_stb0899_config.demod_address);
901 if (stb6100_attach(adap->fe, &az6027_stb6100_config, &adap->dev->i2c_adap)) {
902 deb_info("found STB6100 DVB-S/DVB-S2 frontend @0x%02x", az6027_stb6100_config.tuner_address);
903 adap->fe->ops.set_voltage = az6027_set_voltage;
904 az6027_ci_init(adap);
905 } else {
906 adap->fe = NULL;
908 } else
909 warn("no front-end attached\n");
911 az6027_frontend_tsbypass(adap, 0);
913 return 0;
916 static struct dvb_usb_device_properties az6027_properties;
918 static void az6027_usb_disconnect(struct usb_interface *intf)
920 struct dvb_usb_device *d = usb_get_intfdata(intf);
921 az6027_ci_uninit(d);
922 dvb_usb_device_exit(intf);
926 static int az6027_usb_probe(struct usb_interface *intf,
927 const struct usb_device_id *id)
929 return dvb_usb_device_init(intf,
930 &az6027_properties,
931 THIS_MODULE,
932 NULL,
933 adapter_nr);
936 /* I2C */
937 static int az6027_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
939 struct dvb_usb_device *d = i2c_get_adapdata(adap);
940 int i = 0, j = 0, len = 0;
941 int ret;
942 u16 index;
943 u16 value;
944 int length;
945 u8 req;
946 u8 data[256];
948 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
949 return -EAGAIN;
951 if (num > 2)
952 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
954 for (i = 0; i < num; i++) {
956 if (msg[i].addr == 0x99) {
957 req = 0xBE;
958 index = 0;
959 value = msg[i].buf[0] & 0x00ff;
960 length = 1;
961 az6027_usb_out_op(d, req, value, index, data, length);
964 if (msg[i].addr == 0xd0) {
965 /* write/read request */
966 if (i + 1 < num && (msg[i + 1].flags & I2C_M_RD)) {
967 req = 0xB9;
968 index = (((msg[i].buf[0] << 8) & 0xff00) | (msg[i].buf[1] & 0x00ff));
969 value = msg[i].addr + (msg[i].len << 8);
970 length = msg[i + 1].len + 6;
971 ret = az6027_usb_in_op(d, req, value, index, data, length);
972 len = msg[i + 1].len;
973 for (j = 0; j < len; j++)
974 msg[i + 1].buf[j] = data[j + 5];
976 i++;
977 } else {
979 if (msg[i].addr == 0xd0) {
980 /* demod 16bit addr */
981 req = 0xBD;
982 index = (((msg[i].buf[0] << 8) & 0xff00) | (msg[i].buf[1] & 0x00ff));
983 value = msg[i].addr + (2 << 8);
984 length = msg[i].len - 2;
985 len = msg[i].len - 2;
986 for (j = 0; j < len; j++)
987 data[j] = msg[i].buf[j + 2];
990 az6027_usb_out_op(d, req, value, index, data, length);
994 if (msg[i].addr == 0xc0) {
995 if (msg[i].flags & I2C_M_RD) {
997 req = 0xB9;
998 index = 0x0;
999 value = msg[i].addr;
1000 length = msg[i].len + 6;
1001 ret = az6027_usb_in_op(d, req, value, index, data, length);
1002 len = msg[i].len;
1003 for (j = 0; j < len; j++)
1004 msg[i].buf[j] = data[j + 5];
1006 } else {
1008 req = 0xBD;
1009 index = msg[i].buf[0] & 0x00FF;
1010 value = msg[i].addr + (1 << 8);
1011 length = msg[i].len - 1;
1012 len = msg[i].len - 1;
1014 for (j = 0; j < len; j++)
1015 data[j] = msg[i].buf[j + 1];
1017 az6027_usb_out_op(d, req, value, index, data, length);
1021 mutex_unlock(&d->i2c_mutex);
1023 return i;
1027 static u32 az6027_i2c_func(struct i2c_adapter *adapter)
1029 return I2C_FUNC_I2C;
1032 static struct i2c_algorithm az6027_i2c_algo = {
1033 .master_xfer = az6027_i2c_xfer,
1034 .functionality = az6027_i2c_func,
1037 int az6027_identify_state(struct usb_device *udev,
1038 struct dvb_usb_device_properties *props,
1039 struct dvb_usb_device_description **desc,
1040 int *cold)
1042 u8 b[16];
1043 s16 ret = usb_control_msg(udev,
1044 usb_rcvctrlpipe(udev, 0),
1045 0xb7,
1046 USB_TYPE_VENDOR | USB_DIR_IN,
1051 USB_CTRL_GET_TIMEOUT);
1053 *cold = ret <= 0;
1055 deb_info("cold: %d\n", *cold);
1056 return 0;
1060 static struct usb_device_id az6027_usb_table[] = {
1061 { USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_AZ6027) },
1062 { USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_DVBS2CI) },
1063 { USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_HDCI) },
1064 { },
1067 MODULE_DEVICE_TABLE(usb, az6027_usb_table);
1069 static struct dvb_usb_device_properties az6027_properties = {
1070 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1071 .usb_ctrl = CYPRESS_FX2,
1072 .firmware = "dvb-usb-az6027-03.fw",
1073 .no_reconnect = 1,
1075 .size_of_priv = sizeof(struct az6027_device_state),
1076 .identify_state = az6027_identify_state,
1077 .num_adapters = 1,
1078 .adapter = {
1080 .streaming_ctrl = az6027_streaming_ctrl,
1081 .frontend_attach = az6027_frontend_attach,
1083 /* parameter for the MPEG2-data transfer */
1084 .stream = {
1085 .type = USB_BULK,
1086 .count = 10,
1087 .endpoint = 0x02,
1088 .u = {
1089 .bulk = {
1090 .buffersize = 4096,
1097 .power_ctrl = az6027_power_ctrl,
1098 .read_mac_address = az6027_read_mac_addr,
1100 .rc_key_map = az6027_rc_keys,
1101 .rc_key_map_size = ARRAY_SIZE(az6027_rc_keys),
1102 .rc_interval = 400,
1103 .rc_query = az6027_rc_query,
1104 .i2c_algo = &az6027_i2c_algo,
1106 .num_device_descs = 1,
1107 .devices = {
1109 .name = "AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)",
1110 .cold_ids = { &az6027_usb_table[0], NULL },
1111 .warm_ids = { NULL },
1113 { NULL },
1117 /* usb specific object needed to register this driver with the usb subsystem */
1118 static struct usb_driver az6027_usb_driver = {
1119 .name = "dvb_usb_az6027",
1120 .probe = az6027_usb_probe,
1121 .disconnect = az6027_usb_disconnect,
1122 .id_table = az6027_usb_table,
1125 /* module stuff */
1126 static int __init az6027_usb_module_init(void)
1128 int result;
1130 result = usb_register(&az6027_usb_driver);
1131 if (result) {
1132 err("usb_register failed. (%d)", result);
1133 return result;
1136 return 0;
1139 static void __exit az6027_usb_module_exit(void)
1141 /* deregister this driver from the USB subsystem */
1142 usb_deregister(&az6027_usb_driver);
1145 module_init(az6027_usb_module_init);
1146 module_exit(az6027_usb_module_exit);
1148 MODULE_AUTHOR("Adams Xu <Adams.xu@azwave.com.cn>");
1149 MODULE_DESCRIPTION("Driver for AZUREWAVE DVB-S/S2 USB2.0 (AZ6027)");
1150 MODULE_VERSION("1.0");
1151 MODULE_LICENSE("GPL");