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