GNU Linux-libre 4.9.318-gnu1
[releases.git] / drivers / media / usb / dvb-usb / dw2102.c
1 /* DVB USB framework compliant Linux driver for the
2  *      DVBWorld DVB-S 2101, 2102, DVB-S2 2104, DVB-C 3101,
3  *      TeVii S421, S480, S482, S600, S630, S632, S650, S660, S662,
4  *      Prof 1100, 7500,
5  *      Geniatech SU3000, T220,
6  *      TechnoTrend S2-4600,
7  *      Terratec Cinergy S2 cards
8  * Copyright (C) 2008-2012 Igor M. Liplianin (liplianin@me.by)
9  *
10  *      This program is free software; you can redistribute it and/or modify it
11  *      under the terms of the GNU General Public License as published by the
12  *      Free Software Foundation, version 2.
13  *
14  * see Documentation/dvb/README.dvb-usb for more information
15  */
16 #include "dvb-usb-ids.h"
17 #include "dw2102.h"
18 #include "si21xx.h"
19 #include "stv0299.h"
20 #include "z0194a.h"
21 #include "stv0288.h"
22 #include "stb6000.h"
23 #include "eds1547.h"
24 #include "cx24116.h"
25 #include "tda1002x.h"
26 #include "mt312.h"
27 #include "zl10039.h"
28 #include "ts2020.h"
29 #include "ds3000.h"
30 #include "stv0900.h"
31 #include "stv6110.h"
32 #include "stb6100.h"
33 #include "stb6100_proc.h"
34 #include "m88rs2000.h"
35 #include "tda18271.h"
36 #include "cxd2820r.h"
37 #include "m88ds3103.h"
38
39 /* Max transfer size done by I2C transfer functions */
40 #define MAX_XFER_SIZE  64
41
42
43 #define DW210X_READ_MSG 0
44 #define DW210X_WRITE_MSG 1
45
46 #define REG_1F_SYMBOLRATE_BYTE0 0x1f
47 #define REG_20_SYMBOLRATE_BYTE1 0x20
48 #define REG_21_SYMBOLRATE_BYTE2 0x21
49 /* on my own*/
50 #define DW2102_VOLTAGE_CTRL (0x1800)
51 #define SU3000_STREAM_CTRL (0x1900)
52 #define DW2102_RC_QUERY (0x1a00)
53 #define DW2102_LED_CTRL (0x1b00)
54
55 #define DW2101_FIRMWARE "/*(DEBLOBBED)*/"
56 #define DW2102_FIRMWARE "/*(DEBLOBBED)*/"
57 #define DW2104_FIRMWARE "/*(DEBLOBBED)*/"
58 #define DW3101_FIRMWARE "/*(DEBLOBBED)*/"
59 #define S630_FIRMWARE   "/*(DEBLOBBED)*/"
60 #define S660_FIRMWARE   "/*(DEBLOBBED)*/"
61 #define P1100_FIRMWARE  "/*(DEBLOBBED)*/"
62 #define P7500_FIRMWARE  "/*(DEBLOBBED)*/"
63
64 #define err_str "did not find the firmware file. (%s) " \
65                 "Please see linux/Documentation/dvb/ for more details " \
66                 "on firmware-problems."
67
68 struct dw2102_state {
69         u8 initialized;
70         u8 last_lock;
71         u8 data[MAX_XFER_SIZE + 4];
72         struct i2c_client *i2c_client_demod;
73         struct i2c_client *i2c_client_tuner;
74
75         /* fe hook functions*/
76         int (*old_set_voltage)(struct dvb_frontend *f, enum fe_sec_voltage v);
77         int (*fe_read_status)(struct dvb_frontend *fe,
78                               enum fe_status *status);
79 };
80
81 /* debug */
82 static int dvb_usb_dw2102_debug;
83 module_param_named(debug, dvb_usb_dw2102_debug, int, 0644);
84 MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer 4=rc(or-able))."
85                                                 DVB_USB_DEBUG_STATUS);
86
87 /* demod probe */
88 static int demod_probe = 1;
89 module_param_named(demod, demod_probe, int, 0644);
90 MODULE_PARM_DESC(demod, "demod to probe (1=cx24116 2=stv0903+stv6110 "
91                         "4=stv0903+stb6100(or-able)).");
92
93 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
94
95 static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
96                         u16 index, u8 * data, u16 len, int flags)
97 {
98         int ret;
99         u8 *u8buf;
100         unsigned int pipe = (flags == DW210X_READ_MSG) ?
101                                 usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
102         u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
103
104         u8buf = kmalloc(len, GFP_KERNEL);
105         if (!u8buf)
106                 return -ENOMEM;
107
108
109         if (flags == DW210X_WRITE_MSG)
110                 memcpy(u8buf, data, len);
111         ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
112                                 value, index , u8buf, len, 2000);
113
114         if (flags == DW210X_READ_MSG)
115                 memcpy(data, u8buf, len);
116
117         kfree(u8buf);
118         return ret;
119 }
120
121 /* I2C */
122 static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
123                 int num)
124 {
125         struct dvb_usb_device *d = i2c_get_adapdata(adap);
126         int i = 0;
127         u8 buf6[] = {0x2c, 0x05, 0xc0, 0, 0, 0, 0};
128         u16 value;
129
130         if (!d)
131                 return -ENODEV;
132         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
133                 return -EAGAIN;
134
135         switch (num) {
136         case 2:
137                 /* read stv0299 register */
138                 value = msg[0].buf[0];/* register */
139                 for (i = 0; i < msg[1].len; i++) {
140                         dw210x_op_rw(d->udev, 0xb5, value + i, 0,
141                                         buf6, 2, DW210X_READ_MSG);
142                         msg[1].buf[i] = buf6[0];
143                 }
144                 break;
145         case 1:
146                 switch (msg[0].addr) {
147                 case 0x68:
148                         /* write to stv0299 register */
149                         buf6[0] = 0x2a;
150                         buf6[1] = msg[0].buf[0];
151                         buf6[2] = msg[0].buf[1];
152                         dw210x_op_rw(d->udev, 0xb2, 0, 0,
153                                         buf6, 3, DW210X_WRITE_MSG);
154                         break;
155                 case 0x60:
156                         if (msg[0].flags == 0) {
157                         /* write to tuner pll */
158                                 buf6[0] = 0x2c;
159                                 buf6[1] = 5;
160                                 buf6[2] = 0xc0;
161                                 buf6[3] = msg[0].buf[0];
162                                 buf6[4] = msg[0].buf[1];
163                                 buf6[5] = msg[0].buf[2];
164                                 buf6[6] = msg[0].buf[3];
165                                 dw210x_op_rw(d->udev, 0xb2, 0, 0,
166                                                 buf6, 7, DW210X_WRITE_MSG);
167                         } else {
168                         /* read from tuner */
169                                 dw210x_op_rw(d->udev, 0xb5, 0, 0,
170                                                 buf6, 1, DW210X_READ_MSG);
171                                 msg[0].buf[0] = buf6[0];
172                         }
173                         break;
174                 case (DW2102_RC_QUERY):
175                         dw210x_op_rw(d->udev, 0xb8, 0, 0,
176                                         buf6, 2, DW210X_READ_MSG);
177                         msg[0].buf[0] = buf6[0];
178                         msg[0].buf[1] = buf6[1];
179                         break;
180                 case (DW2102_VOLTAGE_CTRL):
181                         buf6[0] = 0x30;
182                         buf6[1] = msg[0].buf[0];
183                         dw210x_op_rw(d->udev, 0xb2, 0, 0,
184                                         buf6, 2, DW210X_WRITE_MSG);
185                         break;
186                 }
187
188                 break;
189         }
190
191         mutex_unlock(&d->i2c_mutex);
192         return num;
193 }
194
195 static int dw2102_serit_i2c_transfer(struct i2c_adapter *adap,
196                                                 struct i2c_msg msg[], int num)
197 {
198         struct dvb_usb_device *d = i2c_get_adapdata(adap);
199         u8 buf6[] = {0, 0, 0, 0, 0, 0, 0};
200
201         if (!d)
202                 return -ENODEV;
203         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
204                 return -EAGAIN;
205
206         switch (num) {
207         case 2:
208                 if (msg[0].len != 1) {
209                         warn("i2c rd: len=%d is not 1!\n",
210                              msg[0].len);
211                         num = -EOPNOTSUPP;
212                         break;
213                 }
214
215                 if (2 + msg[1].len > sizeof(buf6)) {
216                         warn("i2c rd: len=%d is too big!\n",
217                              msg[1].len);
218                         num = -EOPNOTSUPP;
219                         break;
220                 }
221
222                 /* read si2109 register by number */
223                 buf6[0] = msg[0].addr << 1;
224                 buf6[1] = msg[0].len;
225                 buf6[2] = msg[0].buf[0];
226                 dw210x_op_rw(d->udev, 0xc2, 0, 0,
227                                 buf6, msg[0].len + 2, DW210X_WRITE_MSG);
228                 /* read si2109 register */
229                 dw210x_op_rw(d->udev, 0xc3, 0xd0, 0,
230                                 buf6, msg[1].len + 2, DW210X_READ_MSG);
231                 memcpy(msg[1].buf, buf6 + 2, msg[1].len);
232
233                 break;
234         case 1:
235                 switch (msg[0].addr) {
236                 case 0x68:
237                         if (2 + msg[0].len > sizeof(buf6)) {
238                                 warn("i2c wr: len=%d is too big!\n",
239                                      msg[0].len);
240                                 num = -EOPNOTSUPP;
241                                 break;
242                         }
243
244                         /* write to si2109 register */
245                         buf6[0] = msg[0].addr << 1;
246                         buf6[1] = msg[0].len;
247                         memcpy(buf6 + 2, msg[0].buf, msg[0].len);
248                         dw210x_op_rw(d->udev, 0xc2, 0, 0, buf6,
249                                         msg[0].len + 2, DW210X_WRITE_MSG);
250                         break;
251                 case(DW2102_RC_QUERY):
252                         dw210x_op_rw(d->udev, 0xb8, 0, 0,
253                                         buf6, 2, DW210X_READ_MSG);
254                         msg[0].buf[0] = buf6[0];
255                         msg[0].buf[1] = buf6[1];
256                         break;
257                 case(DW2102_VOLTAGE_CTRL):
258                         buf6[0] = 0x30;
259                         buf6[1] = msg[0].buf[0];
260                         dw210x_op_rw(d->udev, 0xb2, 0, 0,
261                                         buf6, 2, DW210X_WRITE_MSG);
262                         break;
263                 }
264                 break;
265         }
266
267         mutex_unlock(&d->i2c_mutex);
268         return num;
269 }
270
271 static int dw2102_earda_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
272 {
273         struct dvb_usb_device *d = i2c_get_adapdata(adap);
274         int ret;
275
276         if (!d)
277                 return -ENODEV;
278         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
279                 return -EAGAIN;
280
281         switch (num) {
282         case 2: {
283                 /* read */
284                 /* first write first register number */
285                 u8 ibuf[MAX_XFER_SIZE], obuf[3];
286
287                 if (2 + msg[0].len != sizeof(obuf)) {
288                         warn("i2c rd: len=%d is not 1!\n",
289                              msg[0].len);
290                         ret = -EOPNOTSUPP;
291                         goto unlock;
292                 }
293
294                 if (2 + msg[1].len > sizeof(ibuf)) {
295                         warn("i2c rd: len=%d is too big!\n",
296                              msg[1].len);
297                         ret = -EOPNOTSUPP;
298                         goto unlock;
299                 }
300
301                 obuf[0] = msg[0].addr << 1;
302                 obuf[1] = msg[0].len;
303                 obuf[2] = msg[0].buf[0];
304                 dw210x_op_rw(d->udev, 0xc2, 0, 0,
305                                 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
306                 /* second read registers */
307                 dw210x_op_rw(d->udev, 0xc3, 0xd1 , 0,
308                                 ibuf, msg[1].len + 2, DW210X_READ_MSG);
309                 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
310
311                 break;
312         }
313         case 1:
314                 switch (msg[0].addr) {
315                 case 0x68: {
316                         /* write to register */
317                         u8 obuf[MAX_XFER_SIZE];
318
319                         if (2 + msg[0].len > sizeof(obuf)) {
320                                 warn("i2c wr: len=%d is too big!\n",
321                                      msg[1].len);
322                                 ret = -EOPNOTSUPP;
323                                 goto unlock;
324                         }
325
326                         obuf[0] = msg[0].addr << 1;
327                         obuf[1] = msg[0].len;
328                         memcpy(obuf + 2, msg[0].buf, msg[0].len);
329                         dw210x_op_rw(d->udev, 0xc2, 0, 0,
330                                         obuf, msg[0].len + 2, DW210X_WRITE_MSG);
331                         break;
332                 }
333                 case 0x61: {
334                         /* write to tuner */
335                         u8 obuf[MAX_XFER_SIZE];
336
337                         if (2 + msg[0].len > sizeof(obuf)) {
338                                 warn("i2c wr: len=%d is too big!\n",
339                                      msg[1].len);
340                                 ret = -EOPNOTSUPP;
341                                 goto unlock;
342                         }
343
344                         obuf[0] = msg[0].addr << 1;
345                         obuf[1] = msg[0].len;
346                         memcpy(obuf + 2, msg[0].buf, msg[0].len);
347                         dw210x_op_rw(d->udev, 0xc2, 0, 0,
348                                         obuf, msg[0].len + 2, DW210X_WRITE_MSG);
349                         break;
350                 }
351                 case(DW2102_RC_QUERY): {
352                         u8 ibuf[2];
353                         dw210x_op_rw(d->udev, 0xb8, 0, 0,
354                                         ibuf, 2, DW210X_READ_MSG);
355                         memcpy(msg[0].buf, ibuf , 2);
356                         break;
357                 }
358                 case(DW2102_VOLTAGE_CTRL): {
359                         u8 obuf[2];
360                         obuf[0] = 0x30;
361                         obuf[1] = msg[0].buf[0];
362                         dw210x_op_rw(d->udev, 0xb2, 0, 0,
363                                         obuf, 2, DW210X_WRITE_MSG);
364                         break;
365                 }
366                 }
367
368                 break;
369         }
370         ret = num;
371
372 unlock:
373         mutex_unlock(&d->i2c_mutex);
374         return ret;
375 }
376
377 static int dw2104_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
378 {
379         struct dvb_usb_device *d = i2c_get_adapdata(adap);
380         int len, i, j, ret;
381
382         if (!d)
383                 return -ENODEV;
384         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
385                 return -EAGAIN;
386
387         for (j = 0; j < num; j++) {
388                 switch (msg[j].addr) {
389                 case(DW2102_RC_QUERY): {
390                         u8 ibuf[2];
391                         dw210x_op_rw(d->udev, 0xb8, 0, 0,
392                                         ibuf, 2, DW210X_READ_MSG);
393                         memcpy(msg[j].buf, ibuf , 2);
394                         break;
395                 }
396                 case(DW2102_VOLTAGE_CTRL): {
397                         u8 obuf[2];
398                         obuf[0] = 0x30;
399                         obuf[1] = msg[j].buf[0];
400                         dw210x_op_rw(d->udev, 0xb2, 0, 0,
401                                         obuf, 2, DW210X_WRITE_MSG);
402                         break;
403                 }
404                 /*case 0x55: cx24116
405                 case 0x6a: stv0903
406                 case 0x68: ds3000, stv0903
407                 case 0x60: ts2020, stv6110, stb6100 */
408                 default: {
409                         if (msg[j].flags == I2C_M_RD) {
410                                 /* read registers */
411                                 u8  ibuf[MAX_XFER_SIZE];
412
413                                 if (2 + msg[j].len > sizeof(ibuf)) {
414                                         warn("i2c rd: len=%d is too big!\n",
415                                              msg[j].len);
416                                         ret = -EOPNOTSUPP;
417                                         goto unlock;
418                                 }
419
420                                 dw210x_op_rw(d->udev, 0xc3,
421                                                 (msg[j].addr << 1) + 1, 0,
422                                                 ibuf, msg[j].len + 2,
423                                                 DW210X_READ_MSG);
424                                 memcpy(msg[j].buf, ibuf + 2, msg[j].len);
425                                 mdelay(10);
426                         } else if (((msg[j].buf[0] == 0xb0) &&
427                                                 (msg[j].addr == 0x68)) ||
428                                                 ((msg[j].buf[0] == 0xf7) &&
429                                                 (msg[j].addr == 0x55))) {
430                                 /* write firmware */
431                                 u8 obuf[19];
432                                 obuf[0] = msg[j].addr << 1;
433                                 obuf[1] = (msg[j].len > 15 ? 17 : msg[j].len);
434                                 obuf[2] = msg[j].buf[0];
435                                 len = msg[j].len - 1;
436                                 i = 1;
437                                 do {
438                                         memcpy(obuf + 3, msg[j].buf + i,
439                                                         (len > 16 ? 16 : len));
440                                         dw210x_op_rw(d->udev, 0xc2, 0, 0,
441                                                 obuf, (len > 16 ? 16 : len) + 3,
442                                                 DW210X_WRITE_MSG);
443                                         i += 16;
444                                         len -= 16;
445                                 } while (len > 0);
446                         } else {
447                                 /* write registers */
448                                 u8 obuf[MAX_XFER_SIZE];
449
450                                 if (2 + msg[j].len > sizeof(obuf)) {
451                                         warn("i2c wr: len=%d is too big!\n",
452                                              msg[j].len);
453                                         ret = -EOPNOTSUPP;
454                                         goto unlock;
455                                 }
456
457                                 obuf[0] = msg[j].addr << 1;
458                                 obuf[1] = msg[j].len;
459                                 memcpy(obuf + 2, msg[j].buf, msg[j].len);
460                                 dw210x_op_rw(d->udev, 0xc2, 0, 0,
461                                                 obuf, msg[j].len + 2,
462                                                 DW210X_WRITE_MSG);
463                         }
464                         break;
465                 }
466                 }
467
468         }
469         ret = num;
470
471 unlock:
472         mutex_unlock(&d->i2c_mutex);
473         return ret;
474 }
475
476 static int dw3101_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
477                                                                 int num)
478 {
479         struct dvb_usb_device *d = i2c_get_adapdata(adap);
480         int ret;
481         int i;
482
483         if (!d)
484                 return -ENODEV;
485         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
486                 return -EAGAIN;
487
488         switch (num) {
489         case 2: {
490                 /* read */
491                 /* first write first register number */
492                 u8 ibuf[MAX_XFER_SIZE], obuf[3];
493
494                 if (2 + msg[0].len != sizeof(obuf)) {
495                         warn("i2c rd: len=%d is not 1!\n",
496                              msg[0].len);
497                         ret = -EOPNOTSUPP;
498                         goto unlock;
499                 }
500                 if (2 + msg[1].len > sizeof(ibuf)) {
501                         warn("i2c rd: len=%d is too big!\n",
502                              msg[1].len);
503                         ret = -EOPNOTSUPP;
504                         goto unlock;
505                 }
506                 obuf[0] = msg[0].addr << 1;
507                 obuf[1] = msg[0].len;
508                 obuf[2] = msg[0].buf[0];
509                 dw210x_op_rw(d->udev, 0xc2, 0, 0,
510                                 obuf, msg[0].len + 2, DW210X_WRITE_MSG);
511                 /* second read registers */
512                 dw210x_op_rw(d->udev, 0xc3, 0x19 , 0,
513                                 ibuf, msg[1].len + 2, DW210X_READ_MSG);
514                 memcpy(msg[1].buf, ibuf + 2, msg[1].len);
515
516                 break;
517         }
518         case 1:
519                 switch (msg[0].addr) {
520                 case 0x60:
521                 case 0x0c: {
522                         /* write to register */
523                         u8 obuf[MAX_XFER_SIZE];
524
525                         if (2 + msg[0].len > sizeof(obuf)) {
526                                 warn("i2c wr: len=%d is too big!\n",
527                                      msg[0].len);
528                                 ret = -EOPNOTSUPP;
529                                 goto unlock;
530                         }
531                         obuf[0] = msg[0].addr << 1;
532                         obuf[1] = msg[0].len;
533                         memcpy(obuf + 2, msg[0].buf, msg[0].len);
534                         dw210x_op_rw(d->udev, 0xc2, 0, 0,
535                                         obuf, msg[0].len + 2, DW210X_WRITE_MSG);
536                         break;
537                 }
538                 case(DW2102_RC_QUERY): {
539                         u8 ibuf[2];
540                         dw210x_op_rw(d->udev, 0xb8, 0, 0,
541                                         ibuf, 2, DW210X_READ_MSG);
542                         memcpy(msg[0].buf, ibuf , 2);
543                         break;
544                 }
545                 }
546
547                 break;
548         }
549
550         for (i = 0; i < num; i++) {
551                 deb_xfer("%02x:%02x: %s ", i, msg[i].addr,
552                                 msg[i].flags == 0 ? ">>>" : "<<<");
553                 debug_dump(msg[i].buf, msg[i].len, deb_xfer);
554         }
555         ret = num;
556
557 unlock:
558         mutex_unlock(&d->i2c_mutex);
559         return ret;
560 }
561
562 static int s6x0_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
563                                                                 int num)
564 {
565         struct dvb_usb_device *d = i2c_get_adapdata(adap);
566         struct usb_device *udev;
567         int len, i, j, ret;
568
569         if (!d)
570                 return -ENODEV;
571         udev = d->udev;
572         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
573                 return -EAGAIN;
574
575         for (j = 0; j < num; j++) {
576                 switch (msg[j].addr) {
577                 case (DW2102_RC_QUERY): {
578                         u8 ibuf[5];
579                         dw210x_op_rw(d->udev, 0xb8, 0, 0,
580                                         ibuf, 5, DW210X_READ_MSG);
581                         memcpy(msg[j].buf, ibuf + 3, 2);
582                         break;
583                 }
584                 case (DW2102_VOLTAGE_CTRL): {
585                         u8 obuf[2];
586
587                         obuf[0] = 1;
588                         obuf[1] = msg[j].buf[1];/* off-on */
589                         dw210x_op_rw(d->udev, 0x8a, 0, 0,
590                                         obuf, 2, DW210X_WRITE_MSG);
591                         obuf[0] = 3;
592                         obuf[1] = msg[j].buf[0];/* 13v-18v */
593                         dw210x_op_rw(d->udev, 0x8a, 0, 0,
594                                         obuf, 2, DW210X_WRITE_MSG);
595                         break;
596                 }
597                 case (DW2102_LED_CTRL): {
598                         u8 obuf[2];
599
600                         obuf[0] = 5;
601                         obuf[1] = msg[j].buf[0];
602                         dw210x_op_rw(d->udev, 0x8a, 0, 0,
603                                         obuf, 2, DW210X_WRITE_MSG);
604                         break;
605                 }
606                 /*case 0x55: cx24116
607                 case 0x6a: stv0903
608                 case 0x68: ds3000, stv0903, rs2000
609                 case 0x60: ts2020, stv6110, stb6100
610                 case 0xa0: eeprom */
611                 default: {
612                         if (msg[j].flags == I2C_M_RD) {
613                                 /* read registers */
614                                 u8 ibuf[MAX_XFER_SIZE];
615
616                                 if (msg[j].len > sizeof(ibuf)) {
617                                         warn("i2c rd: len=%d is too big!\n",
618                                              msg[j].len);
619                                         ret = -EOPNOTSUPP;
620                                         goto unlock;
621                                 }
622
623                                 dw210x_op_rw(d->udev, 0x91, 0, 0,
624                                                 ibuf, msg[j].len,
625                                                 DW210X_READ_MSG);
626                                 memcpy(msg[j].buf, ibuf, msg[j].len);
627                                 break;
628                         } else if ((msg[j].buf[0] == 0xb0) &&
629                                                 (msg[j].addr == 0x68)) {
630                                 /* write firmware */
631                                 u8 obuf[19];
632                                 obuf[0] = (msg[j].len > 16 ?
633                                                 18 : msg[j].len + 1);
634                                 obuf[1] = msg[j].addr << 1;
635                                 obuf[2] = msg[j].buf[0];
636                                 len = msg[j].len - 1;
637                                 i = 1;
638                                 do {
639                                         memcpy(obuf + 3, msg[j].buf + i,
640                                                         (len > 16 ? 16 : len));
641                                         dw210x_op_rw(d->udev, 0x80, 0, 0,
642                                                 obuf, (len > 16 ? 16 : len) + 3,
643                                                 DW210X_WRITE_MSG);
644                                         i += 16;
645                                         len -= 16;
646                                 } while (len > 0);
647                         } else if (j < (num - 1)) {
648                                 /* write register addr before read */
649                                 u8 obuf[MAX_XFER_SIZE];
650
651                                 if (2 + msg[j].len > sizeof(obuf)) {
652                                         warn("i2c wr: len=%d is too big!\n",
653                                              msg[j].len);
654                                         ret = -EOPNOTSUPP;
655                                         goto unlock;
656                                 }
657
658                                 obuf[0] = msg[j + 1].len;
659                                 obuf[1] = (msg[j].addr << 1);
660                                 memcpy(obuf + 2, msg[j].buf, msg[j].len);
661                                 dw210x_op_rw(d->udev,
662                                                 le16_to_cpu(udev->descriptor.idProduct) ==
663                                                 0x7500 ? 0x92 : 0x90, 0, 0,
664                                                 obuf, msg[j].len + 2,
665                                                 DW210X_WRITE_MSG);
666                                 break;
667                         } else {
668                                 /* write registers */
669                                 u8 obuf[MAX_XFER_SIZE];
670
671                                 if (2 + msg[j].len > sizeof(obuf)) {
672                                         warn("i2c wr: len=%d is too big!\n",
673                                              msg[j].len);
674                                         ret = -EOPNOTSUPP;
675                                         goto unlock;
676                                 }
677                                 obuf[0] = msg[j].len + 1;
678                                 obuf[1] = (msg[j].addr << 1);
679                                 memcpy(obuf + 2, msg[j].buf, msg[j].len);
680                                 dw210x_op_rw(d->udev, 0x80, 0, 0,
681                                                 obuf, msg[j].len + 2,
682                                                 DW210X_WRITE_MSG);
683                                 break;
684                         }
685                         break;
686                 }
687                 }
688         }
689         ret = num;
690
691 unlock:
692         mutex_unlock(&d->i2c_mutex);
693         return ret;
694 }
695
696 static int su3000_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
697                                                                 int num)
698 {
699         struct dvb_usb_device *d = i2c_get_adapdata(adap);
700         struct dw2102_state *state;
701
702         if (!d)
703                 return -ENODEV;
704
705         state = d->priv;
706
707         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
708                 return -EAGAIN;
709         if (mutex_lock_interruptible(&d->data_mutex) < 0) {
710                 mutex_unlock(&d->i2c_mutex);
711                 return -EAGAIN;
712         }
713
714         switch (num) {
715         case 1:
716                 switch (msg[0].addr) {
717                 case SU3000_STREAM_CTRL:
718                         state->data[0] = msg[0].buf[0] + 0x36;
719                         state->data[1] = 3;
720                         state->data[2] = 0;
721                         if (dvb_usb_generic_rw(d, state->data, 3,
722                                         state->data, 0, 0) < 0)
723                                 err("i2c transfer failed.");
724                         break;
725                 case DW2102_RC_QUERY:
726                         state->data[0] = 0x10;
727                         if (dvb_usb_generic_rw(d, state->data, 1,
728                                         state->data, 2, 0) < 0)
729                                 err("i2c transfer failed.");
730                         msg[0].buf[1] = state->data[0];
731                         msg[0].buf[0] = state->data[1];
732                         break;
733                 default:
734                         if (3 + msg[0].len > sizeof(state->data)) {
735                                 warn("i2c wr: len=%d is too big!\n",
736                                      msg[0].len);
737                                 num = -EOPNOTSUPP;
738                                 break;
739                         }
740
741                         /* always i2c write*/
742                         state->data[0] = 0x08;
743                         state->data[1] = msg[0].addr;
744                         state->data[2] = msg[0].len;
745
746                         memcpy(&state->data[3], msg[0].buf, msg[0].len);
747
748                         if (dvb_usb_generic_rw(d, state->data, msg[0].len + 3,
749                                                 state->data, 1, 0) < 0)
750                                 err("i2c transfer failed.");
751
752                 }
753                 break;
754         case 2:
755                 /* always i2c read */
756                 if (4 + msg[0].len > sizeof(state->data)) {
757                         warn("i2c rd: len=%d is too big!\n",
758                              msg[0].len);
759                         num = -EOPNOTSUPP;
760                         break;
761                 }
762                 if (1 + msg[1].len > sizeof(state->data)) {
763                         warn("i2c rd: len=%d is too big!\n",
764                              msg[1].len);
765                         num = -EOPNOTSUPP;
766                         break;
767                 }
768
769                 state->data[0] = 0x09;
770                 state->data[1] = msg[0].len;
771                 state->data[2] = msg[1].len;
772                 state->data[3] = msg[0].addr;
773                 memcpy(&state->data[4], msg[0].buf, msg[0].len);
774
775                 if (dvb_usb_generic_rw(d, state->data, msg[0].len + 4,
776                                         state->data, msg[1].len + 1, 0) < 0)
777                         err("i2c transfer failed.");
778
779                 memcpy(msg[1].buf, &state->data[1], msg[1].len);
780                 break;
781         default:
782                 warn("more than 2 i2c messages at a time is not handled yet.");
783                 break;
784         }
785         mutex_unlock(&d->data_mutex);
786         mutex_unlock(&d->i2c_mutex);
787         return num;
788 }
789
790 static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
791 {
792         return I2C_FUNC_I2C;
793 }
794
795 static struct i2c_algorithm dw2102_i2c_algo = {
796         .master_xfer = dw2102_i2c_transfer,
797         .functionality = dw210x_i2c_func,
798 };
799
800 static struct i2c_algorithm dw2102_serit_i2c_algo = {
801         .master_xfer = dw2102_serit_i2c_transfer,
802         .functionality = dw210x_i2c_func,
803 };
804
805 static struct i2c_algorithm dw2102_earda_i2c_algo = {
806         .master_xfer = dw2102_earda_i2c_transfer,
807         .functionality = dw210x_i2c_func,
808 };
809
810 static struct i2c_algorithm dw2104_i2c_algo = {
811         .master_xfer = dw2104_i2c_transfer,
812         .functionality = dw210x_i2c_func,
813 };
814
815 static struct i2c_algorithm dw3101_i2c_algo = {
816         .master_xfer = dw3101_i2c_transfer,
817         .functionality = dw210x_i2c_func,
818 };
819
820 static struct i2c_algorithm s6x0_i2c_algo = {
821         .master_xfer = s6x0_i2c_transfer,
822         .functionality = dw210x_i2c_func,
823 };
824
825 static struct i2c_algorithm su3000_i2c_algo = {
826         .master_xfer = su3000_i2c_transfer,
827         .functionality = dw210x_i2c_func,
828 };
829
830 static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
831 {
832         int i;
833         u8 ibuf[] = {0, 0};
834         u8 eeprom[256], eepromline[16];
835
836         for (i = 0; i < 256; i++) {
837                 if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
838                         err("read eeprom failed.");
839                         return -1;
840                 } else {
841                         eepromline[i%16] = ibuf[0];
842                         eeprom[i] = ibuf[0];
843                 }
844                 if ((i % 16) == 15) {
845                         deb_xfer("%02x: ", i - 15);
846                         debug_dump(eepromline, 16, deb_xfer);
847                 }
848         }
849
850         memcpy(mac, eeprom + 8, 6);
851         return 0;
852 };
853
854 static int s6x0_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
855 {
856         int i, ret;
857         u8 ibuf[] = { 0 }, obuf[] = { 0 };
858         u8 eeprom[256], eepromline[16];
859         struct i2c_msg msg[] = {
860                 {
861                         .addr = 0xa0 >> 1,
862                         .flags = 0,
863                         .buf = obuf,
864                         .len = 1,
865                 }, {
866                         .addr = 0xa0 >> 1,
867                         .flags = I2C_M_RD,
868                         .buf = ibuf,
869                         .len = 1,
870                 }
871         };
872
873         for (i = 0; i < 256; i++) {
874                 obuf[0] = i;
875                 ret = s6x0_i2c_transfer(&d->i2c_adap, msg, 2);
876                 if (ret != 2) {
877                         err("read eeprom failed.");
878                         return -1;
879                 } else {
880                         eepromline[i % 16] = ibuf[0];
881                         eeprom[i] = ibuf[0];
882                 }
883
884                 if ((i % 16) == 15) {
885                         deb_xfer("%02x: ", i - 15);
886                         debug_dump(eepromline, 16, deb_xfer);
887                 }
888         }
889
890         memcpy(mac, eeprom + 16, 6);
891         return 0;
892 };
893
894 static int su3000_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
895 {
896         static u8 command_start[] = {0x00};
897         static u8 command_stop[] = {0x01};
898         struct i2c_msg msg = {
899                 .addr = SU3000_STREAM_CTRL,
900                 .flags = 0,
901                 .buf = onoff ? command_start : command_stop,
902                 .len = 1
903         };
904
905         i2c_transfer(&adap->dev->i2c_adap, &msg, 1);
906
907         return 0;
908 }
909
910 static int su3000_power_ctrl(struct dvb_usb_device *d, int i)
911 {
912         struct dw2102_state *state = (struct dw2102_state *)d->priv;
913         int ret = 0;
914
915         info("%s: %d, initialized %d", __func__, i, state->initialized);
916
917         if (i && !state->initialized) {
918                 mutex_lock(&d->data_mutex);
919
920                 state->data[0] = 0xde;
921                 state->data[1] = 0;
922
923                 state->initialized = 1;
924                 /* reset board */
925                 ret = dvb_usb_generic_rw(d, state->data, 2, NULL, 0, 0);
926                 mutex_unlock(&d->data_mutex);
927         }
928
929         return ret;
930 }
931
932 static int su3000_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
933 {
934         int i;
935         u8 obuf[] = { 0x1f, 0xf0 };
936         u8 ibuf[] = { 0 };
937         struct i2c_msg msg[] = {
938                 {
939                         .addr = 0x51,
940                         .flags = 0,
941                         .buf = obuf,
942                         .len = 2,
943                 }, {
944                         .addr = 0x51,
945                         .flags = I2C_M_RD,
946                         .buf = ibuf,
947                         .len = 1,
948
949                 }
950         };
951
952         for (i = 0; i < 6; i++) {
953                 obuf[1] = 0xf0 + i;
954                 if (i2c_transfer(&d->i2c_adap, msg, 2) != 2)
955                         break;
956                 else
957                         mac[i] = ibuf[0];
958         }
959
960         return 0;
961 }
962
963 static int su3000_identify_state(struct usb_device *udev,
964                                  struct dvb_usb_device_properties *props,
965                                  struct dvb_usb_device_description **desc,
966                                  int *cold)
967 {
968         info("%s", __func__);
969
970         *cold = 0;
971         return 0;
972 }
973
974 static int dw210x_set_voltage(struct dvb_frontend *fe,
975                               enum fe_sec_voltage voltage)
976 {
977         static u8 command_13v[] = {0x00, 0x01};
978         static u8 command_18v[] = {0x01, 0x01};
979         static u8 command_off[] = {0x00, 0x00};
980         struct i2c_msg msg = {
981                 .addr = DW2102_VOLTAGE_CTRL,
982                 .flags = 0,
983                 .buf = command_off,
984                 .len = 2,
985         };
986
987         struct dvb_usb_adapter *udev_adap =
988                 (struct dvb_usb_adapter *)(fe->dvb->priv);
989         if (voltage == SEC_VOLTAGE_18)
990                 msg.buf = command_18v;
991         else if (voltage == SEC_VOLTAGE_13)
992                 msg.buf = command_13v;
993
994         i2c_transfer(&udev_adap->dev->i2c_adap, &msg, 1);
995
996         return 0;
997 }
998
999 static int s660_set_voltage(struct dvb_frontend *fe,
1000                             enum fe_sec_voltage voltage)
1001 {
1002         struct dvb_usb_adapter *d =
1003                 (struct dvb_usb_adapter *)(fe->dvb->priv);
1004         struct dw2102_state *st = (struct dw2102_state *)d->dev->priv;
1005
1006         dw210x_set_voltage(fe, voltage);
1007         if (st->old_set_voltage)
1008                 st->old_set_voltage(fe, voltage);
1009
1010         return 0;
1011 }
1012
1013 static void dw210x_led_ctrl(struct dvb_frontend *fe, int offon)
1014 {
1015         static u8 led_off[] = { 0 };
1016         static u8 led_on[] = { 1 };
1017         struct i2c_msg msg = {
1018                 .addr = DW2102_LED_CTRL,
1019                 .flags = 0,
1020                 .buf = led_off,
1021                 .len = 1
1022         };
1023         struct dvb_usb_adapter *udev_adap =
1024                 (struct dvb_usb_adapter *)(fe->dvb->priv);
1025
1026         if (offon)
1027                 msg.buf = led_on;
1028         i2c_transfer(&udev_adap->dev->i2c_adap, &msg, 1);
1029 }
1030
1031 static int tt_s2_4600_read_status(struct dvb_frontend *fe,
1032                                   enum fe_status *status)
1033 {
1034         struct dvb_usb_adapter *d =
1035                 (struct dvb_usb_adapter *)(fe->dvb->priv);
1036         struct dw2102_state *st = (struct dw2102_state *)d->dev->priv;
1037         int ret;
1038
1039         ret = st->fe_read_status(fe, status);
1040
1041         /* resync slave fifo when signal change from unlock to lock */
1042         if ((*status & FE_HAS_LOCK) && (!st->last_lock))
1043                 su3000_streaming_ctrl(d, 1);
1044
1045         st->last_lock = (*status & FE_HAS_LOCK) ? 1 : 0;
1046         return ret;
1047 }
1048
1049 static struct stv0299_config sharp_z0194a_config = {
1050         .demod_address = 0x68,
1051         .inittab = sharp_z0194a_inittab,
1052         .mclk = 88000000UL,
1053         .invert = 1,
1054         .skip_reinit = 0,
1055         .lock_output = STV0299_LOCKOUTPUT_1,
1056         .volt13_op0_op1 = STV0299_VOLT13_OP1,
1057         .min_delay_ms = 100,
1058         .set_symbol_rate = sharp_z0194a_set_symbol_rate,
1059 };
1060
1061 static struct cx24116_config dw2104_config = {
1062         .demod_address = 0x55,
1063         .mpg_clk_pos_pol = 0x01,
1064 };
1065
1066 static struct si21xx_config serit_sp1511lhb_config = {
1067         .demod_address = 0x68,
1068         .min_delay_ms = 100,
1069
1070 };
1071
1072 static struct tda10023_config dw3101_tda10023_config = {
1073         .demod_address = 0x0c,
1074         .invert = 1,
1075 };
1076
1077 static struct mt312_config zl313_config = {
1078         .demod_address = 0x0e,
1079 };
1080
1081 static struct ds3000_config dw2104_ds3000_config = {
1082         .demod_address = 0x68,
1083 };
1084
1085 static struct ts2020_config dw2104_ts2020_config = {
1086         .tuner_address = 0x60,
1087         .clk_out_div = 1,
1088         .frequency_div = 1060000,
1089 };
1090
1091 static struct ds3000_config s660_ds3000_config = {
1092         .demod_address = 0x68,
1093         .ci_mode = 1,
1094         .set_lock_led = dw210x_led_ctrl,
1095 };
1096
1097 static struct ts2020_config s660_ts2020_config = {
1098         .tuner_address = 0x60,
1099         .clk_out_div = 1,
1100         .frequency_div = 1146000,
1101 };
1102
1103 static struct stv0900_config dw2104a_stv0900_config = {
1104         .demod_address = 0x6a,
1105         .demod_mode = 0,
1106         .xtal = 27000000,
1107         .clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
1108         .diseqc_mode = 2,/* 2/3 PWM */
1109         .tun1_maddress = 0,/* 0x60 */
1110         .tun1_adc = 0,/* 2 Vpp */
1111         .path1_mode = 3,
1112 };
1113
1114 static struct stb6100_config dw2104a_stb6100_config = {
1115         .tuner_address = 0x60,
1116         .refclock = 27000000,
1117 };
1118
1119 static struct stv0900_config dw2104_stv0900_config = {
1120         .demod_address = 0x68,
1121         .demod_mode = 0,
1122         .xtal = 8000000,
1123         .clkmode = 3,
1124         .diseqc_mode = 2,
1125         .tun1_maddress = 0,
1126         .tun1_adc = 1,/* 1 Vpp */
1127         .path1_mode = 3,
1128 };
1129
1130 static struct stv6110_config dw2104_stv6110_config = {
1131         .i2c_address = 0x60,
1132         .mclk = 16000000,
1133         .clk_div = 1,
1134 };
1135
1136 static struct stv0900_config prof_7500_stv0900_config = {
1137         .demod_address = 0x6a,
1138         .demod_mode = 0,
1139         .xtal = 27000000,
1140         .clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
1141         .diseqc_mode = 2,/* 2/3 PWM */
1142         .tun1_maddress = 0,/* 0x60 */
1143         .tun1_adc = 0,/* 2 Vpp */
1144         .path1_mode = 3,
1145         .tun1_type = 3,
1146         .set_lock_led = dw210x_led_ctrl,
1147 };
1148
1149 static struct ds3000_config su3000_ds3000_config = {
1150         .demod_address = 0x68,
1151         .ci_mode = 1,
1152         .set_lock_led = dw210x_led_ctrl,
1153 };
1154
1155 static struct cxd2820r_config cxd2820r_config = {
1156         .i2c_address = 0x6c, /* (0xd8 >> 1) */
1157         .ts_mode = 0x38,
1158         .ts_clock_inv = 1,
1159 };
1160
1161 static struct tda18271_config tda18271_config = {
1162         .output_opt = TDA18271_OUTPUT_LT_OFF,
1163         .gate = TDA18271_GATE_DIGITAL,
1164 };
1165
1166 static u8 m88rs2000_inittab[] = {
1167         DEMOD_WRITE, 0x9a, 0x30,
1168         DEMOD_WRITE, 0x00, 0x01,
1169         WRITE_DELAY, 0x19, 0x00,
1170         DEMOD_WRITE, 0x00, 0x00,
1171         DEMOD_WRITE, 0x9a, 0xb0,
1172         DEMOD_WRITE, 0x81, 0xc1,
1173         DEMOD_WRITE, 0x81, 0x81,
1174         DEMOD_WRITE, 0x86, 0xc6,
1175         DEMOD_WRITE, 0x9a, 0x30,
1176         DEMOD_WRITE, 0xf0, 0x80,
1177         DEMOD_WRITE, 0xf1, 0xbf,
1178         DEMOD_WRITE, 0xb0, 0x45,
1179         DEMOD_WRITE, 0xb2, 0x01,
1180         DEMOD_WRITE, 0x9a, 0xb0,
1181         0xff, 0xaa, 0xff
1182 };
1183
1184 static struct m88rs2000_config s421_m88rs2000_config = {
1185         .demod_addr = 0x68,
1186         .inittab = m88rs2000_inittab,
1187 };
1188
1189 static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
1190 {
1191         struct dvb_tuner_ops *tuner_ops = NULL;
1192
1193         if (demod_probe & 4) {
1194                 d->fe_adap[0].fe = dvb_attach(stv0900_attach, &dw2104a_stv0900_config,
1195                                 &d->dev->i2c_adap, 0);
1196                 if (d->fe_adap[0].fe != NULL) {
1197                         if (dvb_attach(stb6100_attach, d->fe_adap[0].fe,
1198                                         &dw2104a_stb6100_config,
1199                                         &d->dev->i2c_adap)) {
1200                                 tuner_ops = &d->fe_adap[0].fe->ops.tuner_ops;
1201                                 tuner_ops->set_frequency = stb6100_set_freq;
1202                                 tuner_ops->get_frequency = stb6100_get_freq;
1203                                 tuner_ops->set_bandwidth = stb6100_set_bandw;
1204                                 tuner_ops->get_bandwidth = stb6100_get_bandw;
1205                                 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1206                                 info("Attached STV0900+STB6100!");
1207                                 return 0;
1208                         }
1209                 }
1210         }
1211
1212         if (demod_probe & 2) {
1213                 d->fe_adap[0].fe = dvb_attach(stv0900_attach, &dw2104_stv0900_config,
1214                                 &d->dev->i2c_adap, 0);
1215                 if (d->fe_adap[0].fe != NULL) {
1216                         if (dvb_attach(stv6110_attach, d->fe_adap[0].fe,
1217                                         &dw2104_stv6110_config,
1218                                         &d->dev->i2c_adap)) {
1219                                 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1220                                 info("Attached STV0900+STV6110A!");
1221                                 return 0;
1222                         }
1223                 }
1224         }
1225
1226         if (demod_probe & 1) {
1227                 d->fe_adap[0].fe = dvb_attach(cx24116_attach, &dw2104_config,
1228                                 &d->dev->i2c_adap);
1229                 if (d->fe_adap[0].fe != NULL) {
1230                         d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1231                         info("Attached cx24116!");
1232                         return 0;
1233                 }
1234         }
1235
1236         d->fe_adap[0].fe = dvb_attach(ds3000_attach, &dw2104_ds3000_config,
1237                         &d->dev->i2c_adap);
1238         if (d->fe_adap[0].fe != NULL) {
1239                 dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1240                         &dw2104_ts2020_config, &d->dev->i2c_adap);
1241                 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1242                 info("Attached DS3000!");
1243                 return 0;
1244         }
1245
1246         return -EIO;
1247 }
1248
1249 static struct dvb_usb_device_properties dw2102_properties;
1250 static struct dvb_usb_device_properties dw2104_properties;
1251 static struct dvb_usb_device_properties s6x0_properties;
1252
1253 static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
1254 {
1255         if (dw2102_properties.i2c_algo == &dw2102_serit_i2c_algo) {
1256                 /*dw2102_properties.adapter->tuner_attach = NULL;*/
1257                 d->fe_adap[0].fe = dvb_attach(si21xx_attach, &serit_sp1511lhb_config,
1258                                         &d->dev->i2c_adap);
1259                 if (d->fe_adap[0].fe != NULL) {
1260                         d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1261                         info("Attached si21xx!");
1262                         return 0;
1263                 }
1264         }
1265
1266         if (dw2102_properties.i2c_algo == &dw2102_earda_i2c_algo) {
1267                 d->fe_adap[0].fe = dvb_attach(stv0288_attach, &earda_config,
1268                                         &d->dev->i2c_adap);
1269                 if (d->fe_adap[0].fe != NULL) {
1270                         if (dvb_attach(stb6000_attach, d->fe_adap[0].fe, 0x61,
1271                                         &d->dev->i2c_adap)) {
1272                                 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1273                                 info("Attached stv0288!");
1274                                 return 0;
1275                         }
1276                 }
1277         }
1278
1279         if (dw2102_properties.i2c_algo == &dw2102_i2c_algo) {
1280                 /*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
1281                 d->fe_adap[0].fe = dvb_attach(stv0299_attach, &sharp_z0194a_config,
1282                                         &d->dev->i2c_adap);
1283                 if (d->fe_adap[0].fe != NULL) {
1284                         d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1285                         info("Attached stv0299!");
1286                         return 0;
1287                 }
1288         }
1289         return -EIO;
1290 }
1291
1292 static int dw3101_frontend_attach(struct dvb_usb_adapter *d)
1293 {
1294         d->fe_adap[0].fe = dvb_attach(tda10023_attach, &dw3101_tda10023_config,
1295                                 &d->dev->i2c_adap, 0x48);
1296         if (d->fe_adap[0].fe != NULL) {
1297                 info("Attached tda10023!");
1298                 return 0;
1299         }
1300         return -EIO;
1301 }
1302
1303 static int zl100313_frontend_attach(struct dvb_usb_adapter *d)
1304 {
1305         d->fe_adap[0].fe = dvb_attach(mt312_attach, &zl313_config,
1306                         &d->dev->i2c_adap);
1307         if (d->fe_adap[0].fe != NULL) {
1308                 if (dvb_attach(zl10039_attach, d->fe_adap[0].fe, 0x60,
1309                                 &d->dev->i2c_adap)) {
1310                         d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1311                         info("Attached zl100313+zl10039!");
1312                         return 0;
1313                 }
1314         }
1315
1316         return -EIO;
1317 }
1318
1319 static int stv0288_frontend_attach(struct dvb_usb_adapter *d)
1320 {
1321         u8 obuf[] = {7, 1};
1322
1323         d->fe_adap[0].fe = dvb_attach(stv0288_attach, &earda_config,
1324                         &d->dev->i2c_adap);
1325
1326         if (d->fe_adap[0].fe == NULL)
1327                 return -EIO;
1328
1329         if (NULL == dvb_attach(stb6000_attach, d->fe_adap[0].fe, 0x61, &d->dev->i2c_adap))
1330                 return -EIO;
1331
1332         d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1333
1334         dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);
1335
1336         info("Attached stv0288+stb6000!");
1337
1338         return 0;
1339
1340 }
1341
1342 static int ds3000_frontend_attach(struct dvb_usb_adapter *d)
1343 {
1344         struct dw2102_state *st = d->dev->priv;
1345         u8 obuf[] = {7, 1};
1346
1347         d->fe_adap[0].fe = dvb_attach(ds3000_attach, &s660_ds3000_config,
1348                         &d->dev->i2c_adap);
1349
1350         if (d->fe_adap[0].fe == NULL)
1351                 return -EIO;
1352
1353         dvb_attach(ts2020_attach, d->fe_adap[0].fe, &s660_ts2020_config,
1354                 &d->dev->i2c_adap);
1355
1356         st->old_set_voltage = d->fe_adap[0].fe->ops.set_voltage;
1357         d->fe_adap[0].fe->ops.set_voltage = s660_set_voltage;
1358
1359         dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);
1360
1361         info("Attached ds3000+ts2020!");
1362
1363         return 0;
1364 }
1365
1366 static int prof_7500_frontend_attach(struct dvb_usb_adapter *d)
1367 {
1368         u8 obuf[] = {7, 1};
1369
1370         d->fe_adap[0].fe = dvb_attach(stv0900_attach, &prof_7500_stv0900_config,
1371                                         &d->dev->i2c_adap, 0);
1372         if (d->fe_adap[0].fe == NULL)
1373                 return -EIO;
1374
1375         d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1376
1377         dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);
1378
1379         info("Attached STV0900+STB6100A!");
1380
1381         return 0;
1382 }
1383
1384 static int su3000_frontend_attach(struct dvb_usb_adapter *adap)
1385 {
1386         struct dvb_usb_device *d = adap->dev;
1387         struct dw2102_state *state = d->priv;
1388
1389         mutex_lock(&d->data_mutex);
1390
1391         state->data[0] = 0xe;
1392         state->data[1] = 0x80;
1393         state->data[2] = 0;
1394
1395         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1396                 err("command 0x0e transfer failed.");
1397
1398         state->data[0] = 0xe;
1399         state->data[1] = 0x02;
1400         state->data[2] = 1;
1401
1402         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1403                 err("command 0x0e transfer failed.");
1404         msleep(300);
1405
1406         state->data[0] = 0xe;
1407         state->data[1] = 0x83;
1408         state->data[2] = 0;
1409
1410         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1411                 err("command 0x0e transfer failed.");
1412
1413         state->data[0] = 0xe;
1414         state->data[1] = 0x83;
1415         state->data[2] = 1;
1416
1417         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1418                 err("command 0x0e transfer failed.");
1419
1420         state->data[0] = 0x51;
1421
1422         if (dvb_usb_generic_rw(d, state->data, 1, state->data, 1, 0) < 0)
1423                 err("command 0x51 transfer failed.");
1424
1425         mutex_unlock(&d->data_mutex);
1426
1427         adap->fe_adap[0].fe = dvb_attach(ds3000_attach, &su3000_ds3000_config,
1428                                         &d->i2c_adap);
1429         if (adap->fe_adap[0].fe == NULL)
1430                 return -EIO;
1431
1432         if (dvb_attach(ts2020_attach, adap->fe_adap[0].fe,
1433                                 &dw2104_ts2020_config,
1434                                 &d->i2c_adap)) {
1435                 info("Attached DS3000/TS2020!");
1436                 return 0;
1437         }
1438
1439         info("Failed to attach DS3000/TS2020!");
1440         return -EIO;
1441 }
1442
1443 static int t220_frontend_attach(struct dvb_usb_adapter *adap)
1444 {
1445         struct dvb_usb_device *d = adap->dev;
1446         struct dw2102_state *state = d->priv;
1447
1448         mutex_lock(&d->data_mutex);
1449
1450         state->data[0] = 0xe;
1451         state->data[1] = 0x87;
1452         state->data[2] = 0x0;
1453
1454         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1455                 err("command 0x0e transfer failed.");
1456
1457         state->data[0] = 0xe;
1458         state->data[1] = 0x86;
1459         state->data[2] = 1;
1460
1461         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1462                 err("command 0x0e transfer failed.");
1463
1464         state->data[0] = 0xe;
1465         state->data[1] = 0x80;
1466         state->data[2] = 0;
1467
1468         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1469                 err("command 0x0e transfer failed.");
1470
1471         msleep(50);
1472
1473         state->data[0] = 0xe;
1474         state->data[1] = 0x80;
1475         state->data[2] = 1;
1476
1477         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1478                 err("command 0x0e transfer failed.");
1479
1480         state->data[0] = 0x51;
1481
1482         if (dvb_usb_generic_rw(d, state->data, 1, state->data, 1, 0) < 0)
1483                 err("command 0x51 transfer failed.");
1484
1485         mutex_unlock(&d->data_mutex);
1486
1487         adap->fe_adap[0].fe = dvb_attach(cxd2820r_attach, &cxd2820r_config,
1488                                         &d->i2c_adap, NULL);
1489         if (adap->fe_adap[0].fe != NULL) {
1490                 if (dvb_attach(tda18271_attach, adap->fe_adap[0].fe, 0x60,
1491                                         &d->i2c_adap, &tda18271_config)) {
1492                         info("Attached TDA18271HD/CXD2820R!");
1493                         return 0;
1494                 }
1495         }
1496
1497         info("Failed to attach TDA18271HD/CXD2820R!");
1498         return -EIO;
1499 }
1500
1501 static int m88rs2000_frontend_attach(struct dvb_usb_adapter *adap)
1502 {
1503         struct dvb_usb_device *d = adap->dev;
1504         struct dw2102_state *state = d->priv;
1505
1506         mutex_lock(&d->data_mutex);
1507
1508         state->data[0] = 0x51;
1509
1510         if (dvb_usb_generic_rw(d, state->data, 1, state->data, 1, 0) < 0)
1511                 err("command 0x51 transfer failed.");
1512
1513         mutex_unlock(&d->data_mutex);
1514
1515         adap->fe_adap[0].fe = dvb_attach(m88rs2000_attach,
1516                                         &s421_m88rs2000_config,
1517                                         &d->i2c_adap);
1518
1519         if (adap->fe_adap[0].fe == NULL)
1520                 return -EIO;
1521
1522         if (dvb_attach(ts2020_attach, adap->fe_adap[0].fe,
1523                                 &dw2104_ts2020_config,
1524                                 &d->i2c_adap)) {
1525                 info("Attached RS2000/TS2020!");
1526                 return 0;
1527         }
1528
1529         info("Failed to attach RS2000/TS2020!");
1530         return -EIO;
1531 }
1532
1533 static int tt_s2_4600_frontend_attach(struct dvb_usb_adapter *adap)
1534 {
1535         struct dvb_usb_device *d = adap->dev;
1536         struct dw2102_state *state = d->priv;
1537         struct i2c_adapter *i2c_adapter;
1538         struct i2c_client *client;
1539         struct i2c_board_info board_info;
1540         struct m88ds3103_platform_data m88ds3103_pdata = {};
1541         struct ts2020_config ts2020_config = {};
1542
1543         mutex_lock(&d->data_mutex);
1544
1545         state->data[0] = 0xe;
1546         state->data[1] = 0x80;
1547         state->data[2] = 0x0;
1548
1549         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1550                 err("command 0x0e transfer failed.");
1551
1552         state->data[0] = 0xe;
1553         state->data[1] = 0x02;
1554         state->data[2] = 1;
1555
1556         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1557                 err("command 0x0e transfer failed.");
1558         msleep(300);
1559
1560         state->data[0] = 0xe;
1561         state->data[1] = 0x83;
1562         state->data[2] = 0;
1563
1564         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1565                 err("command 0x0e transfer failed.");
1566
1567         state->data[0] = 0xe;
1568         state->data[1] = 0x83;
1569         state->data[2] = 1;
1570
1571         if (dvb_usb_generic_rw(d, state->data, 3, state->data, 1, 0) < 0)
1572                 err("command 0x0e transfer failed.");
1573
1574         state->data[0] = 0x51;
1575
1576         if (dvb_usb_generic_rw(d, state->data, 1, state->data, 1, 0) < 0)
1577                 err("command 0x51 transfer failed.");
1578
1579         mutex_unlock(&d->data_mutex);
1580
1581         /* attach demod */
1582         m88ds3103_pdata.clk = 27000000;
1583         m88ds3103_pdata.i2c_wr_max = 33;
1584         m88ds3103_pdata.ts_mode = M88DS3103_TS_CI;
1585         m88ds3103_pdata.ts_clk = 16000;
1586         m88ds3103_pdata.ts_clk_pol = 0;
1587         m88ds3103_pdata.spec_inv = 0;
1588         m88ds3103_pdata.agc = 0x99;
1589         m88ds3103_pdata.agc_inv = 0;
1590         m88ds3103_pdata.clk_out = M88DS3103_CLOCK_OUT_ENABLED;
1591         m88ds3103_pdata.envelope_mode = 0;
1592         m88ds3103_pdata.lnb_hv_pol = 1;
1593         m88ds3103_pdata.lnb_en_pol = 0;
1594         memset(&board_info, 0, sizeof(board_info));
1595         strlcpy(board_info.type, "m88ds3103", I2C_NAME_SIZE);
1596         board_info.addr = 0x68;
1597         board_info.platform_data = &m88ds3103_pdata;
1598         request_module("m88ds3103");
1599         client = i2c_new_device(&d->i2c_adap, &board_info);
1600         if (client == NULL || client->dev.driver == NULL)
1601                 return -ENODEV;
1602         if (!try_module_get(client->dev.driver->owner)) {
1603                 i2c_unregister_device(client);
1604                 return -ENODEV;
1605         }
1606         adap->fe_adap[0].fe = m88ds3103_pdata.get_dvb_frontend(client);
1607         i2c_adapter = m88ds3103_pdata.get_i2c_adapter(client);
1608
1609         state->i2c_client_demod = client;
1610
1611         /* attach tuner */
1612         ts2020_config.fe = adap->fe_adap[0].fe;
1613         memset(&board_info, 0, sizeof(board_info));
1614         strlcpy(board_info.type, "ts2022", I2C_NAME_SIZE);
1615         board_info.addr = 0x60;
1616         board_info.platform_data = &ts2020_config;
1617         request_module("ts2020");
1618         client = i2c_new_device(i2c_adapter, &board_info);
1619
1620         if (client == NULL || client->dev.driver == NULL) {
1621                 dvb_frontend_detach(adap->fe_adap[0].fe);
1622                 return -ENODEV;
1623         }
1624
1625         if (!try_module_get(client->dev.driver->owner)) {
1626                 i2c_unregister_device(client);
1627                 dvb_frontend_detach(adap->fe_adap[0].fe);
1628                 return -ENODEV;
1629         }
1630
1631         /* delegate signal strength measurement to tuner */
1632         adap->fe_adap[0].fe->ops.read_signal_strength =
1633                         adap->fe_adap[0].fe->ops.tuner_ops.get_rf_strength;
1634
1635         state->i2c_client_tuner = client;
1636
1637         /* hook fe: need to resync the slave fifo when signal locks */
1638         state->fe_read_status = adap->fe_adap[0].fe->ops.read_status;
1639         adap->fe_adap[0].fe->ops.read_status = tt_s2_4600_read_status;
1640
1641         state->last_lock = 0;
1642
1643         return 0;
1644 }
1645
1646 static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
1647 {
1648         dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1649                 &adap->dev->i2c_adap, DVB_PLL_OPERA1);
1650         return 0;
1651 }
1652
1653 static int dw3101_tuner_attach(struct dvb_usb_adapter *adap)
1654 {
1655         dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1656                 &adap->dev->i2c_adap, DVB_PLL_TUA6034);
1657
1658         return 0;
1659 }
1660
1661 static int dw2102_rc_query(struct dvb_usb_device *d)
1662 {
1663         u8 key[2];
1664         struct i2c_msg msg = {
1665                 .addr = DW2102_RC_QUERY,
1666                 .flags = I2C_M_RD,
1667                 .buf = key,
1668                 .len = 2
1669         };
1670
1671         if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
1672                 if (msg.buf[0] != 0xff) {
1673                         deb_rc("%s: rc code: %x, %x\n",
1674                                         __func__, key[0], key[1]);
1675                         rc_keydown(d->rc_dev, RC_TYPE_UNKNOWN, key[0], 0);
1676                 }
1677         }
1678
1679         return 0;
1680 }
1681
1682 static int prof_rc_query(struct dvb_usb_device *d)
1683 {
1684         u8 key[2];
1685         struct i2c_msg msg = {
1686                 .addr = DW2102_RC_QUERY,
1687                 .flags = I2C_M_RD,
1688                 .buf = key,
1689                 .len = 2
1690         };
1691
1692         if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
1693                 if (msg.buf[0] != 0xff) {
1694                         deb_rc("%s: rc code: %x, %x\n",
1695                                         __func__, key[0], key[1]);
1696                         rc_keydown(d->rc_dev, RC_TYPE_UNKNOWN, key[0]^0xff, 0);
1697                 }
1698         }
1699
1700         return 0;
1701 }
1702
1703 static int su3000_rc_query(struct dvb_usb_device *d)
1704 {
1705         u8 key[2];
1706         struct i2c_msg msg = {
1707                 .addr = DW2102_RC_QUERY,
1708                 .flags = I2C_M_RD,
1709                 .buf = key,
1710                 .len = 2
1711         };
1712
1713         if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
1714                 if (msg.buf[0] != 0xff) {
1715                         deb_rc("%s: rc code: %x, %x\n",
1716                                         __func__, key[0], key[1]);
1717                         rc_keydown(d->rc_dev, RC_TYPE_RC5,
1718                                    RC_SCANCODE_RC5(key[1], key[0]), 0);
1719                 }
1720         }
1721
1722         return 0;
1723 }
1724
1725 enum dw2102_table_entry {
1726         CYPRESS_DW2102,
1727         CYPRESS_DW2101,
1728         CYPRESS_DW2104,
1729         TEVII_S650,
1730         TERRATEC_CINERGY_S,
1731         CYPRESS_DW3101,
1732         TEVII_S630,
1733         PROF_1100,
1734         TEVII_S660,
1735         PROF_7500,
1736         GENIATECH_SU3000,
1737         TERRATEC_CINERGY_S2,
1738         TEVII_S480_1,
1739         TEVII_S480_2,
1740         X3M_SPC1400HD,
1741         TEVII_S421,
1742         TEVII_S632,
1743         TERRATEC_CINERGY_S2_R2,
1744         TERRATEC_CINERGY_S2_R3,
1745         GOTVIEW_SAT_HD,
1746         GENIATECH_T220,
1747         TECHNOTREND_S2_4600,
1748         TEVII_S482_1,
1749         TEVII_S482_2,
1750         TERRATEC_CINERGY_S2_BOX,
1751         TEVII_S662
1752 };
1753
1754 static struct usb_device_id dw2102_table[] = {
1755         [CYPRESS_DW2102] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
1756         [CYPRESS_DW2101] = {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
1757         [CYPRESS_DW2104] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2104)},
1758         [TEVII_S650] = {USB_DEVICE(0x9022, USB_PID_TEVII_S650)},
1759         [TERRATEC_CINERGY_S] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S)},
1760         [CYPRESS_DW3101] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW3101)},
1761         [TEVII_S630] = {USB_DEVICE(0x9022, USB_PID_TEVII_S630)},
1762         [PROF_1100] = {USB_DEVICE(0x3011, USB_PID_PROF_1100)},
1763         [TEVII_S660] = {USB_DEVICE(0x9022, USB_PID_TEVII_S660)},
1764         [PROF_7500] = {USB_DEVICE(0x3034, 0x7500)},
1765         [GENIATECH_SU3000] = {USB_DEVICE(0x1f4d, 0x3000)},
1766         [TERRATEC_CINERGY_S2] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R1)},
1767         [TEVII_S480_1] = {USB_DEVICE(0x9022, USB_PID_TEVII_S480_1)},
1768         [TEVII_S480_2] = {USB_DEVICE(0x9022, USB_PID_TEVII_S480_2)},
1769         [X3M_SPC1400HD] = {USB_DEVICE(0x1f4d, 0x3100)},
1770         [TEVII_S421] = {USB_DEVICE(0x9022, USB_PID_TEVII_S421)},
1771         [TEVII_S632] = {USB_DEVICE(0x9022, USB_PID_TEVII_S632)},
1772         [TERRATEC_CINERGY_S2_R2] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R2)},
1773         [TERRATEC_CINERGY_S2_R3] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R3)},
1774         [GOTVIEW_SAT_HD] = {USB_DEVICE(0x1FE1, USB_PID_GOTVIEW_SAT_HD)},
1775         [GENIATECH_T220] = {USB_DEVICE(0x1f4d, 0xD220)},
1776         [TECHNOTREND_S2_4600] = {USB_DEVICE(USB_VID_TECHNOTREND,
1777                 USB_PID_TECHNOTREND_CONNECT_S2_4600)},
1778         [TEVII_S482_1] = {USB_DEVICE(0x9022, 0xd483)},
1779         [TEVII_S482_2] = {USB_DEVICE(0x9022, 0xd484)},
1780         [TERRATEC_CINERGY_S2_BOX] = {USB_DEVICE(USB_VID_TERRATEC, 0x0105)},
1781         [TEVII_S662] = {USB_DEVICE(0x9022, USB_PID_TEVII_S662)},
1782         { }
1783 };
1784
1785 MODULE_DEVICE_TABLE(usb, dw2102_table);
1786
1787 static int dw2102_load_firmware(struct usb_device *dev,
1788                         const struct firmware *frmwr)
1789 {
1790         u8 *b, *p;
1791         int ret = 0, i;
1792         u8 reset;
1793         u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
1794         const struct firmware *fw;
1795
1796         switch (le16_to_cpu(dev->descriptor.idProduct)) {
1797         case 0x2101:
1798                 ret = reject_firmware(&fw, DW2101_FIRMWARE, &dev->dev);
1799                 if (ret != 0) {
1800                         err(err_str, DW2101_FIRMWARE);
1801                         return ret;
1802                 }
1803                 break;
1804         default:
1805                 fw = frmwr;
1806                 break;
1807         }
1808         info("start downloading DW210X firmware");
1809         p = kmalloc(fw->size, GFP_KERNEL);
1810         reset = 1;
1811         /*stop the CPU*/
1812         dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
1813         dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
1814
1815         if (p != NULL) {
1816                 memcpy(p, fw->data, fw->size);
1817                 for (i = 0; i < fw->size; i += 0x40) {
1818                         b = (u8 *) p + i;
1819                         if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
1820                                         DW210X_WRITE_MSG) != 0x40) {
1821                                 err("error while transferring firmware");
1822                                 ret = -EINVAL;
1823                                 break;
1824                         }
1825                 }
1826                 /* restart the CPU */
1827                 reset = 0;
1828                 if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
1829                                         DW210X_WRITE_MSG) != 1) {
1830                         err("could not restart the USB controller CPU.");
1831                         ret = -EINVAL;
1832                 }
1833                 if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
1834                                         DW210X_WRITE_MSG) != 1) {
1835                         err("could not restart the USB controller CPU.");
1836                         ret = -EINVAL;
1837                 }
1838                 /* init registers */
1839                 switch (le16_to_cpu(dev->descriptor.idProduct)) {
1840                 case USB_PID_TEVII_S650:
1841                         dw2104_properties.rc.core.rc_codes = RC_MAP_TEVII_NEC;
1842                 case USB_PID_DW2104:
1843                         reset = 1;
1844                         dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
1845                                         DW210X_WRITE_MSG);
1846                         /* break omitted intentionally */
1847                 case USB_PID_DW3101:
1848                         reset = 0;
1849                         dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
1850                                         DW210X_WRITE_MSG);
1851                         break;
1852                 case USB_PID_TERRATEC_CINERGY_S:
1853                 case USB_PID_DW2102:
1854                         dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
1855                                         DW210X_WRITE_MSG);
1856                         dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
1857                                         DW210X_READ_MSG);
1858                         /* check STV0299 frontend  */
1859                         dw210x_op_rw(dev, 0xb5, 0, 0, &reset16[0], 2,
1860                                         DW210X_READ_MSG);
1861                         if ((reset16[0] == 0xa1) || (reset16[0] == 0x80)) {
1862                                 dw2102_properties.i2c_algo = &dw2102_i2c_algo;
1863                                 dw2102_properties.adapter->fe[0].tuner_attach = &dw2102_tuner_attach;
1864                                 break;
1865                         } else {
1866                                 /* check STV0288 frontend  */
1867                                 reset16[0] = 0xd0;
1868                                 reset16[1] = 1;
1869                                 reset16[2] = 0;
1870                                 dw210x_op_rw(dev, 0xc2, 0, 0, &reset16[0], 3,
1871                                                 DW210X_WRITE_MSG);
1872                                 dw210x_op_rw(dev, 0xc3, 0xd1, 0, &reset16[0], 3,
1873                                                 DW210X_READ_MSG);
1874                                 if (reset16[2] == 0x11) {
1875                                         dw2102_properties.i2c_algo = &dw2102_earda_i2c_algo;
1876                                         break;
1877                                 }
1878                         }
1879                 case 0x2101:
1880                         dw210x_op_rw(dev, 0xbc, 0x0030, 0, &reset16[0], 2,
1881                                         DW210X_READ_MSG);
1882                         dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
1883                                         DW210X_READ_MSG);
1884                         dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
1885                                         DW210X_READ_MSG);
1886                         dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
1887                                         DW210X_READ_MSG);
1888                         break;
1889                 }
1890
1891                 msleep(100);
1892                 kfree(p);
1893         }
1894
1895         if (le16_to_cpu(dev->descriptor.idProduct) == 0x2101)
1896                 release_firmware(fw);
1897         return ret;
1898 }
1899
1900 static struct dvb_usb_device_properties dw2102_properties = {
1901         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1902         .usb_ctrl = DEVICE_SPECIFIC,
1903         .firmware = DW2102_FIRMWARE,
1904         .no_reconnect = 1,
1905
1906         .i2c_algo = &dw2102_serit_i2c_algo,
1907
1908         .rc.core = {
1909                 .rc_interval = 150,
1910                 .rc_codes = RC_MAP_DM1105_NEC,
1911                 .module_name = "dw2102",
1912                 .allowed_protos   = RC_BIT_NEC,
1913                 .rc_query = dw2102_rc_query,
1914         },
1915
1916         .generic_bulk_ctrl_endpoint = 0x81,
1917         /* parameter for the MPEG2-data transfer */
1918         .num_adapters = 1,
1919         .download_firmware = dw2102_load_firmware,
1920         .read_mac_address = dw210x_read_mac_address,
1921         .adapter = {
1922                 {
1923                 .num_frontends = 1,
1924                 .fe = {{
1925                         .frontend_attach = dw2102_frontend_attach,
1926                         .stream = {
1927                                 .type = USB_BULK,
1928                                 .count = 8,
1929                                 .endpoint = 0x82,
1930                                 .u = {
1931                                         .bulk = {
1932                                                 .buffersize = 4096,
1933                                         }
1934                                 }
1935                         },
1936                 }},
1937                 }
1938         },
1939         .num_device_descs = 3,
1940         .devices = {
1941                 {"DVBWorld DVB-S 2102 USB2.0",
1942                         {&dw2102_table[CYPRESS_DW2102], NULL},
1943                         {NULL},
1944                 },
1945                 {"DVBWorld DVB-S 2101 USB2.0",
1946                         {&dw2102_table[CYPRESS_DW2101], NULL},
1947                         {NULL},
1948                 },
1949                 {"TerraTec Cinergy S USB",
1950                         {&dw2102_table[TERRATEC_CINERGY_S], NULL},
1951                         {NULL},
1952                 },
1953         }
1954 };
1955
1956 static struct dvb_usb_device_properties dw2104_properties = {
1957         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1958         .usb_ctrl = DEVICE_SPECIFIC,
1959         .firmware = DW2104_FIRMWARE,
1960         .no_reconnect = 1,
1961
1962         .i2c_algo = &dw2104_i2c_algo,
1963         .rc.core = {
1964                 .rc_interval = 150,
1965                 .rc_codes = RC_MAP_DM1105_NEC,
1966                 .module_name = "dw2102",
1967                 .allowed_protos   = RC_BIT_NEC,
1968                 .rc_query = dw2102_rc_query,
1969         },
1970
1971         .generic_bulk_ctrl_endpoint = 0x81,
1972         /* parameter for the MPEG2-data transfer */
1973         .num_adapters = 1,
1974         .download_firmware = dw2102_load_firmware,
1975         .read_mac_address = dw210x_read_mac_address,
1976         .adapter = {
1977                 {
1978                 .num_frontends = 1,
1979                 .fe = {{
1980                         .frontend_attach = dw2104_frontend_attach,
1981                         .stream = {
1982                                 .type = USB_BULK,
1983                                 .count = 8,
1984                                 .endpoint = 0x82,
1985                                 .u = {
1986                                         .bulk = {
1987                                                 .buffersize = 4096,
1988                                         }
1989                                 }
1990                         },
1991                 }},
1992                 }
1993         },
1994         .num_device_descs = 2,
1995         .devices = {
1996                 { "DVBWorld DW2104 USB2.0",
1997                         {&dw2102_table[CYPRESS_DW2104], NULL},
1998                         {NULL},
1999                 },
2000                 { "TeVii S650 USB2.0",
2001                         {&dw2102_table[TEVII_S650], NULL},
2002                         {NULL},
2003                 },
2004         }
2005 };
2006
2007 static struct dvb_usb_device_properties dw3101_properties = {
2008         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2009         .usb_ctrl = DEVICE_SPECIFIC,
2010         .firmware = DW3101_FIRMWARE,
2011         .no_reconnect = 1,
2012
2013         .i2c_algo = &dw3101_i2c_algo,
2014         .rc.core = {
2015                 .rc_interval = 150,
2016                 .rc_codes = RC_MAP_DM1105_NEC,
2017                 .module_name = "dw2102",
2018                 .allowed_protos   = RC_BIT_NEC,
2019                 .rc_query = dw2102_rc_query,
2020         },
2021
2022         .generic_bulk_ctrl_endpoint = 0x81,
2023         /* parameter for the MPEG2-data transfer */
2024         .num_adapters = 1,
2025         .download_firmware = dw2102_load_firmware,
2026         .read_mac_address = dw210x_read_mac_address,
2027         .adapter = {
2028                 {
2029                 .num_frontends = 1,
2030                 .fe = {{
2031                         .frontend_attach = dw3101_frontend_attach,
2032                         .tuner_attach = dw3101_tuner_attach,
2033                         .stream = {
2034                                 .type = USB_BULK,
2035                                 .count = 8,
2036                                 .endpoint = 0x82,
2037                                 .u = {
2038                                         .bulk = {
2039                                                 .buffersize = 4096,
2040                                         }
2041                                 }
2042                         },
2043                 }},
2044                 }
2045         },
2046         .num_device_descs = 1,
2047         .devices = {
2048                 { "DVBWorld DVB-C 3101 USB2.0",
2049                         {&dw2102_table[CYPRESS_DW3101], NULL},
2050                         {NULL},
2051                 },
2052         }
2053 };
2054
2055 static struct dvb_usb_device_properties s6x0_properties = {
2056         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2057         .usb_ctrl = DEVICE_SPECIFIC,
2058         .size_of_priv = sizeof(struct dw2102_state),
2059         .firmware = S630_FIRMWARE,
2060         .no_reconnect = 1,
2061
2062         .i2c_algo = &s6x0_i2c_algo,
2063         .rc.core = {
2064                 .rc_interval = 150,
2065                 .rc_codes = RC_MAP_TEVII_NEC,
2066                 .module_name = "dw2102",
2067                 .allowed_protos   = RC_BIT_NEC,
2068                 .rc_query = dw2102_rc_query,
2069         },
2070
2071         .generic_bulk_ctrl_endpoint = 0x81,
2072         .num_adapters = 1,
2073         .download_firmware = dw2102_load_firmware,
2074         .read_mac_address = s6x0_read_mac_address,
2075         .adapter = {
2076                 {
2077                 .num_frontends = 1,
2078                 .fe = {{
2079                         .frontend_attach = zl100313_frontend_attach,
2080                         .stream = {
2081                                 .type = USB_BULK,
2082                                 .count = 8,
2083                                 .endpoint = 0x82,
2084                                 .u = {
2085                                         .bulk = {
2086                                                 .buffersize = 4096,
2087                                         }
2088                                 }
2089                         },
2090                 }},
2091                 }
2092         },
2093         .num_device_descs = 1,
2094         .devices = {
2095                 {"TeVii S630 USB",
2096                         {&dw2102_table[TEVII_S630], NULL},
2097                         {NULL},
2098                 },
2099         }
2100 };
2101
2102 static struct dvb_usb_device_properties *p1100;
2103 static struct dvb_usb_device_description d1100 = {
2104         "Prof 1100 USB ",
2105         {&dw2102_table[PROF_1100], NULL},
2106         {NULL},
2107 };
2108
2109 static struct dvb_usb_device_properties *s660;
2110 static struct dvb_usb_device_description d660 = {
2111         "TeVii S660 USB",
2112         {&dw2102_table[TEVII_S660], NULL},
2113         {NULL},
2114 };
2115
2116 static struct dvb_usb_device_description d480_1 = {
2117         "TeVii S480.1 USB",
2118         {&dw2102_table[TEVII_S480_1], NULL},
2119         {NULL},
2120 };
2121
2122 static struct dvb_usb_device_description d480_2 = {
2123         "TeVii S480.2 USB",
2124         {&dw2102_table[TEVII_S480_2], NULL},
2125         {NULL},
2126 };
2127
2128 static struct dvb_usb_device_properties *p7500;
2129 static struct dvb_usb_device_description d7500 = {
2130         "Prof 7500 USB DVB-S2",
2131         {&dw2102_table[PROF_7500], NULL},
2132         {NULL},
2133 };
2134
2135 static struct dvb_usb_device_properties *s421;
2136 static struct dvb_usb_device_description d421 = {
2137         "TeVii S421 PCI",
2138         {&dw2102_table[TEVII_S421], NULL},
2139         {NULL},
2140 };
2141
2142 static struct dvb_usb_device_description d632 = {
2143         "TeVii S632 USB",
2144         {&dw2102_table[TEVII_S632], NULL},
2145         {NULL},
2146 };
2147
2148 static struct dvb_usb_device_properties su3000_properties = {
2149         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2150         .usb_ctrl = DEVICE_SPECIFIC,
2151         .size_of_priv = sizeof(struct dw2102_state),
2152         .power_ctrl = su3000_power_ctrl,
2153         .num_adapters = 1,
2154         .identify_state = su3000_identify_state,
2155         .i2c_algo = &su3000_i2c_algo,
2156
2157         .rc.core = {
2158                 .rc_interval = 150,
2159                 .rc_codes = RC_MAP_SU3000,
2160                 .module_name = "dw2102",
2161                 .allowed_protos   = RC_BIT_RC5,
2162                 .rc_query = su3000_rc_query,
2163         },
2164
2165         .read_mac_address = su3000_read_mac_address,
2166
2167         .generic_bulk_ctrl_endpoint = 0x01,
2168
2169         .adapter = {
2170                 {
2171                 .num_frontends = 1,
2172                 .fe = {{
2173                         .streaming_ctrl   = su3000_streaming_ctrl,
2174                         .frontend_attach  = su3000_frontend_attach,
2175                         .stream = {
2176                                 .type = USB_BULK,
2177                                 .count = 8,
2178                                 .endpoint = 0x82,
2179                                 .u = {
2180                                         .bulk = {
2181                                                 .buffersize = 4096,
2182                                         }
2183                                 }
2184                         }
2185                 }},
2186                 }
2187         },
2188         .num_device_descs = 6,
2189         .devices = {
2190                 { "SU3000HD DVB-S USB2.0",
2191                         { &dw2102_table[GENIATECH_SU3000], NULL },
2192                         { NULL },
2193                 },
2194                 { "Terratec Cinergy S2 USB HD",
2195                         { &dw2102_table[TERRATEC_CINERGY_S2], NULL },
2196                         { NULL },
2197                 },
2198                 { "X3M TV SPC1400HD PCI",
2199                         { &dw2102_table[X3M_SPC1400HD], NULL },
2200                         { NULL },
2201                 },
2202                 { "Terratec Cinergy S2 USB HD Rev.2",
2203                         { &dw2102_table[TERRATEC_CINERGY_S2_R2], NULL },
2204                         { NULL },
2205                 },
2206                 { "Terratec Cinergy S2 USB HD Rev.3",
2207                         { &dw2102_table[TERRATEC_CINERGY_S2_R3], NULL },
2208                         { NULL },
2209                 },
2210                 { "GOTVIEW Satellite HD",
2211                         { &dw2102_table[GOTVIEW_SAT_HD], NULL },
2212                         { NULL },
2213                 },
2214         }
2215 };
2216
2217 static struct dvb_usb_device_properties t220_properties = {
2218         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2219         .usb_ctrl = DEVICE_SPECIFIC,
2220         .size_of_priv = sizeof(struct dw2102_state),
2221         .power_ctrl = su3000_power_ctrl,
2222         .num_adapters = 1,
2223         .identify_state = su3000_identify_state,
2224         .i2c_algo = &su3000_i2c_algo,
2225
2226         .rc.core = {
2227                 .rc_interval = 150,
2228                 .rc_codes = RC_MAP_SU3000,
2229                 .module_name = "dw2102",
2230                 .allowed_protos   = RC_BIT_RC5,
2231                 .rc_query = su3000_rc_query,
2232         },
2233
2234         .read_mac_address = su3000_read_mac_address,
2235
2236         .generic_bulk_ctrl_endpoint = 0x01,
2237
2238         .adapter = {
2239                 {
2240                 .num_frontends = 1,
2241                 .fe = { {
2242                         .streaming_ctrl   = su3000_streaming_ctrl,
2243                         .frontend_attach  = t220_frontend_attach,
2244                         .stream = {
2245                                 .type = USB_BULK,
2246                                 .count = 8,
2247                                 .endpoint = 0x82,
2248                                 .u = {
2249                                         .bulk = {
2250                                                 .buffersize = 4096,
2251                                         }
2252                                 }
2253                         }
2254                 } },
2255                 }
2256         },
2257         .num_device_descs = 1,
2258         .devices = {
2259                 { "Geniatech T220 DVB-T/T2 USB2.0",
2260                         { &dw2102_table[GENIATECH_T220], NULL },
2261                         { NULL },
2262                 },
2263         }
2264 };
2265
2266 static struct dvb_usb_device_properties tt_s2_4600_properties = {
2267         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2268         .usb_ctrl = DEVICE_SPECIFIC,
2269         .size_of_priv = sizeof(struct dw2102_state),
2270         .power_ctrl = su3000_power_ctrl,
2271         .num_adapters = 1,
2272         .identify_state = su3000_identify_state,
2273         .i2c_algo = &su3000_i2c_algo,
2274
2275         .rc.core = {
2276                 .rc_interval = 250,
2277                 .rc_codes = RC_MAP_TT_1500,
2278                 .module_name = "dw2102",
2279                 .allowed_protos   = RC_BIT_RC5,
2280                 .rc_query = su3000_rc_query,
2281         },
2282
2283         .read_mac_address = su3000_read_mac_address,
2284
2285         .generic_bulk_ctrl_endpoint = 0x01,
2286
2287         .adapter = {
2288                 {
2289                 .num_frontends = 1,
2290                 .fe = {{
2291                         .streaming_ctrl   = su3000_streaming_ctrl,
2292                         .frontend_attach  = tt_s2_4600_frontend_attach,
2293                         .stream = {
2294                                 .type = USB_BULK,
2295                                 .count = 8,
2296                                 .endpoint = 0x82,
2297                                 .u = {
2298                                         .bulk = {
2299                                                 .buffersize = 4096,
2300                                         }
2301                                 }
2302                         }
2303                 } },
2304                 }
2305         },
2306         .num_device_descs = 5,
2307         .devices = {
2308                 { "TechnoTrend TT-connect S2-4600",
2309                         { &dw2102_table[TECHNOTREND_S2_4600], NULL },
2310                         { NULL },
2311                 },
2312                 { "TeVii S482 (tuner 1)",
2313                         { &dw2102_table[TEVII_S482_1], NULL },
2314                         { NULL },
2315                 },
2316                 { "TeVii S482 (tuner 2)",
2317                         { &dw2102_table[TEVII_S482_2], NULL },
2318                         { NULL },
2319                 },
2320                 { "Terratec Cinergy S2 USB BOX",
2321                         { &dw2102_table[TERRATEC_CINERGY_S2_BOX], NULL },
2322                         { NULL },
2323                 },
2324                 { "TeVii S662",
2325                         { &dw2102_table[TEVII_S662], NULL },
2326                         { NULL },
2327                 },
2328         }
2329 };
2330
2331 static int dw2102_probe(struct usb_interface *intf,
2332                 const struct usb_device_id *id)
2333 {
2334         p1100 = kmemdup(&s6x0_properties,
2335                         sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2336         if (!p1100)
2337                 return -ENOMEM;
2338         /* copy default structure */
2339         /* fill only different fields */
2340         p1100->firmware = P1100_FIRMWARE;
2341         p1100->devices[0] = d1100;
2342         p1100->rc.core.rc_query = prof_rc_query;
2343         p1100->rc.core.rc_codes = RC_MAP_TBS_NEC;
2344         p1100->adapter->fe[0].frontend_attach = stv0288_frontend_attach;
2345
2346         s660 = kmemdup(&s6x0_properties,
2347                        sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2348         if (!s660) {
2349                 kfree(p1100);
2350                 return -ENOMEM;
2351         }
2352         s660->firmware = S660_FIRMWARE;
2353         s660->num_device_descs = 3;
2354         s660->devices[0] = d660;
2355         s660->devices[1] = d480_1;
2356         s660->devices[2] = d480_2;
2357         s660->adapter->fe[0].frontend_attach = ds3000_frontend_attach;
2358
2359         p7500 = kmemdup(&s6x0_properties,
2360                         sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2361         if (!p7500) {
2362                 kfree(p1100);
2363                 kfree(s660);
2364                 return -ENOMEM;
2365         }
2366         p7500->firmware = P7500_FIRMWARE;
2367         p7500->devices[0] = d7500;
2368         p7500->rc.core.rc_query = prof_rc_query;
2369         p7500->rc.core.rc_codes = RC_MAP_TBS_NEC;
2370         p7500->adapter->fe[0].frontend_attach = prof_7500_frontend_attach;
2371
2372
2373         s421 = kmemdup(&su3000_properties,
2374                        sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2375         if (!s421) {
2376                 kfree(p1100);
2377                 kfree(s660);
2378                 kfree(p7500);
2379                 return -ENOMEM;
2380         }
2381         s421->num_device_descs = 2;
2382         s421->devices[0] = d421;
2383         s421->devices[1] = d632;
2384         s421->adapter->fe[0].frontend_attach = m88rs2000_frontend_attach;
2385
2386         if (0 == dvb_usb_device_init(intf, &dw2102_properties,
2387                         THIS_MODULE, NULL, adapter_nr) ||
2388             0 == dvb_usb_device_init(intf, &dw2104_properties,
2389                         THIS_MODULE, NULL, adapter_nr) ||
2390             0 == dvb_usb_device_init(intf, &dw3101_properties,
2391                         THIS_MODULE, NULL, adapter_nr) ||
2392             0 == dvb_usb_device_init(intf, &s6x0_properties,
2393                         THIS_MODULE, NULL, adapter_nr) ||
2394             0 == dvb_usb_device_init(intf, p1100,
2395                         THIS_MODULE, NULL, adapter_nr) ||
2396             0 == dvb_usb_device_init(intf, s660,
2397                         THIS_MODULE, NULL, adapter_nr) ||
2398             0 == dvb_usb_device_init(intf, p7500,
2399                         THIS_MODULE, NULL, adapter_nr) ||
2400             0 == dvb_usb_device_init(intf, s421,
2401                         THIS_MODULE, NULL, adapter_nr) ||
2402             0 == dvb_usb_device_init(intf, &su3000_properties,
2403                          THIS_MODULE, NULL, adapter_nr) ||
2404             0 == dvb_usb_device_init(intf, &t220_properties,
2405                          THIS_MODULE, NULL, adapter_nr) ||
2406             0 == dvb_usb_device_init(intf, &tt_s2_4600_properties,
2407                          THIS_MODULE, NULL, adapter_nr))
2408                 return 0;
2409
2410         return -ENODEV;
2411 }
2412
2413 static void dw2102_disconnect(struct usb_interface *intf)
2414 {
2415         struct dvb_usb_device *d = usb_get_intfdata(intf);
2416         struct dw2102_state *st = (struct dw2102_state *)d->priv;
2417         struct i2c_client *client;
2418
2419         /* remove I2C client for tuner */
2420         client = st->i2c_client_tuner;
2421         if (client) {
2422                 module_put(client->dev.driver->owner);
2423                 i2c_unregister_device(client);
2424         }
2425
2426         /* remove I2C client for demodulator */
2427         client = st->i2c_client_demod;
2428         if (client) {
2429                 module_put(client->dev.driver->owner);
2430                 i2c_unregister_device(client);
2431         }
2432
2433         dvb_usb_device_exit(intf);
2434 }
2435
2436 static struct usb_driver dw2102_driver = {
2437         .name = "dw2102",
2438         .probe = dw2102_probe,
2439         .disconnect = dw2102_disconnect,
2440         .id_table = dw2102_table,
2441 };
2442
2443 module_usb_driver(dw2102_driver);
2444
2445 MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
2446 MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104,"
2447                         " DVB-C 3101 USB2.0,"
2448                         " TeVii S421, S480, S482, S600, S630, S632, S650,"
2449                         " TeVii S660, S662, Prof 1100, 7500 USB2.0,"
2450                         " Geniatech SU3000, T220,"
2451                         " TechnoTrend S2-4600, Terratec Cinergy S2 devices");
2452 MODULE_VERSION("0.1");
2453 MODULE_LICENSE("GPL");
2454 /*(DEBLOBBED)*/