Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / media / dvb / dvb-usb / dw2102.c
1 /* DVB USB framework compliant Linux driver for the
2 *       DVBWorld DVB-S 2101, 2102, DVB-S2 2104 Card
3 *
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"
14 #include "si21xx.h"
15 #include "stv0299.h"
16 #include "z0194a.h"
17 #include "stv0288.h"
18 #include "stb6000.h"
19 #include "eds1547.h"
20 #include "cx24116.h"
21
22 #ifndef USB_PID_DW2102
23 #define USB_PID_DW2102 0x2102
24 #endif
25
26 #ifndef USB_PID_DW2104
27 #define USB_PID_DW2104 0x2104
28 #endif
29
30 #define DW210X_READ_MSG 0
31 #define DW210X_WRITE_MSG 1
32
33 #define REG_1F_SYMBOLRATE_BYTE0 0x1f
34 #define REG_20_SYMBOLRATE_BYTE1 0x20
35 #define REG_21_SYMBOLRATE_BYTE2 0x21
36 /* on my own*/
37 #define DW2102_VOLTAGE_CTRL (0x1800)
38 #define DW2102_RC_QUERY (0x1a00)
39
40 struct dw210x_state {
41         u32 last_key_pressed;
42 };
43 struct dw210x_rc_keys {
44         u32 keycode;
45         u32 event;
46 };
47
48 /* debug */
49 static int dvb_usb_dw2102_debug;
50 module_param_named(debug, dvb_usb_dw2102_debug, int, 0644);
51 MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer (or-able))." DVB_USB_DEBUG_STATUS);
52
53 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
54
55 static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
56                         u16 index, u8 * data, u16 len, int flags)
57 {
58         int ret;
59         u8 u8buf[len];
60
61         unsigned int pipe = (flags == DW210X_READ_MSG) ?
62                                 usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
63         u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
64
65         if (flags == DW210X_WRITE_MSG)
66                 memcpy(u8buf, data, len);
67         ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
68                                 value, index , u8buf, len, 2000);
69
70         if (flags == DW210X_READ_MSG)
71                 memcpy(data, u8buf, len);
72         return ret;
73 }
74
75 /* I2C */
76 static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
77                 int num)
78 {
79 struct dvb_usb_device *d = i2c_get_adapdata(adap);
80         int i = 0, ret = 0;
81         u8 buf6[] = {0x2c, 0x05, 0xc0, 0, 0, 0, 0};
82         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 */
92                 value = msg[0].buf[0];/* register */
93                 for (i = 0; i < msg[1].len; i++) {
94                         value = value + i;
95                         ret = dw210x_op_rw(d->udev, 0xb5, value, 0,
96                                         buf6, 2, DW210X_READ_MSG);
97                         msg[1].buf[i] = buf6[0];
98                 }
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];
107                         ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
108                                         buf6, 3, DW210X_WRITE_MSG);
109                         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];
120                                 ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
121                                                 buf6, 7, DW210X_WRITE_MSG);
122                         } else {
123                         /* read from tuner */
124                                 ret = dw210x_op_rw(d->udev, 0xb5, 0, 0,
125                                                 buf6, 1, DW210X_READ_MSG);
126                                 msg[0].buf[0] = buf6[0];
127                         }
128                         break;
129                 case (DW2102_RC_QUERY):
130                         ret  = dw210x_op_rw(d->udev, 0xb8, 0, 0,
131                                         buf6, 2, DW210X_READ_MSG);
132                         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];
138                         ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
139                                         buf6, 2, DW210X_WRITE_MSG);
140                         break;
141                 }
142
143                 break;
144         }
145
146         mutex_unlock(&d->i2c_mutex);
147         return num;
148 }
149
150 static 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 }
205 static 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 }
277
278 static 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];
297                 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
298                                 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
299                 /* second read registers */
300                 ret = dw210x_op_rw(d->udev, 0xc3, 0xab , 0,
301                                 ibuf, msg[1].len + 2, DW210X_READ_MSG);
302                 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));
320                                         ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
321                                                 obuf, (len > 16 ? 16 : len) + 3, DW210X_WRITE_MSG);
322                                         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);
331                                 ret = dw210x_op_rw(d->udev, 0xc2, 0, 0,
332                                                 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
333                         }
334                         break;
335                 }
336                 case(DW2102_RC_QUERY): {
337                         u8 ibuf[2];
338                         ret  = dw210x_op_rw(d->udev, 0xb8, 0, 0,
339                                         ibuf, 2, DW210X_READ_MSG);
340                         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];
347                         ret = dw210x_op_rw(d->udev, 0xb2, 0, 0,
348                                         obuf, 2, DW210X_WRITE_MSG);
349                         break;
350                 }
351                 }
352
353                 break;
354         }
355
356         mutex_unlock(&d->i2c_mutex);
357         return num;
358 }
359
360 static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
361 {
362         return I2C_FUNC_I2C;
363 }
364
365 static struct i2c_algorithm dw2102_i2c_algo = {
366         .master_xfer = dw2102_i2c_transfer,
367         .functionality = dw210x_i2c_func,
368 };
369
370 static struct i2c_algorithm dw2102_serit_i2c_algo = {
371         .master_xfer = dw2102_serit_i2c_transfer,
372         .functionality = dw210x_i2c_func,
373 };
374
375 static struct i2c_algorithm dw2102_earda_i2c_algo = {
376         .master_xfer = dw2102_earda_i2c_transfer,
377         .functionality = dw210x_i2c_func,
378 };
379
380 static struct i2c_algorithm dw2104_i2c_algo = {
381         .master_xfer = dw2104_i2c_transfer,
382         .functionality = dw210x_i2c_func,
383 };
384
385 static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
386 {
387         int i;
388         u8 ibuf[] = {0, 0};
389         u8 eeprom[256], eepromline[16];
390
391         for (i = 0; i < 256; i++) {
392                 if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
393                         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
408 static int dw210x_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
409 {
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
425 static struct cx24116_config dw2104_config = {
426         .demod_address = 0x55,
427         .mpg_clk_pos_pol = 0x01,
428 };
429
430 static struct si21xx_config serit_sp1511lhb_config = {
431         .demod_address = 0x68,
432         .min_delay_ms = 100,
433
434 };
435
436 static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
437 {
438         if ((d->fe = dvb_attach(cx24116_attach, &dw2104_config,
439                         &d->dev->i2c_adap)) != NULL) {
440                 d->fe->ops.set_voltage = dw210x_set_voltage;
441                 info("Attached cx24116!\n");
442                 return 0;
443         }
444         return -EIO;
445 }
446
447 static struct dvb_usb_device_properties dw2102_properties;
448
449 static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
450 {
451         if (dw2102_properties.i2c_algo == &dw2102_serit_i2c_algo) {
452                 /*dw2102_properties.adapter->tuner_attach = NULL;*/
453                 d->fe = dvb_attach(si21xx_attach, &serit_sp1511lhb_config,
454                                         &d->dev->i2c_adap);
455                 if (d->fe != NULL) {
456                         d->fe->ops.set_voltage = dw210x_set_voltage;
457                         info("Attached si21xx!\n");
458                         return 0;
459                 }
460         }
461         if (dw2102_properties.i2c_algo == &dw2102_earda_i2c_algo) {
462                 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
463                 d->fe = dvb_attach(stv0288_attach, &earda_config,
464                                         &d->dev->i2c_adap);
465                 if (d->fe != NULL) {
466                         d->fe->ops.set_voltage = dw210x_set_voltage;
467                         info("Attached stv0288!\n");
468                         return 0;
469                 }
470         }
471
472         if (dw2102_properties.i2c_algo == &dw2102_i2c_algo) {
473                 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
474                 d->fe = dvb_attach(stv0299_attach, &sharp_z0194a_config,
475                                         &d->dev->i2c_adap);
476                 if (d->fe != NULL) {
477                         d->fe->ops.set_voltage = dw210x_set_voltage;
478                         info("Attached stv0299!\n");
479                         return 0;
480                 }
481         }
482         return -EIO;
483 }
484
485 static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
486 {
487         dvb_attach(dvb_pll_attach, adap->fe, 0x60,
488                 &adap->dev->i2c_adap, DVB_PLL_OPERA1);
489         return 0;
490 }
491
492 static int dw2102_earda_tuner_attach(struct dvb_usb_adapter *adap)
493 {
494         dvb_attach(stb6000_attach, adap->fe, 0x61,
495                 &adap->dev->i2c_adap);
496
497         return 0;
498 }
499
500 static struct dvb_usb_rc_key dw210x_rc_keys[] = {
501         { 0xf8, 0x0a, KEY_Q },          /*power*/
502         { 0xf8, 0x0c, KEY_M },          /*mute*/
503         { 0xf8, 0x11, KEY_1 },
504         { 0xf8, 0x12, KEY_2 },
505         { 0xf8, 0x13, KEY_3 },
506         { 0xf8, 0x14, KEY_4 },
507         { 0xf8, 0x15, KEY_5 },
508         { 0xf8, 0x16, KEY_6 },
509         { 0xf8, 0x17, KEY_7 },
510         { 0xf8, 0x18, KEY_8 },
511         { 0xf8, 0x19, KEY_9 },
512         { 0xf8, 0x10, KEY_0 },
513         { 0xf8, 0x1c, KEY_PAGEUP },     /*ch+*/
514         { 0xf8, 0x0f, KEY_PAGEDOWN },   /*ch-*/
515         { 0xf8, 0x1a, KEY_O },          /*vol+*/
516         { 0xf8, 0x0e, KEY_Z },          /*vol-*/
517         { 0xf8, 0x04, KEY_R },          /*rec*/
518         { 0xf8, 0x09, KEY_D },          /*fav*/
519         { 0xf8, 0x08, KEY_BACKSPACE },  /*rewind*/
520         { 0xf8, 0x07, KEY_A },          /*fast*/
521         { 0xf8, 0x0b, KEY_P },          /*pause*/
522         { 0xf8, 0x02, KEY_ESC },        /*cancel*/
523         { 0xf8, 0x03, KEY_G },          /*tab*/
524         { 0xf8, 0x00, KEY_UP },         /*up*/
525         { 0xf8, 0x1f, KEY_ENTER },      /*ok*/
526         { 0xf8, 0x01, KEY_DOWN },       /*down*/
527         { 0xf8, 0x05, KEY_C },          /*cap*/
528         { 0xf8, 0x06, KEY_S },          /*stop*/
529         { 0xf8, 0x40, KEY_F },          /*full*/
530         { 0xf8, 0x1e, KEY_W },          /*tvmode*/
531         { 0xf8, 0x1b, KEY_B },          /*recall*/
532
533 };
534
535
536
537 static int dw2102_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
538 {
539         struct dw210x_state *st = d->priv;
540         u8 key[2];
541         struct i2c_msg msg[] = {
542                 {.addr = DW2102_RC_QUERY, .flags = I2C_M_RD, .buf = key,
543                 .len = 2},
544         };
545         int i;
546
547         *state = REMOTE_NO_KEY_PRESSED;
548         if (dw2102_i2c_transfer(&d->i2c_adap, msg, 1) == 1) {
549                 for (i = 0; i < ARRAY_SIZE(dw210x_rc_keys); i++) {
550                         if (dw210x_rc_keys[i].data == msg[0].buf[0]) {
551                                 *state = REMOTE_KEY_PRESSED;
552                                 *event = dw210x_rc_keys[i].event;
553                                 st->last_key_pressed =
554                                         dw210x_rc_keys[i].event;
555                                 break;
556                         }
557                 st->last_key_pressed = 0;
558                 }
559         }
560         /* info("key: %x %x\n",key[0],key[1]); */
561         return 0;
562 }
563
564 static struct usb_device_id dw2102_table[] = {
565         {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
566         {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
567         {USB_DEVICE(USB_VID_CYPRESS, 0x2104)},
568         {USB_DEVICE(0x9022, 0xd650)},
569         { }
570 };
571
572 MODULE_DEVICE_TABLE(usb, dw2102_table);
573
574 static int dw2102_load_firmware(struct usb_device *dev,
575                         const struct firmware *frmwr)
576 {
577         u8 *b, *p;
578         int ret = 0, i;
579         u8 reset;
580         u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
581         const struct firmware *fw;
582         const char *filename = "dvb-usb-dw2101.fw";
583         switch (dev->descriptor.idProduct) {
584         case 0x2101:
585                 ret = request_firmware(&fw, filename, &dev->dev);
586                 if (ret != 0) {
587                         err("did not find the firmware file. (%s) "
588                         "Please see linux/Documentation/dvb/ for more details "
589                         "on firmware-problems.", filename);
590                         return ret;
591                 }
592                 break;
593         default:
594                 fw = frmwr;
595                 break;
596         }
597         info("start downloading DW210X firmware");
598         p = kmalloc(fw->size, GFP_KERNEL);
599         reset = 1;
600         /*stop the CPU*/
601         dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
602         dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
603
604         if (p != NULL) {
605                 memcpy(p, fw->data, fw->size);
606                 for (i = 0; i < fw->size; i += 0x40) {
607                         b = (u8 *) p + i;
608                         if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
609                                         DW210X_WRITE_MSG) != 0x40) {
610                                 err("error while transferring firmware");
611                                 ret = -EINVAL;
612                                 break;
613                         }
614                 }
615                 /* restart the CPU */
616                 reset = 0;
617                 if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
618                                         DW210X_WRITE_MSG) != 1) {
619                         err("could not restart the USB controller CPU.");
620                         ret = -EINVAL;
621                 }
622                 if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
623                                         DW210X_WRITE_MSG) != 1) {
624                         err("could not restart the USB controller CPU.");
625                         ret = -EINVAL;
626                 }
627                 /* init registers */
628                 switch (dev->descriptor.idProduct) {
629                 case USB_PID_DW2104:
630                 case 0xd650:
631                         reset = 1;
632                         dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
633                                         DW210X_WRITE_MSG);
634                         reset = 0;
635                         dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
636                                         DW210X_WRITE_MSG);
637                         break;
638                 case USB_PID_DW2102:
639                         dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
640                                         DW210X_WRITE_MSG);
641                         dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
642                                         DW210X_READ_MSG);
643                         /* check STV0299 frontend  */
644                         dw210x_op_rw(dev, 0xb5, 0, 0, &reset16[0], 2,
645                                         DW210X_READ_MSG);
646                         if (reset16[0] == 0xa1) {
647                                 dw2102_properties.i2c_algo = &dw2102_i2c_algo;
648                                 dw2102_properties.adapter->tuner_attach = &dw2102_tuner_attach;
649                                 break;
650                         } else {
651                                 /* check STV0288 frontend  */
652                                 reset16[0] = 0xd0;
653                                 reset16[1] = 1;
654                                 reset16[2] = 0;
655                                 dw210x_op_rw(dev, 0xc2, 0, 0, &reset16[0], 3,
656                                                 DW210X_WRITE_MSG);
657                                 dw210x_op_rw(dev, 0xc3, 0xd1, 0, &reset16[0], 3,
658                                                 DW210X_READ_MSG);
659                                 if (reset16[2] == 0x11) {
660                                         dw2102_properties.i2c_algo = &dw2102_earda_i2c_algo;
661                                         dw2102_properties.adapter->tuner_attach = &dw2102_earda_tuner_attach;
662                                         break;
663                                 }
664                         }
665                 case 0x2101:
666                         dw210x_op_rw(dev, 0xbc, 0x0030, 0, &reset16[0], 2,
667                                         DW210X_READ_MSG);
668                         dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
669                                         DW210X_READ_MSG);
670                         dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
671                                         DW210X_READ_MSG);
672                         dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
673                                         DW210X_READ_MSG);
674                         break;
675                 }
676                 msleep(100);
677                 kfree(p);
678         }
679         return ret;
680 }
681
682 static struct dvb_usb_device_properties dw2102_properties = {
683         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
684         .usb_ctrl = DEVICE_SPECIFIC,
685         .firmware = "dvb-usb-dw2102.fw",
686         .size_of_priv = sizeof(struct dw210x_state),
687         .no_reconnect = 1,
688
689         .i2c_algo = &dw2102_serit_i2c_algo,
690         .rc_key_map = dw210x_rc_keys,
691         .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
692         .rc_interval = 150,
693         .rc_query = dw2102_rc_query,
694
695         .generic_bulk_ctrl_endpoint = 0x81,
696         /* parameter for the MPEG2-data transfer */
697         .num_adapters = 1,
698         .download_firmware = dw2102_load_firmware,
699         .read_mac_address = dw210x_read_mac_address,
700                 .adapter = {
701                 {
702                         .frontend_attach = dw2102_frontend_attach,
703                         .streaming_ctrl = NULL,
704                         .tuner_attach = NULL,
705                         .stream = {
706                                 .type = USB_BULK,
707                                 .count = 8,
708                                 .endpoint = 0x82,
709                                 .u = {
710                                         .bulk = {
711                                                 .buffersize = 4096,
712                                         }
713                                 }
714                         },
715                 }
716         },
717         .num_device_descs = 2,
718         .devices = {
719                 {"DVBWorld DVB-S 2102 USB2.0",
720                         {&dw2102_table[0], NULL},
721                         {NULL},
722                 },
723                 {"DVBWorld DVB-S 2101 USB2.0",
724                         {&dw2102_table[1], NULL},
725                         {NULL},
726                 },
727         }
728 };
729
730 static struct dvb_usb_device_properties dw2104_properties = {
731         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
732         .usb_ctrl = DEVICE_SPECIFIC,
733         .firmware = "dvb-usb-dw2104.fw",
734         .size_of_priv = sizeof(struct dw210x_state),
735         .no_reconnect = 1,
736
737         .i2c_algo = &dw2104_i2c_algo,
738         .rc_key_map = dw210x_rc_keys,
739         .rc_key_map_size = ARRAY_SIZE(dw210x_rc_keys),
740         .rc_interval = 150,
741         .rc_query = dw2102_rc_query,
742
743         .generic_bulk_ctrl_endpoint = 0x81,
744         /* parameter for the MPEG2-data transfer */
745         .num_adapters = 1,
746         .download_firmware = dw2102_load_firmware,
747         .read_mac_address = dw210x_read_mac_address,
748         .adapter = {
749                 {
750                         .frontend_attach = dw2104_frontend_attach,
751                         .streaming_ctrl = NULL,
752                         /*.tuner_attach = dw2104_tuner_attach,*/
753                         .stream = {
754                                 .type = USB_BULK,
755                                 .count = 8,
756                                 .endpoint = 0x82,
757                                 .u = {
758                                         .bulk = {
759                                                 .buffersize = 4096,
760                                         }
761                                 }
762                         },
763                 }
764         },
765         .num_device_descs = 2,
766         .devices = {
767                 { "DVBWorld DW2104 USB2.0",
768                         {&dw2102_table[2], NULL},
769                         {NULL},
770                 },
771                 { "TeVii S650 USB2.0",
772                         {&dw2102_table[3], NULL},
773                         {NULL},
774                 },
775         }
776 };
777
778 static int dw2102_probe(struct usb_interface *intf,
779                 const struct usb_device_id *id)
780 {
781         if (0 == dvb_usb_device_init(intf, &dw2102_properties,
782                         THIS_MODULE, NULL, adapter_nr) ||
783             0 == dvb_usb_device_init(intf, &dw2104_properties,
784                         THIS_MODULE, NULL, adapter_nr)) {
785                 return 0;
786         }
787         return -ENODEV;
788 }
789
790 static struct usb_driver dw2102_driver = {
791         .name = "dw2102",
792         .probe = dw2102_probe,
793         .disconnect = dvb_usb_device_exit,
794         .id_table = dw2102_table,
795 };
796
797 static int __init dw2102_module_init(void)
798 {
799         int ret =  usb_register(&dw2102_driver);
800         if (ret)
801                 err("usb_register failed. Error number %d", ret);
802
803         return ret;
804 }
805
806 static void __exit dw2102_module_exit(void)
807 {
808         usb_deregister(&dw2102_driver);
809 }
810
811 module_init(dw2102_module_init);
812 module_exit(dw2102_module_exit);
813
814 MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
815 MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104 USB2.0 device");
816 MODULE_VERSION("0.1");
817 MODULE_LICENSE("GPL");