blob: 6286fbbe7fb5bd1178065d5618c817ab2da5a45d [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*/
12#include <linux/version.h>
13#include "dw2102.h"
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030014#include "si21xx.h"
Igor M Liplianin7fd48282008-07-20 08:05:50 -030015#include "stv0299.h"
16#include "z0194a.h"
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -030017#include "stv0288.h"
18#include "stb6000.h"
19#include "eds1547.h"
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030020#include "cx24116.h"
Igor M Liplianin7fd48282008-07-20 08:05:50 -030021
22#ifndef USB_PID_DW2102
23#define USB_PID_DW2102 0x2102
24#endif
25
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030026#ifndef USB_PID_DW2104
27#define USB_PID_DW2104 0x2104
28#endif
29
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030030#define DW210X_READ_MSG 0
31#define DW210X_WRITE_MSG 1
Igor M Liplianin7fd48282008-07-20 08:05:50 -030032
33#define REG_1F_SYMBOLRATE_BYTE0 0x1f
34#define REG_20_SYMBOLRATE_BYTE1 0x20
35#define REG_21_SYMBOLRATE_BYTE2 0x21
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030036/* on my own*/
Igor M Liplianin7fd48282008-07-20 08:05:50 -030037#define DW2102_VOLTAGE_CTRL (0x1800)
38#define DW2102_RC_QUERY (0x1a00)
39
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030040struct dw210x_state {
Igor M Liplianin7fd48282008-07-20 08:05:50 -030041 u32 last_key_pressed;
42};
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030043struct dw210x_rc_keys {
Igor M Liplianin7fd48282008-07-20 08:05:50 -030044 u32 keycode;
45 u32 event;
46};
47
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030048/* debug */
49static int dvb_usb_dw2102_debug;
50module_param_named(debug, dvb_usb_dw2102_debug, int, 0644);
51MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer (or-able))." DVB_USB_DEBUG_STATUS);
52
Igor M Liplianin7fd48282008-07-20 08:05:50 -030053DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
54
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030055static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030056 u16 index, u8 * data, u16 len, int flags)
Igor M Liplianin7fd48282008-07-20 08:05:50 -030057{
58 int ret;
59 u8 u8buf[len];
60
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030061 unsigned int pipe = (flags == DW210X_READ_MSG) ?
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030062 usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030063 u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
Igor M Liplianin7fd48282008-07-20 08:05:50 -030064
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030065 if (flags == DW210X_WRITE_MSG)
Igor M Liplianin7fd48282008-07-20 08:05:50 -030066 memcpy(u8buf, data, len);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -030067 ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
68 value, index , u8buf, len, 2000);
Igor M Liplianin7fd48282008-07-20 08:05:50 -030069
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030070 if (flags == DW210X_READ_MSG)
Igor M Liplianin7fd48282008-07-20 08:05:50 -030071 memcpy(data, u8buf, len);
72 return ret;
73}
74
75/* I2C */
Igor M Liplianin7fd48282008-07-20 08:05:50 -030076static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
77 int num)
78{
79struct dvb_usb_device *d = i2c_get_adapdata(adap);
80 int i = 0, ret = 0;
81 u8 buf6[] = {0x2c, 0x05, 0xc0, 0, 0, 0, 0};
Igor M Liplianin7fd48282008-07-20 08:05:50 -030082 u16 value;
83
84 if (!d)
85 return -ENODEV;
86 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
87 return -EAGAIN;
88
89 switch (num) {
90 case 2:
91 /* read stv0299 register */
Igor M Liplianin7fd48282008-07-20 08:05:50 -030092 value = msg[0].buf[0];/* register */
93 for (i = 0; i < msg[1].len; i++) {
94 value = value + i;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -030095 ret = dw210x_op_rw(d->udev, 0xb5, value, 0,
96 buf6, 2, DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -030097 msg[1].buf[i] = buf6[0];
Igor M Liplianin7fd48282008-07-20 08:05:50 -030098 }
99 break;
100 case 1:
101 switch (msg[0].addr) {
102 case 0x68:
103 /* write to stv0299 register */
104 buf6[0] = 0x2a;
105 buf6[1] = msg[0].buf[0];
106 buf6[2] = msg[0].buf[1];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300107 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
108 buf6, 3, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300109 break;
110 case 0x60:
111 if (msg[0].flags == 0) {
112 /* write to tuner pll */
113 buf6[0] = 0x2c;
114 buf6[1] = 5;
115 buf6[2] = 0xc0;
116 buf6[3] = msg[0].buf[0];
117 buf6[4] = msg[0].buf[1];
118 buf6[5] = msg[0].buf[2];
119 buf6[6] = msg[0].buf[3];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300120 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
121 buf6, 7, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300122 } else {
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300123 /* read from tuner */
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300124 ret = dw210x_op_rw(d->udev, 0xb5, 0, 0,
125 buf6, 1, DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300126 msg[0].buf[0] = buf6[0];
127 }
128 break;
129 case (DW2102_RC_QUERY):
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300130 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
131 buf6, 2, DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300132 msg[0].buf[0] = buf6[0];
133 msg[0].buf[1] = buf6[1];
134 break;
135 case (DW2102_VOLTAGE_CTRL):
136 buf6[0] = 0x30;
137 buf6[1] = msg[0].buf[0];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300138 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
139 buf6, 2, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300140 break;
141 }
142
143 break;
144 }
145
146 mutex_unlock(&d->i2c_mutex);
147 return num;
148}
149
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300150static int dw2102_serit_i2c_transfer(struct i2c_adapter *adap,
151 struct i2c_msg msg[], int num)
152{
153 struct dvb_usb_device *d = i2c_get_adapdata(adap);
154 int ret = 0;
155 u8 buf6[] = {0, 0, 0, 0, 0, 0, 0};
156
157 if (!d)
158 return -ENODEV;
159 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
160 return -EAGAIN;
161
162 switch (num) {
163 case 2:
164 /* read si2109 register by number */
165 buf6[0] = 0xd0;
166 buf6[1] = msg[0].len;
167 buf6[2] = msg[0].buf[0];
168 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
169 buf6, msg[0].len + 2, DW210X_WRITE_MSG);
170 /* read si2109 register */
171 ret = dw210x_op_rw(d->udev, 0xc3, 0xd0, 0,
172 buf6, msg[1].len + 2, DW210X_READ_MSG);
173 memcpy(msg[1].buf, buf6 + 2, msg[1].len);
174
175 break;
176 case 1:
177 switch (msg[0].addr) {
178 case 0x68:
179 /* write to si2109 register */
180 buf6[0] = 0xd0;
181 buf6[1] = msg[0].len;
182 memcpy(buf6 + 2, msg[0].buf, msg[0].len);
183 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0, buf6,
184 msg[0].len + 2, DW210X_WRITE_MSG);
185 break;
186 case(DW2102_RC_QUERY):
187 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
188 buf6, 2, DW210X_READ_MSG);
189 msg[0].buf[0] = buf6[0];
190 msg[0].buf[1] = buf6[1];
191 break;
192 case(DW2102_VOLTAGE_CTRL):
193 buf6[0] = 0x30;
194 buf6[1] = msg[0].buf[0];
195 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
196 buf6, 2, DW210X_WRITE_MSG);
197 break;
198 }
199 break;
200 }
201
202 mutex_unlock(&d->i2c_mutex);
203 return num;
204}
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300205static int dw2102_earda_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
206{
207 struct dvb_usb_device *d = i2c_get_adapdata(adap);
208 int ret = 0;
209
210 if (!d)
211 return -ENODEV;
212 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
213 return -EAGAIN;
214
215 switch (num) {
216 case 2: {
217 /* read */
218 /* first write first register number */
219 u8 ibuf [msg[1].len + 2], obuf[3];
220 obuf[0] = 0xd0;
221 obuf[1] = msg[0].len;
222 obuf[2] = msg[0].buf[0];
223 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
224 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
225 /* second read registers */
226 ret = dw210x_op_rw(d->udev, 0xc3, 0xd1 , 0,
227 ibuf, msg[1].len + 2, DW210X_READ_MSG);
228 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
229
230 break;
231 }
232 case 1:
233 switch (msg[0].addr) {
234 case 0x68: {
235 /* write to register */
236 u8 obuf[msg[0].len + 2];
237 obuf[0] = 0xd0;
238 obuf[1] = msg[0].len;
239 memcpy(obuf + 2, msg[0].buf, msg[0].len);
240 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
241 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
242 break;
243 }
244 case 0x61: {
245 /* write to tuner */
246 u8 obuf[msg[0].len + 2];
247 obuf[0] = 0xc2;
248 obuf[1] = msg[0].len;
249 memcpy(obuf + 2, msg[0].buf, msg[0].len);
250 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
251 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
252 break;
253 }
254 case(DW2102_RC_QUERY): {
255 u8 ibuf[2];
256 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
257 ibuf, 2, DW210X_READ_MSG);
258 memcpy(msg[0].buf, ibuf , 2);
259 break;
260 }
261 case(DW2102_VOLTAGE_CTRL): {
262 u8 obuf[2];
263 obuf[0] = 0x30;
264 obuf[1] = msg[0].buf[0];
265 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
266 obuf, 2, DW210X_WRITE_MSG);
267 break;
268 }
269 }
270
271 break;
272 }
273
274 mutex_unlock(&d->i2c_mutex);
275 return num;
276}
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300277
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300278static int dw2104_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
279{
280 struct dvb_usb_device *d = i2c_get_adapdata(adap);
281 int ret = 0;
282 int len, i;
283
284 if (!d)
285 return -ENODEV;
286 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
287 return -EAGAIN;
288
289 switch (num) {
290 case 2: {
291 /* read */
292 /* first write first register number */
293 u8 ibuf [msg[1].len + 2], obuf[3];
294 obuf[0] = 0xaa;
295 obuf[1] = msg[0].len;
296 obuf[2] = msg[0].buf[0];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300297 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
298 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300299 /* second read registers */
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300300 ret = dw210x_op_rw(d->udev, 0xc3, 0xab , 0,
301 ibuf, msg[1].len + 2, DW210X_READ_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300302 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
303
304 break;
305 }
306 case 1:
307 switch (msg[0].addr) {
308 case 0x55: {
309 if (msg[0].buf[0] == 0xf7) {
310 /* firmware */
311 /* Write in small blocks */
312 u8 obuf[19];
313 obuf[0] = 0xaa;
314 obuf[1] = 0x11;
315 obuf[2] = 0xf7;
316 len = msg[0].len - 1;
317 i = 1;
318 do {
319 memcpy(obuf + 3, msg[0].buf + i, (len > 16 ? 16 : len));
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300320 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
321 obuf, (len > 16 ? 16 : len) + 3, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300322 i += 16;
323 len -= 16;
324 } while (len > 0);
325 } else {
326 /* write to register */
327 u8 obuf[msg[0].len + 2];
328 obuf[0] = 0xaa;
329 obuf[1] = msg[0].len;
330 memcpy(obuf + 2, msg[0].buf, msg[0].len);
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300331 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
332 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300333 }
334 break;
335 }
336 case(DW2102_RC_QUERY): {
337 u8 ibuf[2];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300338 ret = dw210x_op_rw(d->udev, 0xb8, 0, 0,
339 ibuf, 2, DW210X_READ_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300340 memcpy(msg[0].buf, ibuf , 2);
341 break;
342 }
343 case(DW2102_VOLTAGE_CTRL): {
344 u8 obuf[2];
345 obuf[0] = 0x30;
346 obuf[1] = msg[0].buf[0];
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300347 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
348 obuf, 2, DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300349 break;
350 }
351 }
352
353 break;
354 }
355
356 mutex_unlock(&d->i2c_mutex);
357 return num;
358}
359
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300360static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300361{
362 return I2C_FUNC_I2C;
363}
364
365static struct i2c_algorithm dw2102_i2c_algo = {
366 .master_xfer = dw2102_i2c_transfer,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300367 .functionality = dw210x_i2c_func,
368};
369
370static struct i2c_algorithm dw2102_serit_i2c_algo = {
371 .master_xfer = dw2102_serit_i2c_transfer,
372 .functionality = dw210x_i2c_func,
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300373};
374
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300375static struct i2c_algorithm dw2102_earda_i2c_algo = {
376 .master_xfer = dw2102_earda_i2c_transfer,
377 .functionality = dw210x_i2c_func,
378};
379
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300380static struct i2c_algorithm dw2104_i2c_algo = {
381 .master_xfer = dw2104_i2c_transfer,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300382 .functionality = dw210x_i2c_func,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300383};
384
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300385static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300386{
387 int i;
388 u8 ibuf[] = {0, 0};
389 u8 eeprom[256], eepromline[16];
390
391 for (i = 0; i < 256; i++) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300392 if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300393 err("read eeprom failed.");
394 return -1;
395 } else {
396 eepromline[i%16] = ibuf[0];
397 eeprom[i] = ibuf[0];
398 }
399 if ((i % 16) == 15) {
400 deb_xfer("%02x: ", i - 15);
401 debug_dump(eepromline, 16, deb_xfer);
402 }
403 }
404 memcpy(mac, eeprom + 8, 6);
405 return 0;
406};
407
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300408static int dw210x_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300409{
410 static u8 command_13v[1] = {0x00};
411 static u8 command_18v[1] = {0x01};
412 struct i2c_msg msg[] = {
413 {.addr = DW2102_VOLTAGE_CTRL, .flags = 0,
414 .buf = command_13v, .len = 1},
415 };
416
417 struct dvb_usb_adapter *udev_adap =
418 (struct dvb_usb_adapter *)(fe->dvb->priv);
419 if (voltage == SEC_VOLTAGE_18)
420 msg[0].buf = command_18v;
421 i2c_transfer(&udev_adap->dev->i2c_adap, msg, 1);
422 return 0;
423}
424
Igor M. Liplianind4305c62008-10-17 13:45:55 -0300425static struct stv0299_config sharp_z0194a_config = {
426 .demod_address = 0x68,
427 .inittab = sharp_z0194a_inittab,
428 .mclk = 88000000UL,
429 .invert = 1,
430 .skip_reinit = 0,
431 .lock_output = STV0299_LOCKOUTPUT_1,
432 .volt13_op0_op1 = STV0299_VOLT13_OP1,
433 .min_delay_ms = 100,
434 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
435};
436
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300437static struct cx24116_config dw2104_config = {
438 .demod_address = 0x55,
Igor M. Liplianincc8c4f32008-09-09 13:57:47 -0300439 .mpg_clk_pos_pol = 0x01,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300440};
441
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300442static struct si21xx_config serit_sp1511lhb_config = {
443 .demod_address = 0x68,
444 .min_delay_ms = 100,
445
446};
447
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300448static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
449{
450 if ((d->fe = dvb_attach(cx24116_attach, &dw2104_config,
451 &d->dev->i2c_adap)) != NULL) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300452 d->fe->ops.set_voltage = dw210x_set_voltage;
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300453 info("Attached cx24116!\n");
454 return 0;
455 }
456 return -EIO;
457}
458
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300459static struct dvb_usb_device_properties dw2102_properties;
460
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300461static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
462{
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300463 if (dw2102_properties.i2c_algo == &dw2102_serit_i2c_algo) {
464 /*dw2102_properties.adapter->tuner_attach = NULL;*/
465 d->fe = dvb_attach(si21xx_attach, &serit_sp1511lhb_config,
466 &d->dev->i2c_adap);
467 if (d->fe != NULL) {
468 d->fe->ops.set_voltage = dw210x_set_voltage;
469 info("Attached si21xx!\n");
470 return 0;
471 }
472 }
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300473 if (dw2102_properties.i2c_algo == &dw2102_earda_i2c_algo) {
474 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
475 d->fe = dvb_attach(stv0288_attach, &earda_config,
476 &d->dev->i2c_adap);
477 if (d->fe != NULL) {
478 d->fe->ops.set_voltage = dw210x_set_voltage;
479 info("Attached stv0288!\n");
480 return 0;
481 }
482 }
483
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300484 if (dw2102_properties.i2c_algo == &dw2102_i2c_algo) {
485 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
486 d->fe = dvb_attach(stv0299_attach, &sharp_z0194a_config,
487 &d->dev->i2c_adap);
488 if (d->fe != NULL) {
489 d->fe->ops.set_voltage = dw210x_set_voltage;
490 info("Attached stv0299!\n");
491 return 0;
492 }
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300493 }
494 return -EIO;
495}
496
497static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
498{
499 dvb_attach(dvb_pll_attach, adap->fe, 0x60,
500 &adap->dev->i2c_adap, DVB_PLL_OPERA1);
501 return 0;
502}
503
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300504static int dw2102_earda_tuner_attach(struct dvb_usb_adapter *adap)
505{
506 dvb_attach(stb6000_attach, adap->fe, 0x61,
507 &adap->dev->i2c_adap);
508
509 return 0;
510}
511
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300512static struct dvb_usb_rc_key dw210x_rc_keys[] = {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300513 { 0xf8, 0x0a, KEY_Q }, /*power*/
514 { 0xf8, 0x0c, KEY_M }, /*mute*/
515 { 0xf8, 0x11, KEY_1 },
516 { 0xf8, 0x12, KEY_2 },
517 { 0xf8, 0x13, KEY_3 },
518 { 0xf8, 0x14, KEY_4 },
519 { 0xf8, 0x15, KEY_5 },
520 { 0xf8, 0x16, KEY_6 },
521 { 0xf8, 0x17, KEY_7 },
522 { 0xf8, 0x18, KEY_8 },
523 { 0xf8, 0x19, KEY_9 },
524 { 0xf8, 0x10, KEY_0 },
525 { 0xf8, 0x1c, KEY_PAGEUP }, /*ch+*/
526 { 0xf8, 0x0f, KEY_PAGEDOWN }, /*ch-*/
527 { 0xf8, 0x1a, KEY_O }, /*vol+*/
528 { 0xf8, 0x0e, KEY_Z }, /*vol-*/
529 { 0xf8, 0x04, KEY_R }, /*rec*/
530 { 0xf8, 0x09, KEY_D }, /*fav*/
531 { 0xf8, 0x08, KEY_BACKSPACE }, /*rewind*/
532 { 0xf8, 0x07, KEY_A }, /*fast*/
533 { 0xf8, 0x0b, KEY_P }, /*pause*/
534 { 0xf8, 0x02, KEY_ESC }, /*cancel*/
535 { 0xf8, 0x03, KEY_G }, /*tab*/
536 { 0xf8, 0x00, KEY_UP }, /*up*/
537 { 0xf8, 0x1f, KEY_ENTER }, /*ok*/
538 { 0xf8, 0x01, KEY_DOWN }, /*down*/
539 { 0xf8, 0x05, KEY_C }, /*cap*/
540 { 0xf8, 0x06, KEY_S }, /*stop*/
541 { 0xf8, 0x40, KEY_F }, /*full*/
542 { 0xf8, 0x1e, KEY_W }, /*tvmode*/
543 { 0xf8, 0x1b, KEY_B }, /*recall*/
544
545};
546
547
548
549static int dw2102_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
550{
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300551 struct dw210x_state *st = d->priv;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300552 u8 key[2];
553 struct i2c_msg msg[] = {
554 {.addr = DW2102_RC_QUERY, .flags = I2C_M_RD, .buf = key,
555 .len = 2},
556 };
557 int i;
558
559 *state = REMOTE_NO_KEY_PRESSED;
560 if (dw2102_i2c_transfer(&d->i2c_adap, msg, 1) == 1) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300561 for (i = 0; i < ARRAY_SIZE(dw210x_rc_keys); i++) {
562 if (dw210x_rc_keys[i].data == msg[0].buf[0]) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300563 *state = REMOTE_KEY_PRESSED;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300564 *event = dw210x_rc_keys[i].event;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300565 st->last_key_pressed =
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300566 dw210x_rc_keys[i].event;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300567 break;
568 }
569 st->last_key_pressed = 0;
570 }
571 }
572 /* info("key: %x %x\n",key[0],key[1]); */
573 return 0;
574}
575
576static struct usb_device_id dw2102_table[] = {
577 {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
578 {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300579 {USB_DEVICE(USB_VID_CYPRESS, 0x2104)},
580 {USB_DEVICE(0x9022, 0xd650)},
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300581 { }
582};
583
584MODULE_DEVICE_TABLE(usb, dw2102_table);
585
586static int dw2102_load_firmware(struct usb_device *dev,
587 const struct firmware *frmwr)
588{
589 u8 *b, *p;
590 int ret = 0, i;
591 u8 reset;
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300592 u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300593 const struct firmware *fw;
594 const char *filename = "dvb-usb-dw2101.fw";
595 switch (dev->descriptor.idProduct) {
596 case 0x2101:
597 ret = request_firmware(&fw, filename, &dev->dev);
598 if (ret != 0) {
599 err("did not find the firmware file. (%s) "
600 "Please see linux/Documentation/dvb/ for more details "
601 "on firmware-problems.", filename);
602 return ret;
603 }
604 break;
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300605 default:
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300606 fw = frmwr;
607 break;
608 }
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300609 info("start downloading DW210X firmware");
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300610 p = kmalloc(fw->size, GFP_KERNEL);
611 reset = 1;
612 /*stop the CPU*/
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300613 dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
614 dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300615
616 if (p != NULL) {
617 memcpy(p, fw->data, fw->size);
618 for (i = 0; i < fw->size; i += 0x40) {
619 b = (u8 *) p + i;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300620 if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
621 DW210X_WRITE_MSG) != 0x40) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300622 err("error while transferring firmware");
623 ret = -EINVAL;
624 break;
625 }
626 }
627 /* restart the CPU */
628 reset = 0;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300629 if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
630 DW210X_WRITE_MSG) != 1) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300631 err("could not restart the USB controller CPU.");
632 ret = -EINVAL;
633 }
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300634 if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
635 DW210X_WRITE_MSG) != 1) {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300636 err("could not restart the USB controller CPU.");
637 ret = -EINVAL;
638 }
639 /* init registers */
640 switch (dev->descriptor.idProduct) {
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300641 case USB_PID_DW2104:
642 case 0xd650:
643 reset = 1;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300644 dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
645 DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300646 reset = 0;
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300647 dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
648 DW210X_WRITE_MSG);
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300649 break;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300650 case USB_PID_DW2102:
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300651 dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
652 DW210X_WRITE_MSG);
653 dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
654 DW210X_READ_MSG);
655 /* check STV0299 frontend */
656 dw210x_op_rw(dev, 0xb5, 0, 0, &reset16[0], 2,
657 DW210X_READ_MSG);
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300658 if (reset16[0] == 0xa1) {
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300659 dw2102_properties.i2c_algo = &dw2102_i2c_algo;
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300660 dw2102_properties.adapter->tuner_attach = &dw2102_tuner_attach;
661 break;
662 } else {
663 /* check STV0288 frontend */
664 reset16[0] = 0xd0;
665 reset16[1] = 1;
666 reset16[2] = 0;
667 dw210x_op_rw(dev, 0xc2, 0, 0, &reset16[0], 3,
668 DW210X_WRITE_MSG);
669 dw210x_op_rw(dev, 0xc3, 0xd1, 0, &reset16[0], 3,
670 DW210X_READ_MSG);
671 if (reset16[2] == 0x11) {
672 dw2102_properties.i2c_algo = &dw2102_earda_i2c_algo;
673 dw2102_properties.adapter->tuner_attach = &dw2102_earda_tuner_attach;
674 break;
675 }
676 }
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300677 case 0x2101:
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300678 dw210x_op_rw(dev, 0xbc, 0x0030, 0, &reset16[0], 2,
679 DW210X_READ_MSG);
680 dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
681 DW210X_READ_MSG);
682 dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
683 DW210X_READ_MSG);
684 dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
685 DW210X_READ_MSG);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300686 break;
687 }
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300688 msleep(100);
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300689 kfree(p);
690 }
691 return ret;
692}
693
694static struct dvb_usb_device_properties dw2102_properties = {
695 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
696 .usb_ctrl = DEVICE_SPECIFIC,
697 .firmware = "dvb-usb-dw2102.fw",
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300698 .size_of_priv = sizeof(struct dw210x_state),
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300699 .no_reconnect = 1,
700
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300701 .i2c_algo = &dw2102_serit_i2c_algo,
702 .rc_key_map = dw210x_rc_keys,
703 .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300704 .rc_interval = 150,
705 .rc_query = dw2102_rc_query,
706
707 .generic_bulk_ctrl_endpoint = 0x81,
708 /* parameter for the MPEG2-data transfer */
709 .num_adapters = 1,
710 .download_firmware = dw2102_load_firmware,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300711 .read_mac_address = dw210x_read_mac_address,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300712 .adapter = {
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300713 {
714 .frontend_attach = dw2102_frontend_attach,
715 .streaming_ctrl = NULL,
Igor M. Liplianin8a4949b2008-10-05 09:11:21 -0300716 .tuner_attach = NULL,
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300717 .stream = {
718 .type = USB_BULK,
719 .count = 8,
720 .endpoint = 0x82,
721 .u = {
722 .bulk = {
723 .buffersize = 4096,
724 }
725 }
726 },
727 }
728 },
729 .num_device_descs = 2,
730 .devices = {
731 {"DVBWorld DVB-S 2102 USB2.0",
732 {&dw2102_table[0], NULL},
733 {NULL},
734 },
735 {"DVBWorld DVB-S 2101 USB2.0",
736 {&dw2102_table[1], NULL},
737 {NULL},
738 },
739 }
740};
741
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300742static struct dvb_usb_device_properties dw2104_properties = {
743 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
744 .usb_ctrl = DEVICE_SPECIFIC,
745 .firmware = "dvb-usb-dw2104.fw",
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300746 .size_of_priv = sizeof(struct dw210x_state),
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300747 .no_reconnect = 1,
748
749 .i2c_algo = &dw2104_i2c_algo,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300750 .rc_key_map = dw210x_rc_keys,
751 .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300752 .rc_interval = 150,
753 .rc_query = dw2102_rc_query,
754
755 .generic_bulk_ctrl_endpoint = 0x81,
756 /* parameter for the MPEG2-data transfer */
757 .num_adapters = 1,
758 .download_firmware = dw2102_load_firmware,
Igor M. Liplianin21b007b2008-09-17 19:19:19 -0300759 .read_mac_address = dw210x_read_mac_address,
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300760 .adapter = {
761 {
762 .frontend_attach = dw2104_frontend_attach,
763 .streaming_ctrl = NULL,
764 /*.tuner_attach = dw2104_tuner_attach,*/
765 .stream = {
766 .type = USB_BULK,
767 .count = 8,
768 .endpoint = 0x82,
769 .u = {
770 .bulk = {
771 .buffersize = 4096,
772 }
773 }
774 },
775 }
776 },
777 .num_device_descs = 2,
778 .devices = {
779 { "DVBWorld DW2104 USB2.0",
780 {&dw2102_table[2], NULL},
781 {NULL},
782 },
783 { "TeVii S650 USB2.0",
784 {&dw2102_table[3], NULL},
785 {NULL},
786 },
787 }
788};
789
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300790static int dw2102_probe(struct usb_interface *intf,
791 const struct usb_device_id *id)
792{
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300793 if (0 == dvb_usb_device_init(intf, &dw2102_properties,
794 THIS_MODULE, NULL, adapter_nr) ||
795 0 == dvb_usb_device_init(intf, &dw2104_properties,
796 THIS_MODULE, NULL, adapter_nr)) {
797 return 0;
798 }
799 return -ENODEV;
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300800}
801
802static struct usb_driver dw2102_driver = {
803 .name = "dw2102",
804 .probe = dw2102_probe,
805 .disconnect = dvb_usb_device_exit,
806 .id_table = dw2102_table,
807};
808
809static int __init dw2102_module_init(void)
810{
811 int ret = usb_register(&dw2102_driver);
812 if (ret)
813 err("usb_register failed. Error number %d", ret);
814
815 return ret;
816}
817
818static void __exit dw2102_module_exit(void)
819{
820 usb_deregister(&dw2102_driver);
821}
822
823module_init(dw2102_module_init);
824module_exit(dw2102_module_exit);
825
826MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
Igor M. Liplianinfe03d5e2008-09-08 17:16:40 -0300827MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104 USB2.0 device");
Igor M Liplianin7fd48282008-07-20 08:05:50 -0300828MODULE_VERSION("0.1");
829MODULE_LICENSE("GPL");