blob: c65f273ff313cda9d874f3183f343949da953169 [file] [log] [blame]
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -03001/* DVB USB framework compliant Linux driver for the
2* DVBWorld DVB-S 2101, 2102, DVB-S2 2104 Card
Igor M Liplianin7fd48282008-07-20 08:05:50 -03003*
4* Copyright (C) 2008 Igor M. Liplianin (liplianin@me.by)
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
8* Free Software Foundation, version 2.
9*
10* see Documentation/dvb/README.dvb-usb for more information
11*/
Igor M Liplianin7fd48282008-07-20 08:05:50 -030012#include "dw2102.h"
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030013#include "si21xx.h"
Igor M Liplianin7fd48282008-07-20 08:05:50 -030014#include "stv0299.h"
15#include "z0194a.h"
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -030016#include "stv0288.h"
17#include "stb6000.h"
18#include "eds1547.h"
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030019#include "cx24116.h"
Igor M Liplianin7fd48282008-07-20 08:05:50 -030020
21#ifndef USB_PID_DW2102
22#define USB_PID_DW2102 0x2102
23#endif
24
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030025#ifndef USB_PID_DW2104
26#define USB_PID_DW2104 0x2104
27#endif
28
Igor M. Liplianin4cc0edf2008-11-05 22:12:56 -030029#ifndef USB_PID_CINERGY_S
30#define USB_PID_CINERGY_S 0x0064
31#endif
32
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030033#define DW210X_READ_MSG 0
34#define DW210X_WRITE_MSG 1
Igor M Liplianin7fd48282008-07-20 08:05:50 -030035
36#define REG_1F_SYMBOLRATE_BYTE0 0x1f
37#define REG_20_SYMBOLRATE_BYTE1 0x20
38#define REG_21_SYMBOLRATE_BYTE2 0x21
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030039/* on my own*/
Igor M Liplianin7fd48282008-07-20 08:05:50 -030040#define DW2102_VOLTAGE_CTRL (0x1800)
41#define DW2102_RC_QUERY (0x1a00)
42
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030043struct dw210x_state {
Igor M Liplianin7fd48282008-07-20 08:05:50 -030044 u32 last_key_pressed;
45};
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030046struct dw210x_rc_keys {
Igor M Liplianin7fd48282008-07-20 08:05:50 -030047 u32 keycode;
48 u32 event;
49};
50
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030051/* debug */
52static int dvb_usb_dw2102_debug;
53module_param_named(debug, dvb_usb_dw2102_debug, int, 0644);
54MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer (or-able))." DVB_USB_DEBUG_STATUS);
55
Igor M Liplianin7fd48282008-07-20 08:05:50 -030056DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
57
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030058static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030059 u16 index, u8 * data, u16 len, int flags)
Igor M Liplianin7fd48282008-07-20 08:05:50 -030060{
61 int ret;
62 u8 u8buf[len];
63
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030064 unsigned int pipe = (flags == DW210X_READ_MSG) ?
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030065 usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030066 u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
Igor M Liplianin7fd48282008-07-20 08:05:50 -030067
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030068 if (flags == DW210X_WRITE_MSG)
Igor M Liplianin7fd48282008-07-20 08:05:50 -030069 memcpy(u8buf, data, len);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030070 ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
71 value, index , u8buf, len, 2000);
Igor M Liplianin7fd48282008-07-20 08:05:50 -030072
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030073 if (flags == DW210X_READ_MSG)
Igor M Liplianin7fd48282008-07-20 08:05:50 -030074 memcpy(data, u8buf, len);
75 return ret;
76}
77
78/* I2C */
Igor M Liplianin7fd48282008-07-20 08:05:50 -030079static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
80 int num)
81{
82struct dvb_usb_device *d = i2c_get_adapdata(adap);
83 int i = 0, ret = 0;
84 u8 buf6[] = {0x2c, 0x05, 0xc0, 0, 0, 0, 0};
Igor M Liplianin7fd48282008-07-20 08:05:50 -030085 u16 value;
86
87 if (!d)
88 return -ENODEV;
89 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
90 return -EAGAIN;
91
92 switch (num) {
93 case 2:
94 /* read stv0299 register */
Igor M Liplianin7fd48282008-07-20 08:05:50 -030095 value = msg[0].buf[0];/* register */
96 for (i = 0; i < msg[1].len; i++) {
97 value = value + i;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030098 ret = dw210x_op_rw(d->udev, 0xb5, value, 0,
99 buf6, 2, DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300100 msg[1].buf[i] = buf6[0];
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300101 }
102 break;
103 case 1:
104 switch (msg[0].addr) {
105 case 0x68:
106 /* write to stv0299 register */
107 buf6[0] = 0x2a;
108 buf6[1] = msg[0].buf[0];
109 buf6[2] = msg[0].buf[1];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300110 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
111 buf6, 3, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300112 break;
113 case 0x60:
114 if (msg[0].flags == 0) {
115 /* write to tuner pll */
116 buf6[0] = 0x2c;
117 buf6[1] = 5;
118 buf6[2] = 0xc0;
119 buf6[3] = msg[0].buf[0];
120 buf6[4] = msg[0].buf[1];
121 buf6[5] = msg[0].buf[2];
122 buf6[6] = msg[0].buf[3];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300123 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
124 buf6, 7, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300125 } else {
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300126 /* read from tuner */
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300127 ret = dw210x_op_rw(d->udev, 0xb5, 0, 0,
128 buf6, 1, DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300129 msg[0].buf[0] = buf6[0];
130 }
131 break;
132 case (DW2102_RC_QUERY):
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300133 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
134 buf6, 2, DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300135 msg[0].buf[0] = buf6[0];
136 msg[0].buf[1] = buf6[1];
137 break;
138 case (DW2102_VOLTAGE_CTRL):
139 buf6[0] = 0x30;
140 buf6[1] = msg[0].buf[0];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300141 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
142 buf6, 2, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300143 break;
144 }
145
146 break;
147 }
148
149 mutex_unlock(&d->i2c_mutex);
150 return num;
151}
152
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300153static int dw2102_serit_i2c_transfer(struct i2c_adapter *adap,
154 struct i2c_msg msg[], int num)
155{
156 struct dvb_usb_device *d = i2c_get_adapdata(adap);
157 int ret = 0;
158 u8 buf6[] = {0, 0, 0, 0, 0, 0, 0};
159
160 if (!d)
161 return -ENODEV;
162 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
163 return -EAGAIN;
164
165 switch (num) {
166 case 2:
167 /* read si2109 register by number */
168 buf6[0] = 0xd0;
169 buf6[1] = msg[0].len;
170 buf6[2] = msg[0].buf[0];
171 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
172 buf6, msg[0].len + 2, DW210X_WRITE_MSG);
173 /* read si2109 register */
174 ret = dw210x_op_rw(d->udev, 0xc3, 0xd0, 0,
175 buf6, msg[1].len + 2, DW210X_READ_MSG);
176 memcpy(msg[1].buf, buf6 + 2, msg[1].len);
177
178 break;
179 case 1:
180 switch (msg[0].addr) {
181 case 0x68:
182 /* write to si2109 register */
183 buf6[0] = 0xd0;
184 buf6[1] = msg[0].len;
185 memcpy(buf6 + 2, msg[0].buf, msg[0].len);
186 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0, buf6,
187 msg[0].len + 2, DW210X_WRITE_MSG);
188 break;
189 case(DW2102_RC_QUERY):
190 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
191 buf6, 2, DW210X_READ_MSG);
192 msg[0].buf[0] = buf6[0];
193 msg[0].buf[1] = buf6[1];
194 break;
195 case(DW2102_VOLTAGE_CTRL):
196 buf6[0] = 0x30;
197 buf6[1] = msg[0].buf[0];
198 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
199 buf6, 2, DW210X_WRITE_MSG);
200 break;
201 }
202 break;
203 }
204
205 mutex_unlock(&d->i2c_mutex);
206 return num;
207}
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300208static int dw2102_earda_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
209{
210 struct dvb_usb_device *d = i2c_get_adapdata(adap);
211 int ret = 0;
212
213 if (!d)
214 return -ENODEV;
215 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
216 return -EAGAIN;
217
218 switch (num) {
219 case 2: {
220 /* read */
221 /* first write first register number */
222 u8 ibuf [msg[1].len + 2], obuf[3];
223 obuf[0] = 0xd0;
224 obuf[1] = msg[0].len;
225 obuf[2] = msg[0].buf[0];
226 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
227 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
228 /* second read registers */
229 ret = dw210x_op_rw(d->udev, 0xc3, 0xd1 , 0,
230 ibuf, msg[1].len + 2, DW210X_READ_MSG);
231 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
232
233 break;
234 }
235 case 1:
236 switch (msg[0].addr) {
237 case 0x68: {
238 /* write to register */
239 u8 obuf[msg[0].len + 2];
240 obuf[0] = 0xd0;
241 obuf[1] = msg[0].len;
242 memcpy(obuf + 2, msg[0].buf, msg[0].len);
243 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
244 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
245 break;
246 }
247 case 0x61: {
248 /* write to tuner */
249 u8 obuf[msg[0].len + 2];
250 obuf[0] = 0xc2;
251 obuf[1] = msg[0].len;
252 memcpy(obuf + 2, msg[0].buf, msg[0].len);
253 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
254 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
255 break;
256 }
257 case(DW2102_RC_QUERY): {
258 u8 ibuf[2];
259 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
260 ibuf, 2, DW210X_READ_MSG);
261 memcpy(msg[0].buf, ibuf , 2);
262 break;
263 }
264 case(DW2102_VOLTAGE_CTRL): {
265 u8 obuf[2];
266 obuf[0] = 0x30;
267 obuf[1] = msg[0].buf[0];
268 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
269 obuf, 2, DW210X_WRITE_MSG);
270 break;
271 }
272 }
273
274 break;
275 }
276
277 mutex_unlock(&d->i2c_mutex);
278 return num;
279}
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300280
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300281static int dw2104_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
282{
283 struct dvb_usb_device *d = i2c_get_adapdata(adap);
284 int ret = 0;
285 int len, i;
286
287 if (!d)
288 return -ENODEV;
289 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
290 return -EAGAIN;
291
292 switch (num) {
293 case 2: {
294 /* read */
295 /* first write first register number */
296 u8 ibuf [msg[1].len + 2], obuf[3];
297 obuf[0] = 0xaa;
298 obuf[1] = msg[0].len;
299 obuf[2] = msg[0].buf[0];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300300 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
301 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300302 /* second read registers */
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300303 ret = dw210x_op_rw(d->udev, 0xc3, 0xab , 0,
304 ibuf, msg[1].len + 2, DW210X_READ_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300305 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
306
307 break;
308 }
309 case 1:
310 switch (msg[0].addr) {
311 case 0x55: {
312 if (msg[0].buf[0] == 0xf7) {
313 /* firmware */
314 /* Write in small blocks */
315 u8 obuf[19];
316 obuf[0] = 0xaa;
317 obuf[1] = 0x11;
318 obuf[2] = 0xf7;
319 len = msg[0].len - 1;
320 i = 1;
321 do {
322 memcpy(obuf + 3, msg[0].buf + i, (len > 16 ? 16 : len));
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300323 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
324 obuf, (len > 16 ? 16 : len) + 3, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300325 i += 16;
326 len -= 16;
327 } while (len > 0);
328 } else {
329 /* write to register */
330 u8 obuf[msg[0].len + 2];
331 obuf[0] = 0xaa;
332 obuf[1] = msg[0].len;
333 memcpy(obuf + 2, msg[0].buf, msg[0].len);
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300334 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
335 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300336 }
337 break;
338 }
339 case(DW2102_RC_QUERY): {
340 u8 ibuf[2];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300341 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
342 ibuf, 2, DW210X_READ_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300343 memcpy(msg[0].buf, ibuf , 2);
344 break;
345 }
346 case(DW2102_VOLTAGE_CTRL): {
347 u8 obuf[2];
348 obuf[0] = 0x30;
349 obuf[1] = msg[0].buf[0];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300350 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
351 obuf, 2, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300352 break;
353 }
354 }
355
356 break;
357 }
358
359 mutex_unlock(&d->i2c_mutex);
360 return num;
361}
362
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300363static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300364{
365 return I2C_FUNC_I2C;
366}
367
368static struct i2c_algorithm dw2102_i2c_algo = {
369 .master_xfer = dw2102_i2c_transfer,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300370 .functionality = dw210x_i2c_func,
371};
372
373static struct i2c_algorithm dw2102_serit_i2c_algo = {
374 .master_xfer = dw2102_serit_i2c_transfer,
375 .functionality = dw210x_i2c_func,
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300376};
377
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300378static struct i2c_algorithm dw2102_earda_i2c_algo = {
379 .master_xfer = dw2102_earda_i2c_transfer,
380 .functionality = dw210x_i2c_func,
381};
382
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300383static struct i2c_algorithm dw2104_i2c_algo = {
384 .master_xfer = dw2104_i2c_transfer,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300385 .functionality = dw210x_i2c_func,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300386};
387
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300388static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300389{
390 int i;
391 u8 ibuf[] = {0, 0};
392 u8 eeprom[256], eepromline[16];
393
394 for (i = 0; i < 256; i++) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300395 if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300396 err("read eeprom failed.");
397 return -1;
398 } else {
399 eepromline[i%16] = ibuf[0];
400 eeprom[i] = ibuf[0];
401 }
402 if ((i % 16) == 15) {
403 deb_xfer("%02x: ", i - 15);
404 debug_dump(eepromline, 16, deb_xfer);
405 }
406 }
407 memcpy(mac, eeprom + 8, 6);
408 return 0;
409};
410
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300411static int dw210x_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300412{
413 static u8 command_13v[1] = {0x00};
414 static u8 command_18v[1] = {0x01};
415 struct i2c_msg msg[] = {
416 {.addr = DW2102_VOLTAGE_CTRL, .flags = 0,
417 .buf = command_13v, .len = 1},
418 };
419
420 struct dvb_usb_adapter *udev_adap =
421 (struct dvb_usb_adapter *)(fe->dvb->priv);
422 if (voltage == SEC_VOLTAGE_18)
423 msg[0].buf = command_18v;
424 i2c_transfer(&udev_adap->dev->i2c_adap, msg, 1);
425 return 0;
426}
427
Igor M. Liplianind4305c62008-10-17 13:45:55 -0300428static struct stv0299_config sharp_z0194a_config = {
429 .demod_address = 0x68,
430 .inittab = sharp_z0194a_inittab,
431 .mclk = 88000000UL,
432 .invert = 1,
433 .skip_reinit = 0,
434 .lock_output = STV0299_LOCKOUTPUT_1,
435 .volt13_op0_op1 = STV0299_VOLT13_OP1,
436 .min_delay_ms = 100,
437 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
438};
439
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300440static struct cx24116_config dw2104_config = {
441 .demod_address = 0x55,
Igor M. Liplianincc8c4f32008-09-09 13:57:47 -0300442 .mpg_clk_pos_pol = 0x01,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300443};
444
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300445static struct si21xx_config serit_sp1511lhb_config = {
446 .demod_address = 0x68,
447 .min_delay_ms = 100,
448
449};
450
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300451static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
452{
453 if ((d->fe = dvb_attach(cx24116_attach, &dw2104_config,
454 &d->dev->i2c_adap)) != NULL) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300455 d->fe->ops.set_voltage = dw210x_set_voltage;
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300456 info("Attached cx24116!\n");
457 return 0;
458 }
459 return -EIO;
460}
461
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300462static struct dvb_usb_device_properties dw2102_properties;
463
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300464static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
465{
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300466 if (dw2102_properties.i2c_algo == &dw2102_serit_i2c_algo) {
467 /*dw2102_properties.adapter->tuner_attach = NULL;*/
468 d->fe = dvb_attach(si21xx_attach, &serit_sp1511lhb_config,
469 &d->dev->i2c_adap);
470 if (d->fe != NULL) {
471 d->fe->ops.set_voltage = dw210x_set_voltage;
472 info("Attached si21xx!\n");
473 return 0;
474 }
475 }
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300476 if (dw2102_properties.i2c_algo == &dw2102_earda_i2c_algo) {
477 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
478 d->fe = dvb_attach(stv0288_attach, &earda_config,
479 &d->dev->i2c_adap);
480 if (d->fe != NULL) {
481 d->fe->ops.set_voltage = dw210x_set_voltage;
482 info("Attached stv0288!\n");
483 return 0;
484 }
485 }
486
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300487 if (dw2102_properties.i2c_algo == &dw2102_i2c_algo) {
488 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
489 d->fe = dvb_attach(stv0299_attach, &sharp_z0194a_config,
490 &d->dev->i2c_adap);
491 if (d->fe != NULL) {
492 d->fe->ops.set_voltage = dw210x_set_voltage;
493 info("Attached stv0299!\n");
494 return 0;
495 }
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300496 }
497 return -EIO;
498}
499
500static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
501{
502 dvb_attach(dvb_pll_attach, adap->fe, 0x60,
503 &adap->dev->i2c_adap, DVB_PLL_OPERA1);
504 return 0;
505}
506
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300507static int dw2102_earda_tuner_attach(struct dvb_usb_adapter *adap)
508{
509 dvb_attach(stb6000_attach, adap->fe, 0x61,
510 &adap->dev->i2c_adap);
511
512 return 0;
513}
514
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300515static struct dvb_usb_rc_key dw210x_rc_keys[] = {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300516 { 0xf8, 0x0a, KEY_Q }, /*power*/
517 { 0xf8, 0x0c, KEY_M }, /*mute*/
518 { 0xf8, 0x11, KEY_1 },
519 { 0xf8, 0x12, KEY_2 },
520 { 0xf8, 0x13, KEY_3 },
521 { 0xf8, 0x14, KEY_4 },
522 { 0xf8, 0x15, KEY_5 },
523 { 0xf8, 0x16, KEY_6 },
524 { 0xf8, 0x17, KEY_7 },
525 { 0xf8, 0x18, KEY_8 },
526 { 0xf8, 0x19, KEY_9 },
527 { 0xf8, 0x10, KEY_0 },
528 { 0xf8, 0x1c, KEY_PAGEUP }, /*ch+*/
529 { 0xf8, 0x0f, KEY_PAGEDOWN }, /*ch-*/
530 { 0xf8, 0x1a, KEY_O }, /*vol+*/
531 { 0xf8, 0x0e, KEY_Z }, /*vol-*/
532 { 0xf8, 0x04, KEY_R }, /*rec*/
533 { 0xf8, 0x09, KEY_D }, /*fav*/
534 { 0xf8, 0x08, KEY_BACKSPACE }, /*rewind*/
535 { 0xf8, 0x07, KEY_A }, /*fast*/
536 { 0xf8, 0x0b, KEY_P }, /*pause*/
537 { 0xf8, 0x02, KEY_ESC }, /*cancel*/
538 { 0xf8, 0x03, KEY_G }, /*tab*/
539 { 0xf8, 0x00, KEY_UP }, /*up*/
540 { 0xf8, 0x1f, KEY_ENTER }, /*ok*/
541 { 0xf8, 0x01, KEY_DOWN }, /*down*/
542 { 0xf8, 0x05, KEY_C }, /*cap*/
543 { 0xf8, 0x06, KEY_S }, /*stop*/
544 { 0xf8, 0x40, KEY_F }, /*full*/
545 { 0xf8, 0x1e, KEY_W }, /*tvmode*/
546 { 0xf8, 0x1b, KEY_B }, /*recall*/
547
548};
549
550
551
552static int dw2102_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
553{
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300554 struct dw210x_state *st = d->priv;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300555 u8 key[2];
556 struct i2c_msg msg[] = {
557 {.addr = DW2102_RC_QUERY, .flags = I2C_M_RD, .buf = key,
558 .len = 2},
559 };
560 int i;
561
562 *state = REMOTE_NO_KEY_PRESSED;
563 if (dw2102_i2c_transfer(&d->i2c_adap, msg, 1) == 1) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300564 for (i = 0; i < ARRAY_SIZE(dw210x_rc_keys); i++) {
565 if (dw210x_rc_keys[i].data == msg[0].buf[0]) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300566 *state = REMOTE_KEY_PRESSED;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300567 *event = dw210x_rc_keys[i].event;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300568 st->last_key_pressed =
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300569 dw210x_rc_keys[i].event;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300570 break;
571 }
572 st->last_key_pressed = 0;
573 }
574 }
575 /* info("key: %x %x\n",key[0],key[1]); */
576 return 0;
577}
578
579static struct usb_device_id dw2102_table[] = {
580 {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
581 {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300582 {USB_DEVICE(USB_VID_CYPRESS, 0x2104)},
583 {USB_DEVICE(0x9022, 0xd650)},
Igor M. Liplianin4cc0edf2008-11-05 22:12:56 -0300584 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_CINERGY_S)},
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300585 { }
586};
587
588MODULE_DEVICE_TABLE(usb, dw2102_table);
589
590static int dw2102_load_firmware(struct usb_device *dev,
591 const struct firmware *frmwr)
592{
593 u8 *b, *p;
594 int ret = 0, i;
595 u8 reset;
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300596 u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300597 const struct firmware *fw;
598 const char *filename = "dvb-usb-dw2101.fw";
599 switch (dev->descriptor.idProduct) {
600 case 0x2101:
601 ret = request_firmware(&fw, filename, &dev->dev);
602 if (ret != 0) {
603 err("did not find the firmware file. (%s) "
604 "Please see linux/Documentation/dvb/ for more details "
605 "on firmware-problems.", filename);
606 return ret;
607 }
608 break;
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300609 default:
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300610 fw = frmwr;
611 break;
612 }
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300613 info("start downloading DW210X firmware");
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300614 p = kmalloc(fw->size, GFP_KERNEL);
615 reset = 1;
616 /*stop the CPU*/
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300617 dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
618 dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300619
620 if (p != NULL) {
621 memcpy(p, fw->data, fw->size);
622 for (i = 0; i < fw->size; i += 0x40) {
623 b = (u8 *) p + i;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300624 if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
625 DW210X_WRITE_MSG) != 0x40) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300626 err("error while transferring firmware");
627 ret = -EINVAL;
628 break;
629 }
630 }
631 /* restart the CPU */
632 reset = 0;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300633 if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
634 DW210X_WRITE_MSG) != 1) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300635 err("could not restart the USB controller CPU.");
636 ret = -EINVAL;
637 }
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300638 if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
639 DW210X_WRITE_MSG) != 1) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300640 err("could not restart the USB controller CPU.");
641 ret = -EINVAL;
642 }
643 /* init registers */
644 switch (dev->descriptor.idProduct) {
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300645 case USB_PID_DW2104:
646 case 0xd650:
647 reset = 1;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300648 dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
649 DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300650 reset = 0;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300651 dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
652 DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300653 break;
Igor M. Liplianin4cc0edf2008-11-05 22:12:56 -0300654 case USB_PID_CINERGY_S:
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300655 case USB_PID_DW2102:
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300656 dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
657 DW210X_WRITE_MSG);
658 dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
659 DW210X_READ_MSG);
660 /* check STV0299 frontend */
661 dw210x_op_rw(dev, 0xb5, 0, 0, &reset16[0], 2,
662 DW210X_READ_MSG);
Igor M. Liplianinea023df2008-12-04 12:49:23 -0300663 if ((reset16[0] == 0xa1) || (reset16[0] == 0x80)) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300664 dw2102_properties.i2c_algo = &dw2102_i2c_algo;
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300665 dw2102_properties.adapter->tuner_attach = &dw2102_tuner_attach;
666 break;
667 } else {
668 /* check STV0288 frontend */
669 reset16[0] = 0xd0;
670 reset16[1] = 1;
671 reset16[2] = 0;
672 dw210x_op_rw(dev, 0xc2, 0, 0, &reset16[0], 3,
673 DW210X_WRITE_MSG);
674 dw210x_op_rw(dev, 0xc3, 0xd1, 0, &reset16[0], 3,
675 DW210X_READ_MSG);
676 if (reset16[2] == 0x11) {
677 dw2102_properties.i2c_algo = &dw2102_earda_i2c_algo;
678 dw2102_properties.adapter->tuner_attach = &dw2102_earda_tuner_attach;
679 break;
680 }
681 }
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300682 case 0x2101:
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300683 dw210x_op_rw(dev, 0xbc, 0x0030, 0, &reset16[0], 2,
684 DW210X_READ_MSG);
685 dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
686 DW210X_READ_MSG);
687 dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
688 DW210X_READ_MSG);
689 dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
690 DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300691 break;
692 }
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300693 msleep(100);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300694 kfree(p);
695 }
696 return ret;
697}
698
699static struct dvb_usb_device_properties dw2102_properties = {
700 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
701 .usb_ctrl = DEVICE_SPECIFIC,
702 .firmware = "dvb-usb-dw2102.fw",
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300703 .size_of_priv = sizeof(struct dw210x_state),
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300704 .no_reconnect = 1,
705
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300706 .i2c_algo = &dw2102_serit_i2c_algo,
707 .rc_key_map = dw210x_rc_keys,
708 .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300709 .rc_interval = 150,
710 .rc_query = dw2102_rc_query,
711
712 .generic_bulk_ctrl_endpoint = 0x81,
713 /* parameter for the MPEG2-data transfer */
714 .num_adapters = 1,
715 .download_firmware = dw2102_load_firmware,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300716 .read_mac_address = dw210x_read_mac_address,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300717 .adapter = {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300718 {
719 .frontend_attach = dw2102_frontend_attach,
720 .streaming_ctrl = NULL,
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300721 .tuner_attach = NULL,
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300722 .stream = {
723 .type = USB_BULK,
724 .count = 8,
725 .endpoint = 0x82,
726 .u = {
727 .bulk = {
728 .buffersize = 4096,
729 }
730 }
731 },
732 }
733 },
Igor M. Liplianin4cc0edf2008-11-05 22:12:56 -0300734 .num_device_descs = 3,
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300735 .devices = {
736 {"DVBWorld DVB-S 2102 USB2.0",
737 {&dw2102_table[0], NULL},
738 {NULL},
739 },
740 {"DVBWorld DVB-S 2101 USB2.0",
741 {&dw2102_table[1], NULL},
742 {NULL},
743 },
Igor M. Liplianin4cc0edf2008-11-05 22:12:56 -0300744 {"TerraTec Cinergy S USB",
745 {&dw2102_table[4], NULL},
746 {NULL},
747 },
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300748 }
749};
750
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300751static struct dvb_usb_device_properties dw2104_properties = {
752 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
753 .usb_ctrl = DEVICE_SPECIFIC,
754 .firmware = "dvb-usb-dw2104.fw",
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300755 .size_of_priv = sizeof(struct dw210x_state),
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300756 .no_reconnect = 1,
757
758 .i2c_algo = &dw2104_i2c_algo,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300759 .rc_key_map = dw210x_rc_keys,
760 .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300761 .rc_interval = 150,
762 .rc_query = dw2102_rc_query,
763
764 .generic_bulk_ctrl_endpoint = 0x81,
765 /* parameter for the MPEG2-data transfer */
766 .num_adapters = 1,
767 .download_firmware = dw2102_load_firmware,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300768 .read_mac_address = dw210x_read_mac_address,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300769 .adapter = {
770 {
771 .frontend_attach = dw2104_frontend_attach,
772 .streaming_ctrl = NULL,
773 /*.tuner_attach = dw2104_tuner_attach,*/
774 .stream = {
775 .type = USB_BULK,
776 .count = 8,
777 .endpoint = 0x82,
778 .u = {
779 .bulk = {
780 .buffersize = 4096,
781 }
782 }
783 },
784 }
785 },
786 .num_device_descs = 2,
787 .devices = {
788 { "DVBWorld DW2104 USB2.0",
789 {&dw2102_table[2], NULL},
790 {NULL},
791 },
792 { "TeVii S650 USB2.0",
793 {&dw2102_table[3], NULL},
794 {NULL},
795 },
796 }
797};
798
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300799static int dw2102_probe(struct usb_interface *intf,
800 const struct usb_device_id *id)
801{
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300802 if (0 == dvb_usb_device_init(intf, &dw2102_properties,
803 THIS_MODULE, NULL, adapter_nr) ||
804 0 == dvb_usb_device_init(intf, &dw2104_properties,
805 THIS_MODULE, NULL, adapter_nr)) {
806 return 0;
807 }
808 return -ENODEV;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300809}
810
811static struct usb_driver dw2102_driver = {
812 .name = "dw2102",
813 .probe = dw2102_probe,
814 .disconnect = dvb_usb_device_exit,
815 .id_table = dw2102_table,
816};
817
818static int __init dw2102_module_init(void)
819{
820 int ret = usb_register(&dw2102_driver);
821 if (ret)
822 err("usb_register failed. Error number %d", ret);
823
824 return ret;
825}
826
827static void __exit dw2102_module_exit(void)
828{
829 usb_deregister(&dw2102_driver);
830}
831
832module_init(dw2102_module_init);
833module_exit(dw2102_module_exit);
834
835MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300836MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104 USB2.0 device");
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300837MODULE_VERSION("0.1");
838MODULE_LICENSE("GPL");