blob: dee9399f85974e75986065f3a538cb74daf2da2b [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);
Igor M. Liplianin8a8dad72009-06-13 08:10:24 -030054MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer 4=rc(or-able))."
55 DVB_USB_DEBUG_STATUS);
56
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030057
Igor M Liplianin7fd48282008-07-20 08:05:50 -030058DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
59
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030060static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030061 u16 index, u8 * data, u16 len, int flags)
Igor M Liplianin7fd48282008-07-20 08:05:50 -030062{
63 int ret;
64 u8 u8buf[len];
65
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030066 unsigned int pipe = (flags == DW210X_READ_MSG) ?
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030067 usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030068 u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
Igor M Liplianin7fd48282008-07-20 08:05:50 -030069
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030070 if (flags == DW210X_WRITE_MSG)
Igor M Liplianin7fd48282008-07-20 08:05:50 -030071 memcpy(u8buf, data, len);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030072 ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
73 value, index , u8buf, len, 2000);
Igor M Liplianin7fd48282008-07-20 08:05:50 -030074
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030075 if (flags == DW210X_READ_MSG)
Igor M Liplianin7fd48282008-07-20 08:05:50 -030076 memcpy(data, u8buf, len);
77 return ret;
78}
79
80/* I2C */
Igor M Liplianin7fd48282008-07-20 08:05:50 -030081static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
82 int num)
83{
84struct dvb_usb_device *d = i2c_get_adapdata(adap);
85 int i = 0, ret = 0;
86 u8 buf6[] = {0x2c, 0x05, 0xc0, 0, 0, 0, 0};
Igor M Liplianin7fd48282008-07-20 08:05:50 -030087 u16 value;
88
89 if (!d)
90 return -ENODEV;
91 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
92 return -EAGAIN;
93
94 switch (num) {
95 case 2:
96 /* read stv0299 register */
Igor M Liplianin7fd48282008-07-20 08:05:50 -030097 value = msg[0].buf[0];/* register */
98 for (i = 0; i < msg[1].len; i++) {
99 value = value + i;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300100 ret = dw210x_op_rw(d->udev, 0xb5, value, 0,
101 buf6, 2, DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300102 msg[1].buf[i] = buf6[0];
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300103 }
104 break;
105 case 1:
106 switch (msg[0].addr) {
107 case 0x68:
108 /* write to stv0299 register */
109 buf6[0] = 0x2a;
110 buf6[1] = msg[0].buf[0];
111 buf6[2] = msg[0].buf[1];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300112 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
113 buf6, 3, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300114 break;
115 case 0x60:
116 if (msg[0].flags == 0) {
117 /* write to tuner pll */
118 buf6[0] = 0x2c;
119 buf6[1] = 5;
120 buf6[2] = 0xc0;
121 buf6[3] = msg[0].buf[0];
122 buf6[4] = msg[0].buf[1];
123 buf6[5] = msg[0].buf[2];
124 buf6[6] = msg[0].buf[3];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300125 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
126 buf6, 7, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300127 } else {
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300128 /* read from tuner */
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300129 ret = dw210x_op_rw(d->udev, 0xb5, 0, 0,
130 buf6, 1, DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300131 msg[0].buf[0] = buf6[0];
132 }
133 break;
134 case (DW2102_RC_QUERY):
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300135 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
136 buf6, 2, DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300137 msg[0].buf[0] = buf6[0];
138 msg[0].buf[1] = buf6[1];
139 break;
140 case (DW2102_VOLTAGE_CTRL):
141 buf6[0] = 0x30;
142 buf6[1] = msg[0].buf[0];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300143 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
144 buf6, 2, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300145 break;
146 }
147
148 break;
149 }
150
151 mutex_unlock(&d->i2c_mutex);
152 return num;
153}
154
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300155static int dw2102_serit_i2c_transfer(struct i2c_adapter *adap,
156 struct i2c_msg msg[], int num)
157{
158 struct dvb_usb_device *d = i2c_get_adapdata(adap);
159 int ret = 0;
160 u8 buf6[] = {0, 0, 0, 0, 0, 0, 0};
161
162 if (!d)
163 return -ENODEV;
164 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
165 return -EAGAIN;
166
167 switch (num) {
168 case 2:
169 /* read si2109 register by number */
170 buf6[0] = 0xd0;
171 buf6[1] = msg[0].len;
172 buf6[2] = msg[0].buf[0];
173 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
174 buf6, msg[0].len + 2, DW210X_WRITE_MSG);
175 /* read si2109 register */
176 ret = dw210x_op_rw(d->udev, 0xc3, 0xd0, 0,
177 buf6, msg[1].len + 2, DW210X_READ_MSG);
178 memcpy(msg[1].buf, buf6 + 2, msg[1].len);
179
180 break;
181 case 1:
182 switch (msg[0].addr) {
183 case 0x68:
184 /* write to si2109 register */
185 buf6[0] = 0xd0;
186 buf6[1] = msg[0].len;
187 memcpy(buf6 + 2, msg[0].buf, msg[0].len);
188 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0, buf6,
189 msg[0].len + 2, DW210X_WRITE_MSG);
190 break;
191 case(DW2102_RC_QUERY):
192 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
193 buf6, 2, DW210X_READ_MSG);
194 msg[0].buf[0] = buf6[0];
195 msg[0].buf[1] = buf6[1];
196 break;
197 case(DW2102_VOLTAGE_CTRL):
198 buf6[0] = 0x30;
199 buf6[1] = msg[0].buf[0];
200 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
201 buf6, 2, DW210X_WRITE_MSG);
202 break;
203 }
204 break;
205 }
206
207 mutex_unlock(&d->i2c_mutex);
208 return num;
209}
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300210static int dw2102_earda_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
211{
212 struct dvb_usb_device *d = i2c_get_adapdata(adap);
213 int ret = 0;
214
215 if (!d)
216 return -ENODEV;
217 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
218 return -EAGAIN;
219
220 switch (num) {
221 case 2: {
222 /* read */
223 /* first write first register number */
224 u8 ibuf [msg[1].len + 2], obuf[3];
225 obuf[0] = 0xd0;
226 obuf[1] = msg[0].len;
227 obuf[2] = msg[0].buf[0];
228 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
229 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
230 /* second read registers */
231 ret = dw210x_op_rw(d->udev, 0xc3, 0xd1 , 0,
232 ibuf, msg[1].len + 2, DW210X_READ_MSG);
233 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
234
235 break;
236 }
237 case 1:
238 switch (msg[0].addr) {
239 case 0x68: {
240 /* write to register */
241 u8 obuf[msg[0].len + 2];
242 obuf[0] = 0xd0;
243 obuf[1] = msg[0].len;
244 memcpy(obuf + 2, msg[0].buf, msg[0].len);
245 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
246 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
247 break;
248 }
249 case 0x61: {
250 /* write to tuner */
251 u8 obuf[msg[0].len + 2];
252 obuf[0] = 0xc2;
253 obuf[1] = msg[0].len;
254 memcpy(obuf + 2, msg[0].buf, msg[0].len);
255 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
256 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
257 break;
258 }
259 case(DW2102_RC_QUERY): {
260 u8 ibuf[2];
261 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
262 ibuf, 2, DW210X_READ_MSG);
263 memcpy(msg[0].buf, ibuf , 2);
264 break;
265 }
266 case(DW2102_VOLTAGE_CTRL): {
267 u8 obuf[2];
268 obuf[0] = 0x30;
269 obuf[1] = msg[0].buf[0];
270 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
271 obuf, 2, DW210X_WRITE_MSG);
272 break;
273 }
274 }
275
276 break;
277 }
278
279 mutex_unlock(&d->i2c_mutex);
280 return num;
281}
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300282
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300283static int dw2104_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
284{
285 struct dvb_usb_device *d = i2c_get_adapdata(adap);
286 int ret = 0;
287 int len, i;
288
289 if (!d)
290 return -ENODEV;
291 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
292 return -EAGAIN;
293
294 switch (num) {
295 case 2: {
296 /* read */
297 /* first write first register number */
298 u8 ibuf [msg[1].len + 2], obuf[3];
299 obuf[0] = 0xaa;
300 obuf[1] = msg[0].len;
301 obuf[2] = msg[0].buf[0];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300302 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
303 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300304 /* second read registers */
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300305 ret = dw210x_op_rw(d->udev, 0xc3, 0xab , 0,
306 ibuf, msg[1].len + 2, DW210X_READ_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300307 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
308
309 break;
310 }
311 case 1:
312 switch (msg[0].addr) {
313 case 0x55: {
314 if (msg[0].buf[0] == 0xf7) {
315 /* firmware */
316 /* Write in small blocks */
317 u8 obuf[19];
318 obuf[0] = 0xaa;
319 obuf[1] = 0x11;
320 obuf[2] = 0xf7;
321 len = msg[0].len - 1;
322 i = 1;
323 do {
324 memcpy(obuf + 3, msg[0].buf + i, (len > 16 ? 16 : len));
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300325 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
326 obuf, (len > 16 ? 16 : len) + 3, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300327 i += 16;
328 len -= 16;
329 } while (len > 0);
330 } else {
331 /* write to register */
332 u8 obuf[msg[0].len + 2];
333 obuf[0] = 0xaa;
334 obuf[1] = msg[0].len;
335 memcpy(obuf + 2, msg[0].buf, msg[0].len);
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300336 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
337 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300338 }
339 break;
340 }
341 case(DW2102_RC_QUERY): {
342 u8 ibuf[2];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300343 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
344 ibuf, 2, DW210X_READ_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300345 memcpy(msg[0].buf, ibuf , 2);
346 break;
347 }
348 case(DW2102_VOLTAGE_CTRL): {
349 u8 obuf[2];
350 obuf[0] = 0x30;
351 obuf[1] = msg[0].buf[0];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300352 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
353 obuf, 2, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300354 break;
355 }
356 }
357
358 break;
359 }
360
361 mutex_unlock(&d->i2c_mutex);
362 return num;
363}
364
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300365static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300366{
367 return I2C_FUNC_I2C;
368}
369
370static struct i2c_algorithm dw2102_i2c_algo = {
371 .master_xfer = dw2102_i2c_transfer,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300372 .functionality = dw210x_i2c_func,
373};
374
375static struct i2c_algorithm dw2102_serit_i2c_algo = {
376 .master_xfer = dw2102_serit_i2c_transfer,
377 .functionality = dw210x_i2c_func,
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300378};
379
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300380static struct i2c_algorithm dw2102_earda_i2c_algo = {
381 .master_xfer = dw2102_earda_i2c_transfer,
382 .functionality = dw210x_i2c_func,
383};
384
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300385static struct i2c_algorithm dw2104_i2c_algo = {
386 .master_xfer = dw2104_i2c_transfer,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300387 .functionality = dw210x_i2c_func,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300388};
389
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300390static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300391{
392 int i;
393 u8 ibuf[] = {0, 0};
394 u8 eeprom[256], eepromline[16];
395
396 for (i = 0; i < 256; i++) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300397 if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300398 err("read eeprom failed.");
399 return -1;
400 } else {
401 eepromline[i%16] = ibuf[0];
402 eeprom[i] = ibuf[0];
403 }
404 if ((i % 16) == 15) {
405 deb_xfer("%02x: ", i - 15);
406 debug_dump(eepromline, 16, deb_xfer);
407 }
408 }
409 memcpy(mac, eeprom + 8, 6);
410 return 0;
411};
412
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300413static int dw210x_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300414{
415 static u8 command_13v[1] = {0x00};
416 static u8 command_18v[1] = {0x01};
417 struct i2c_msg msg[] = {
418 {.addr = DW2102_VOLTAGE_CTRL, .flags = 0,
419 .buf = command_13v, .len = 1},
420 };
421
422 struct dvb_usb_adapter *udev_adap =
423 (struct dvb_usb_adapter *)(fe->dvb->priv);
424 if (voltage == SEC_VOLTAGE_18)
425 msg[0].buf = command_18v;
426 i2c_transfer(&udev_adap->dev->i2c_adap, msg, 1);
427 return 0;
428}
429
Igor M. Liplianind4305c62008-10-17 13:45:55 -0300430static struct stv0299_config sharp_z0194a_config = {
431 .demod_address = 0x68,
432 .inittab = sharp_z0194a_inittab,
433 .mclk = 88000000UL,
434 .invert = 1,
435 .skip_reinit = 0,
436 .lock_output = STV0299_LOCKOUTPUT_1,
437 .volt13_op0_op1 = STV0299_VOLT13_OP1,
438 .min_delay_ms = 100,
439 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
440};
441
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300442static struct cx24116_config dw2104_config = {
443 .demod_address = 0x55,
Igor M. Liplianincc8c4f32008-09-09 13:57:47 -0300444 .mpg_clk_pos_pol = 0x01,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300445};
446
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300447static struct si21xx_config serit_sp1511lhb_config = {
448 .demod_address = 0x68,
449 .min_delay_ms = 100,
450
451};
452
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300453static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
454{
455 if ((d->fe = dvb_attach(cx24116_attach, &dw2104_config,
456 &d->dev->i2c_adap)) != NULL) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300457 d->fe->ops.set_voltage = dw210x_set_voltage;
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300458 info("Attached cx24116!\n");
459 return 0;
460 }
461 return -EIO;
462}
463
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300464static struct dvb_usb_device_properties dw2102_properties;
465
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300466static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
467{
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300468 if (dw2102_properties.i2c_algo == &dw2102_serit_i2c_algo) {
469 /*dw2102_properties.adapter->tuner_attach = NULL;*/
470 d->fe = dvb_attach(si21xx_attach, &serit_sp1511lhb_config,
471 &d->dev->i2c_adap);
472 if (d->fe != NULL) {
473 d->fe->ops.set_voltage = dw210x_set_voltage;
474 info("Attached si21xx!\n");
475 return 0;
476 }
477 }
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300478 if (dw2102_properties.i2c_algo == &dw2102_earda_i2c_algo) {
479 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
480 d->fe = dvb_attach(stv0288_attach, &earda_config,
481 &d->dev->i2c_adap);
482 if (d->fe != NULL) {
483 d->fe->ops.set_voltage = dw210x_set_voltage;
484 info("Attached stv0288!\n");
485 return 0;
486 }
487 }
488
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300489 if (dw2102_properties.i2c_algo == &dw2102_i2c_algo) {
490 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
491 d->fe = dvb_attach(stv0299_attach, &sharp_z0194a_config,
492 &d->dev->i2c_adap);
493 if (d->fe != NULL) {
494 d->fe->ops.set_voltage = dw210x_set_voltage;
495 info("Attached stv0299!\n");
496 return 0;
497 }
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300498 }
499 return -EIO;
500}
501
502static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
503{
504 dvb_attach(dvb_pll_attach, adap->fe, 0x60,
505 &adap->dev->i2c_adap, DVB_PLL_OPERA1);
506 return 0;
507}
508
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300509static int dw2102_earda_tuner_attach(struct dvb_usb_adapter *adap)
510{
511 dvb_attach(stb6000_attach, adap->fe, 0x61,
512 &adap->dev->i2c_adap);
513
514 return 0;
515}
516
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300517static struct dvb_usb_rc_key dw210x_rc_keys[] = {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300518 { 0xf8, 0x0a, KEY_Q }, /*power*/
519 { 0xf8, 0x0c, KEY_M }, /*mute*/
520 { 0xf8, 0x11, KEY_1 },
521 { 0xf8, 0x12, KEY_2 },
522 { 0xf8, 0x13, KEY_3 },
523 { 0xf8, 0x14, KEY_4 },
524 { 0xf8, 0x15, KEY_5 },
525 { 0xf8, 0x16, KEY_6 },
526 { 0xf8, 0x17, KEY_7 },
527 { 0xf8, 0x18, KEY_8 },
528 { 0xf8, 0x19, KEY_9 },
529 { 0xf8, 0x10, KEY_0 },
530 { 0xf8, 0x1c, KEY_PAGEUP }, /*ch+*/
531 { 0xf8, 0x0f, KEY_PAGEDOWN }, /*ch-*/
532 { 0xf8, 0x1a, KEY_O }, /*vol+*/
533 { 0xf8, 0x0e, KEY_Z }, /*vol-*/
534 { 0xf8, 0x04, KEY_R }, /*rec*/
535 { 0xf8, 0x09, KEY_D }, /*fav*/
536 { 0xf8, 0x08, KEY_BACKSPACE }, /*rewind*/
537 { 0xf8, 0x07, KEY_A }, /*fast*/
538 { 0xf8, 0x0b, KEY_P }, /*pause*/
539 { 0xf8, 0x02, KEY_ESC }, /*cancel*/
540 { 0xf8, 0x03, KEY_G }, /*tab*/
541 { 0xf8, 0x00, KEY_UP }, /*up*/
542 { 0xf8, 0x1f, KEY_ENTER }, /*ok*/
543 { 0xf8, 0x01, KEY_DOWN }, /*down*/
544 { 0xf8, 0x05, KEY_C }, /*cap*/
545 { 0xf8, 0x06, KEY_S }, /*stop*/
546 { 0xf8, 0x40, KEY_F }, /*full*/
547 { 0xf8, 0x1e, KEY_W }, /*tvmode*/
548 { 0xf8, 0x1b, KEY_B }, /*recall*/
549
550};
551
552
553
554static int dw2102_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
555{
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300556 struct dw210x_state *st = d->priv;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300557 u8 key[2];
Igor M. Liplianin8a8dad72009-06-13 08:10:24 -0300558 struct i2c_msg msg = {
559 .addr = DW2102_RC_QUERY,
560 .flags = I2C_M_RD,
561 .buf = key,
562 .len = 2
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300563 };
564 int i;
565
566 *state = REMOTE_NO_KEY_PRESSED;
Igor M. Liplianin8a8dad72009-06-13 08:10:24 -0300567 if (dw2102_i2c_transfer(&d->i2c_adap, &msg, 1) == 1) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300568 for (i = 0; i < ARRAY_SIZE(dw210x_rc_keys); i++) {
Igor M. Liplianin8a8dad72009-06-13 08:10:24 -0300569 if (dw210x_rc_keys[i].data == msg.buf[0]) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300570 *state = REMOTE_KEY_PRESSED;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300571 *event = dw210x_rc_keys[i].event;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300572 st->last_key_pressed =
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300573 dw210x_rc_keys[i].event;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300574 break;
575 }
Igor M. Liplianin8a8dad72009-06-13 08:10:24 -0300576
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300577 st->last_key_pressed = 0;
578 }
Igor M. Liplianin8a8dad72009-06-13 08:10:24 -0300579
580 if ((*state) == REMOTE_KEY_PRESSED)
581 deb_rc("%s: found rc key: %x, %x, event: %x\n",
582 __func__, key[0], key[1], (*event));
583 else if (key[0] != 0xff)
584 deb_rc("%s: unknown rc key: %x, %x\n",
585 __func__, key[0], key[1]);
586
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300587 }
Igor M. Liplianin8a8dad72009-06-13 08:10:24 -0300588
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300589 return 0;
590}
591
592static struct usb_device_id dw2102_table[] = {
593 {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
594 {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300595 {USB_DEVICE(USB_VID_CYPRESS, 0x2104)},
596 {USB_DEVICE(0x9022, 0xd650)},
Igor M. Liplianin4cc0edf2008-11-05 22:12:56 -0300597 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_CINERGY_S)},
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300598 { }
599};
600
601MODULE_DEVICE_TABLE(usb, dw2102_table);
602
603static int dw2102_load_firmware(struct usb_device *dev,
604 const struct firmware *frmwr)
605{
606 u8 *b, *p;
607 int ret = 0, i;
608 u8 reset;
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300609 u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300610 const struct firmware *fw;
611 const char *filename = "dvb-usb-dw2101.fw";
612 switch (dev->descriptor.idProduct) {
613 case 0x2101:
614 ret = request_firmware(&fw, filename, &dev->dev);
615 if (ret != 0) {
616 err("did not find the firmware file. (%s) "
617 "Please see linux/Documentation/dvb/ for more details "
618 "on firmware-problems.", filename);
619 return ret;
620 }
621 break;
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300622 default:
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300623 fw = frmwr;
624 break;
625 }
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300626 info("start downloading DW210X firmware");
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300627 p = kmalloc(fw->size, GFP_KERNEL);
628 reset = 1;
629 /*stop the CPU*/
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300630 dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
631 dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300632
633 if (p != NULL) {
634 memcpy(p, fw->data, fw->size);
635 for (i = 0; i < fw->size; i += 0x40) {
636 b = (u8 *) p + i;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300637 if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
638 DW210X_WRITE_MSG) != 0x40) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300639 err("error while transferring firmware");
640 ret = -EINVAL;
641 break;
642 }
643 }
644 /* restart the CPU */
645 reset = 0;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300646 if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
647 DW210X_WRITE_MSG) != 1) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300648 err("could not restart the USB controller CPU.");
649 ret = -EINVAL;
650 }
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300651 if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
652 DW210X_WRITE_MSG) != 1) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300653 err("could not restart the USB controller CPU.");
654 ret = -EINVAL;
655 }
656 /* init registers */
657 switch (dev->descriptor.idProduct) {
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300658 case USB_PID_DW2104:
659 case 0xd650:
660 reset = 1;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300661 dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
662 DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300663 reset = 0;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300664 dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
665 DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300666 break;
Igor M. Liplianin4cc0edf2008-11-05 22:12:56 -0300667 case USB_PID_CINERGY_S:
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300668 case USB_PID_DW2102:
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300669 dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
670 DW210X_WRITE_MSG);
671 dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
672 DW210X_READ_MSG);
673 /* check STV0299 frontend */
674 dw210x_op_rw(dev, 0xb5, 0, 0, &reset16[0], 2,
675 DW210X_READ_MSG);
Igor M. Liplianinea023df2008-12-04 12:49:23 -0300676 if ((reset16[0] == 0xa1) || (reset16[0] == 0x80)) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300677 dw2102_properties.i2c_algo = &dw2102_i2c_algo;
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300678 dw2102_properties.adapter->tuner_attach = &dw2102_tuner_attach;
679 break;
680 } else {
681 /* check STV0288 frontend */
682 reset16[0] = 0xd0;
683 reset16[1] = 1;
684 reset16[2] = 0;
685 dw210x_op_rw(dev, 0xc2, 0, 0, &reset16[0], 3,
686 DW210X_WRITE_MSG);
687 dw210x_op_rw(dev, 0xc3, 0xd1, 0, &reset16[0], 3,
688 DW210X_READ_MSG);
689 if (reset16[2] == 0x11) {
690 dw2102_properties.i2c_algo = &dw2102_earda_i2c_algo;
691 dw2102_properties.adapter->tuner_attach = &dw2102_earda_tuner_attach;
692 break;
693 }
694 }
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300695 case 0x2101:
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300696 dw210x_op_rw(dev, 0xbc, 0x0030, 0, &reset16[0], 2,
697 DW210X_READ_MSG);
698 dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
699 DW210X_READ_MSG);
700 dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
701 DW210X_READ_MSG);
702 dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
703 DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300704 break;
705 }
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300706 msleep(100);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300707 kfree(p);
708 }
709 return ret;
710}
711
712static struct dvb_usb_device_properties dw2102_properties = {
713 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
714 .usb_ctrl = DEVICE_SPECIFIC,
715 .firmware = "dvb-usb-dw2102.fw",
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300716 .size_of_priv = sizeof(struct dw210x_state),
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300717 .no_reconnect = 1,
718
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300719 .i2c_algo = &dw2102_serit_i2c_algo,
720 .rc_key_map = dw210x_rc_keys,
721 .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300722 .rc_interval = 150,
723 .rc_query = dw2102_rc_query,
724
725 .generic_bulk_ctrl_endpoint = 0x81,
726 /* parameter for the MPEG2-data transfer */
727 .num_adapters = 1,
728 .download_firmware = dw2102_load_firmware,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300729 .read_mac_address = dw210x_read_mac_address,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300730 .adapter = {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300731 {
732 .frontend_attach = dw2102_frontend_attach,
733 .streaming_ctrl = NULL,
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300734 .tuner_attach = NULL,
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300735 .stream = {
736 .type = USB_BULK,
737 .count = 8,
738 .endpoint = 0x82,
739 .u = {
740 .bulk = {
741 .buffersize = 4096,
742 }
743 }
744 },
745 }
746 },
Igor M. Liplianin4cc0edf2008-11-05 22:12:56 -0300747 .num_device_descs = 3,
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300748 .devices = {
749 {"DVBWorld DVB-S 2102 USB2.0",
750 {&dw2102_table[0], NULL},
751 {NULL},
752 },
753 {"DVBWorld DVB-S 2101 USB2.0",
754 {&dw2102_table[1], NULL},
755 {NULL},
756 },
Igor M. Liplianin4cc0edf2008-11-05 22:12:56 -0300757 {"TerraTec Cinergy S USB",
758 {&dw2102_table[4], NULL},
759 {NULL},
760 },
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300761 }
762};
763
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300764static struct dvb_usb_device_properties dw2104_properties = {
765 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
766 .usb_ctrl = DEVICE_SPECIFIC,
767 .firmware = "dvb-usb-dw2104.fw",
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300768 .size_of_priv = sizeof(struct dw210x_state),
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300769 .no_reconnect = 1,
770
771 .i2c_algo = &dw2104_i2c_algo,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300772 .rc_key_map = dw210x_rc_keys,
773 .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300774 .rc_interval = 150,
775 .rc_query = dw2102_rc_query,
776
777 .generic_bulk_ctrl_endpoint = 0x81,
778 /* parameter for the MPEG2-data transfer */
779 .num_adapters = 1,
780 .download_firmware = dw2102_load_firmware,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300781 .read_mac_address = dw210x_read_mac_address,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300782 .adapter = {
783 {
784 .frontend_attach = dw2104_frontend_attach,
785 .streaming_ctrl = NULL,
786 /*.tuner_attach = dw2104_tuner_attach,*/
787 .stream = {
788 .type = USB_BULK,
789 .count = 8,
790 .endpoint = 0x82,
791 .u = {
792 .bulk = {
793 .buffersize = 4096,
794 }
795 }
796 },
797 }
798 },
799 .num_device_descs = 2,
800 .devices = {
801 { "DVBWorld DW2104 USB2.0",
802 {&dw2102_table[2], NULL},
803 {NULL},
804 },
805 { "TeVii S650 USB2.0",
806 {&dw2102_table[3], NULL},
807 {NULL},
808 },
809 }
810};
811
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300812static int dw2102_probe(struct usb_interface *intf,
813 const struct usb_device_id *id)
814{
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300815 if (0 == dvb_usb_device_init(intf, &dw2102_properties,
816 THIS_MODULE, NULL, adapter_nr) ||
817 0 == dvb_usb_device_init(intf, &dw2104_properties,
818 THIS_MODULE, NULL, adapter_nr)) {
819 return 0;
820 }
821 return -ENODEV;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300822}
823
824static struct usb_driver dw2102_driver = {
825 .name = "dw2102",
826 .probe = dw2102_probe,
827 .disconnect = dvb_usb_device_exit,
828 .id_table = dw2102_table,
829};
830
831static int __init dw2102_module_init(void)
832{
833 int ret = usb_register(&dw2102_driver);
834 if (ret)
835 err("usb_register failed. Error number %d", ret);
836
837 return ret;
838}
839
840static void __exit dw2102_module_exit(void)
841{
842 usb_deregister(&dw2102_driver);
843}
844
845module_init(dw2102_module_init);
846module_exit(dw2102_module_exit);
847
848MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300849MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104 USB2.0 device");
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300850MODULE_VERSION("0.1");
851MODULE_LICENSE("GPL");