These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / 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 };
1692
1693 static struct usb_device_id dw2102_table[] = {
1694         [CYPRESS_DW2102] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
1695         [CYPRESS_DW2101] = {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
1696         [CYPRESS_DW2104] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2104)},
1697         [TEVII_S650] = {USB_DEVICE(0x9022, USB_PID_TEVII_S650)},
1698         [TERRATEC_CINERGY_S] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_CINERGY_S)},
1699         [CYPRESS_DW3101] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW3101)},
1700         [TEVII_S630] = {USB_DEVICE(0x9022, USB_PID_TEVII_S630)},
1701         [PROF_1100] = {USB_DEVICE(0x3011, USB_PID_PROF_1100)},
1702         [TEVII_S660] = {USB_DEVICE(0x9022, USB_PID_TEVII_S660)},
1703         [PROF_7500] = {USB_DEVICE(0x3034, 0x7500)},
1704         [GENIATECH_SU3000] = {USB_DEVICE(0x1f4d, 0x3000)},
1705         [TERRATEC_CINERGY_S2] = {USB_DEVICE(USB_VID_TERRATEC, 0x00a8)},
1706         [TEVII_S480_1] = {USB_DEVICE(0x9022, USB_PID_TEVII_S480_1)},
1707         [TEVII_S480_2] = {USB_DEVICE(0x9022, USB_PID_TEVII_S480_2)},
1708         [X3M_SPC1400HD] = {USB_DEVICE(0x1f4d, 0x3100)},
1709         [TEVII_S421] = {USB_DEVICE(0x9022, USB_PID_TEVII_S421)},
1710         [TEVII_S632] = {USB_DEVICE(0x9022, USB_PID_TEVII_S632)},
1711         [TERRATEC_CINERGY_S2_R2] = {USB_DEVICE(USB_VID_TERRATEC, 0x00b0)},
1712         [GOTVIEW_SAT_HD] = {USB_DEVICE(0x1FE1, USB_PID_GOTVIEW_SAT_HD)},
1713         [GENIATECH_T220] = {USB_DEVICE(0x1f4d, 0xD220)},
1714         [TECHNOTREND_S2_4600] = {USB_DEVICE(USB_VID_TECHNOTREND,
1715                 USB_PID_TECHNOTREND_CONNECT_S2_4600)},
1716         [TEVII_S482_1] = {USB_DEVICE(0x9022, 0xd483)},
1717         [TEVII_S482_2] = {USB_DEVICE(0x9022, 0xd484)},
1718         { }
1719 };
1720
1721 MODULE_DEVICE_TABLE(usb, dw2102_table);
1722
1723 static int dw2102_load_firmware(struct usb_device *dev,
1724                         const struct firmware *frmwr)
1725 {
1726         u8 *b, *p;
1727         int ret = 0, i;
1728         u8 reset;
1729         u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
1730         const struct firmware *fw;
1731
1732         switch (le16_to_cpu(dev->descriptor.idProduct)) {
1733         case 0x2101:
1734                 ret = request_firmware(&fw, DW2101_FIRMWARE, &dev->dev);
1735                 if (ret != 0) {
1736                         err(err_str, DW2101_FIRMWARE);
1737                         return ret;
1738                 }
1739                 break;
1740         default:
1741                 fw = frmwr;
1742                 break;
1743         }
1744         info("start downloading DW210X firmware");
1745         p = kmalloc(fw->size, GFP_KERNEL);
1746         reset = 1;
1747         /*stop the CPU*/
1748         dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
1749         dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
1750
1751         if (p != NULL) {
1752                 memcpy(p, fw->data, fw->size);
1753                 for (i = 0; i < fw->size; i += 0x40) {
1754                         b = (u8 *) p + i;
1755                         if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
1756                                         DW210X_WRITE_MSG) != 0x40) {
1757                                 err("error while transferring firmware");
1758                                 ret = -EINVAL;
1759                                 break;
1760                         }
1761                 }
1762                 /* restart the CPU */
1763                 reset = 0;
1764                 if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
1765                                         DW210X_WRITE_MSG) != 1) {
1766                         err("could not restart the USB controller CPU.");
1767                         ret = -EINVAL;
1768                 }
1769                 if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
1770                                         DW210X_WRITE_MSG) != 1) {
1771                         err("could not restart the USB controller CPU.");
1772                         ret = -EINVAL;
1773                 }
1774                 /* init registers */
1775                 switch (le16_to_cpu(dev->descriptor.idProduct)) {
1776                 case USB_PID_TEVII_S650:
1777                         dw2104_properties.rc.core.rc_codes = RC_MAP_TEVII_NEC;
1778                 case USB_PID_DW2104:
1779                         reset = 1;
1780                         dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
1781                                         DW210X_WRITE_MSG);
1782                         /* break omitted intentionally */
1783                 case USB_PID_DW3101:
1784                         reset = 0;
1785                         dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
1786                                         DW210X_WRITE_MSG);
1787                         break;
1788                 case USB_PID_CINERGY_S:
1789                 case USB_PID_DW2102:
1790                         dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
1791                                         DW210X_WRITE_MSG);
1792                         dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
1793                                         DW210X_READ_MSG);
1794                         /* check STV0299 frontend  */
1795                         dw210x_op_rw(dev, 0xb5, 0, 0, &reset16[0], 2,
1796                                         DW210X_READ_MSG);
1797                         if ((reset16[0] == 0xa1) || (reset16[0] == 0x80)) {
1798                                 dw2102_properties.i2c_algo = &dw2102_i2c_algo;
1799                                 dw2102_properties.adapter->fe[0].tuner_attach = &dw2102_tuner_attach;
1800                                 break;
1801                         } else {
1802                                 /* check STV0288 frontend  */
1803                                 reset16[0] = 0xd0;
1804                                 reset16[1] = 1;
1805                                 reset16[2] = 0;
1806                                 dw210x_op_rw(dev, 0xc2, 0, 0, &reset16[0], 3,
1807                                                 DW210X_WRITE_MSG);
1808                                 dw210x_op_rw(dev, 0xc3, 0xd1, 0, &reset16[0], 3,
1809                                                 DW210X_READ_MSG);
1810                                 if (reset16[2] == 0x11) {
1811                                         dw2102_properties.i2c_algo = &dw2102_earda_i2c_algo;
1812                                         break;
1813                                 }
1814                         }
1815                 case 0x2101:
1816                         dw210x_op_rw(dev, 0xbc, 0x0030, 0, &reset16[0], 2,
1817                                         DW210X_READ_MSG);
1818                         dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
1819                                         DW210X_READ_MSG);
1820                         dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
1821                                         DW210X_READ_MSG);
1822                         dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
1823                                         DW210X_READ_MSG);
1824                         break;
1825                 }
1826
1827                 msleep(100);
1828                 kfree(p);
1829         }
1830         return ret;
1831 }
1832
1833 static struct dvb_usb_device_properties dw2102_properties = {
1834         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1835         .usb_ctrl = DEVICE_SPECIFIC,
1836         .firmware = DW2102_FIRMWARE,
1837         .no_reconnect = 1,
1838
1839         .i2c_algo = &dw2102_serit_i2c_algo,
1840
1841         .rc.core = {
1842                 .rc_interval = 150,
1843                 .rc_codes = RC_MAP_DM1105_NEC,
1844                 .module_name = "dw2102",
1845                 .allowed_protos   = RC_BIT_NEC,
1846                 .rc_query = dw2102_rc_query,
1847         },
1848
1849         .generic_bulk_ctrl_endpoint = 0x81,
1850         /* parameter for the MPEG2-data transfer */
1851         .num_adapters = 1,
1852         .download_firmware = dw2102_load_firmware,
1853         .read_mac_address = dw210x_read_mac_address,
1854         .adapter = {
1855                 {
1856                 .num_frontends = 1,
1857                 .fe = {{
1858                         .frontend_attach = dw2102_frontend_attach,
1859                         .stream = {
1860                                 .type = USB_BULK,
1861                                 .count = 8,
1862                                 .endpoint = 0x82,
1863                                 .u = {
1864                                         .bulk = {
1865                                                 .buffersize = 4096,
1866                                         }
1867                                 }
1868                         },
1869                 }},
1870                 }
1871         },
1872         .num_device_descs = 3,
1873         .devices = {
1874                 {"DVBWorld DVB-S 2102 USB2.0",
1875                         {&dw2102_table[CYPRESS_DW2102], NULL},
1876                         {NULL},
1877                 },
1878                 {"DVBWorld DVB-S 2101 USB2.0",
1879                         {&dw2102_table[CYPRESS_DW2101], NULL},
1880                         {NULL},
1881                 },
1882                 {"TerraTec Cinergy S USB",
1883                         {&dw2102_table[TERRATEC_CINERGY_S], NULL},
1884                         {NULL},
1885                 },
1886         }
1887 };
1888
1889 static struct dvb_usb_device_properties dw2104_properties = {
1890         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1891         .usb_ctrl = DEVICE_SPECIFIC,
1892         .firmware = DW2104_FIRMWARE,
1893         .no_reconnect = 1,
1894
1895         .i2c_algo = &dw2104_i2c_algo,
1896         .rc.core = {
1897                 .rc_interval = 150,
1898                 .rc_codes = RC_MAP_DM1105_NEC,
1899                 .module_name = "dw2102",
1900                 .allowed_protos   = RC_BIT_NEC,
1901                 .rc_query = dw2102_rc_query,
1902         },
1903
1904         .generic_bulk_ctrl_endpoint = 0x81,
1905         /* parameter for the MPEG2-data transfer */
1906         .num_adapters = 1,
1907         .download_firmware = dw2102_load_firmware,
1908         .read_mac_address = dw210x_read_mac_address,
1909         .adapter = {
1910                 {
1911                 .num_frontends = 1,
1912                 .fe = {{
1913                         .frontend_attach = dw2104_frontend_attach,
1914                         .stream = {
1915                                 .type = USB_BULK,
1916                                 .count = 8,
1917                                 .endpoint = 0x82,
1918                                 .u = {
1919                                         .bulk = {
1920                                                 .buffersize = 4096,
1921                                         }
1922                                 }
1923                         },
1924                 }},
1925                 }
1926         },
1927         .num_device_descs = 2,
1928         .devices = {
1929                 { "DVBWorld DW2104 USB2.0",
1930                         {&dw2102_table[CYPRESS_DW2104], NULL},
1931                         {NULL},
1932                 },
1933                 { "TeVii S650 USB2.0",
1934                         {&dw2102_table[TEVII_S650], NULL},
1935                         {NULL},
1936                 },
1937         }
1938 };
1939
1940 static struct dvb_usb_device_properties dw3101_properties = {
1941         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1942         .usb_ctrl = DEVICE_SPECIFIC,
1943         .firmware = DW3101_FIRMWARE,
1944         .no_reconnect = 1,
1945
1946         .i2c_algo = &dw3101_i2c_algo,
1947         .rc.core = {
1948                 .rc_interval = 150,
1949                 .rc_codes = RC_MAP_DM1105_NEC,
1950                 .module_name = "dw2102",
1951                 .allowed_protos   = RC_BIT_NEC,
1952                 .rc_query = dw2102_rc_query,
1953         },
1954
1955         .generic_bulk_ctrl_endpoint = 0x81,
1956         /* parameter for the MPEG2-data transfer */
1957         .num_adapters = 1,
1958         .download_firmware = dw2102_load_firmware,
1959         .read_mac_address = dw210x_read_mac_address,
1960         .adapter = {
1961                 {
1962                 .num_frontends = 1,
1963                 .fe = {{
1964                         .frontend_attach = dw3101_frontend_attach,
1965                         .tuner_attach = dw3101_tuner_attach,
1966                         .stream = {
1967                                 .type = USB_BULK,
1968                                 .count = 8,
1969                                 .endpoint = 0x82,
1970                                 .u = {
1971                                         .bulk = {
1972                                                 .buffersize = 4096,
1973                                         }
1974                                 }
1975                         },
1976                 }},
1977                 }
1978         },
1979         .num_device_descs = 1,
1980         .devices = {
1981                 { "DVBWorld DVB-C 3101 USB2.0",
1982                         {&dw2102_table[CYPRESS_DW3101], NULL},
1983                         {NULL},
1984                 },
1985         }
1986 };
1987
1988 static struct dvb_usb_device_properties s6x0_properties = {
1989         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1990         .usb_ctrl = DEVICE_SPECIFIC,
1991         .size_of_priv = sizeof(struct dw2102_state),
1992         .firmware = S630_FIRMWARE,
1993         .no_reconnect = 1,
1994
1995         .i2c_algo = &s6x0_i2c_algo,
1996         .rc.core = {
1997                 .rc_interval = 150,
1998                 .rc_codes = RC_MAP_TEVII_NEC,
1999                 .module_name = "dw2102",
2000                 .allowed_protos   = RC_BIT_NEC,
2001                 .rc_query = dw2102_rc_query,
2002         },
2003
2004         .generic_bulk_ctrl_endpoint = 0x81,
2005         .num_adapters = 1,
2006         .download_firmware = dw2102_load_firmware,
2007         .read_mac_address = s6x0_read_mac_address,
2008         .adapter = {
2009                 {
2010                 .num_frontends = 1,
2011                 .fe = {{
2012                         .frontend_attach = zl100313_frontend_attach,
2013                         .stream = {
2014                                 .type = USB_BULK,
2015                                 .count = 8,
2016                                 .endpoint = 0x82,
2017                                 .u = {
2018                                         .bulk = {
2019                                                 .buffersize = 4096,
2020                                         }
2021                                 }
2022                         },
2023                 }},
2024                 }
2025         },
2026         .num_device_descs = 1,
2027         .devices = {
2028                 {"TeVii S630 USB",
2029                         {&dw2102_table[TEVII_S630], NULL},
2030                         {NULL},
2031                 },
2032         }
2033 };
2034
2035 static struct dvb_usb_device_properties *p1100;
2036 static struct dvb_usb_device_description d1100 = {
2037         "Prof 1100 USB ",
2038         {&dw2102_table[PROF_1100], NULL},
2039         {NULL},
2040 };
2041
2042 static struct dvb_usb_device_properties *s660;
2043 static struct dvb_usb_device_description d660 = {
2044         "TeVii S660 USB",
2045         {&dw2102_table[TEVII_S660], NULL},
2046         {NULL},
2047 };
2048
2049 static struct dvb_usb_device_description d480_1 = {
2050         "TeVii S480.1 USB",
2051         {&dw2102_table[TEVII_S480_1], NULL},
2052         {NULL},
2053 };
2054
2055 static struct dvb_usb_device_description d480_2 = {
2056         "TeVii S480.2 USB",
2057         {&dw2102_table[TEVII_S480_2], NULL},
2058         {NULL},
2059 };
2060
2061 static struct dvb_usb_device_properties *p7500;
2062 static struct dvb_usb_device_description d7500 = {
2063         "Prof 7500 USB DVB-S2",
2064         {&dw2102_table[PROF_7500], NULL},
2065         {NULL},
2066 };
2067
2068 static struct dvb_usb_device_properties *s421;
2069 static struct dvb_usb_device_description d421 = {
2070         "TeVii S421 PCI",
2071         {&dw2102_table[TEVII_S421], NULL},
2072         {NULL},
2073 };
2074
2075 static struct dvb_usb_device_description d632 = {
2076         "TeVii S632 USB",
2077         {&dw2102_table[TEVII_S632], NULL},
2078         {NULL},
2079 };
2080
2081 static struct dvb_usb_device_properties su3000_properties = {
2082         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2083         .usb_ctrl = DEVICE_SPECIFIC,
2084         .size_of_priv = sizeof(struct dw2102_state),
2085         .power_ctrl = su3000_power_ctrl,
2086         .num_adapters = 1,
2087         .identify_state = su3000_identify_state,
2088         .i2c_algo = &su3000_i2c_algo,
2089
2090         .rc.core = {
2091                 .rc_interval = 150,
2092                 .rc_codes = RC_MAP_SU3000,
2093                 .module_name = "dw2102",
2094                 .allowed_protos   = RC_BIT_RC5,
2095                 .rc_query = su3000_rc_query,
2096         },
2097
2098         .read_mac_address = su3000_read_mac_address,
2099
2100         .generic_bulk_ctrl_endpoint = 0x01,
2101
2102         .adapter = {
2103                 {
2104                 .num_frontends = 1,
2105                 .fe = {{
2106                         .streaming_ctrl   = su3000_streaming_ctrl,
2107                         .frontend_attach  = su3000_frontend_attach,
2108                         .stream = {
2109                                 .type = USB_BULK,
2110                                 .count = 8,
2111                                 .endpoint = 0x82,
2112                                 .u = {
2113                                         .bulk = {
2114                                                 .buffersize = 4096,
2115                                         }
2116                                 }
2117                         }
2118                 }},
2119                 }
2120         },
2121         .num_device_descs = 5,
2122         .devices = {
2123                 { "SU3000HD DVB-S USB2.0",
2124                         { &dw2102_table[GENIATECH_SU3000], NULL },
2125                         { NULL },
2126                 },
2127                 { "Terratec Cinergy S2 USB HD",
2128                         { &dw2102_table[TERRATEC_CINERGY_S2], NULL },
2129                         { NULL },
2130                 },
2131                 { "X3M TV SPC1400HD PCI",
2132                         { &dw2102_table[X3M_SPC1400HD], NULL },
2133                         { NULL },
2134                 },
2135                 { "Terratec Cinergy S2 USB HD Rev.2",
2136                         { &dw2102_table[TERRATEC_CINERGY_S2_R2], NULL },
2137                         { NULL },
2138                 },
2139                 { "GOTVIEW Satellite HD",
2140                         { &dw2102_table[GOTVIEW_SAT_HD], NULL },
2141                         { NULL },
2142                 },
2143         }
2144 };
2145
2146 static struct dvb_usb_device_properties t220_properties = {
2147         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2148         .usb_ctrl = DEVICE_SPECIFIC,
2149         .size_of_priv = sizeof(struct dw2102_state),
2150         .power_ctrl = su3000_power_ctrl,
2151         .num_adapters = 1,
2152         .identify_state = su3000_identify_state,
2153         .i2c_algo = &su3000_i2c_algo,
2154
2155         .rc.core = {
2156                 .rc_interval = 150,
2157                 .rc_codes = RC_MAP_SU3000,
2158                 .module_name = "dw2102",
2159                 .allowed_protos   = RC_BIT_RC5,
2160                 .rc_query = su3000_rc_query,
2161         },
2162
2163         .read_mac_address = su3000_read_mac_address,
2164
2165         .generic_bulk_ctrl_endpoint = 0x01,
2166
2167         .adapter = {
2168                 {
2169                 .num_frontends = 1,
2170                 .fe = { {
2171                         .streaming_ctrl   = su3000_streaming_ctrl,
2172                         .frontend_attach  = t220_frontend_attach,
2173                         .stream = {
2174                                 .type = USB_BULK,
2175                                 .count = 8,
2176                                 .endpoint = 0x82,
2177                                 .u = {
2178                                         .bulk = {
2179                                                 .buffersize = 4096,
2180                                         }
2181                                 }
2182                         }
2183                 } },
2184                 }
2185         },
2186         .num_device_descs = 1,
2187         .devices = {
2188                 { "Geniatech T220 DVB-T/T2 USB2.0",
2189                         { &dw2102_table[GENIATECH_T220], NULL },
2190                         { NULL },
2191                 },
2192         }
2193 };
2194
2195 static struct dvb_usb_device_properties tt_s2_4600_properties = {
2196         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2197         .usb_ctrl = DEVICE_SPECIFIC,
2198         .size_of_priv = sizeof(struct dw2102_state),
2199         .power_ctrl = su3000_power_ctrl,
2200         .num_adapters = 1,
2201         .identify_state = su3000_identify_state,
2202         .i2c_algo = &su3000_i2c_algo,
2203
2204         .rc.core = {
2205                 .rc_interval = 250,
2206                 .rc_codes = RC_MAP_TT_1500,
2207                 .module_name = "dw2102",
2208                 .allowed_protos   = RC_BIT_RC5,
2209                 .rc_query = su3000_rc_query,
2210         },
2211
2212         .read_mac_address = su3000_read_mac_address,
2213
2214         .generic_bulk_ctrl_endpoint = 0x01,
2215
2216         .adapter = {
2217                 {
2218                 .num_frontends = 1,
2219                 .fe = {{
2220                         .streaming_ctrl   = su3000_streaming_ctrl,
2221                         .frontend_attach  = tt_s2_4600_frontend_attach,
2222                         .stream = {
2223                                 .type = USB_BULK,
2224                                 .count = 8,
2225                                 .endpoint = 0x82,
2226                                 .u = {
2227                                         .bulk = {
2228                                                 .buffersize = 4096,
2229                                         }
2230                                 }
2231                         }
2232                 } },
2233                 }
2234         },
2235         .num_device_descs = 3,
2236         .devices = {
2237                 { "TechnoTrend TT-connect S2-4600",
2238                         { &dw2102_table[TECHNOTREND_S2_4600], NULL },
2239                         { NULL },
2240                 },
2241                 { "TeVii S482 (tuner 1)",
2242                         { &dw2102_table[TEVII_S482_1], NULL },
2243                         { NULL },
2244                 },
2245                 { "TeVii S482 (tuner 2)",
2246                         { &dw2102_table[TEVII_S482_2], NULL },
2247                         { NULL },
2248                 },
2249         }
2250 };
2251
2252 static int dw2102_probe(struct usb_interface *intf,
2253                 const struct usb_device_id *id)
2254 {
2255         p1100 = kmemdup(&s6x0_properties,
2256                         sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2257         if (!p1100)
2258                 return -ENOMEM;
2259         /* copy default structure */
2260         /* fill only different fields */
2261         p1100->firmware = P1100_FIRMWARE;
2262         p1100->devices[0] = d1100;
2263         p1100->rc.core.rc_query = prof_rc_query;
2264         p1100->rc.core.rc_codes = RC_MAP_TBS_NEC;
2265         p1100->adapter->fe[0].frontend_attach = stv0288_frontend_attach;
2266
2267         s660 = kmemdup(&s6x0_properties,
2268                        sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2269         if (!s660) {
2270                 kfree(p1100);
2271                 return -ENOMEM;
2272         }
2273         s660->firmware = S660_FIRMWARE;
2274         s660->num_device_descs = 3;
2275         s660->devices[0] = d660;
2276         s660->devices[1] = d480_1;
2277         s660->devices[2] = d480_2;
2278         s660->adapter->fe[0].frontend_attach = ds3000_frontend_attach;
2279
2280         p7500 = kmemdup(&s6x0_properties,
2281                         sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2282         if (!p7500) {
2283                 kfree(p1100);
2284                 kfree(s660);
2285                 return -ENOMEM;
2286         }
2287         p7500->firmware = P7500_FIRMWARE;
2288         p7500->devices[0] = d7500;
2289         p7500->rc.core.rc_query = prof_rc_query;
2290         p7500->rc.core.rc_codes = RC_MAP_TBS_NEC;
2291         p7500->adapter->fe[0].frontend_attach = prof_7500_frontend_attach;
2292
2293
2294         s421 = kmemdup(&su3000_properties,
2295                        sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2296         if (!s421) {
2297                 kfree(p1100);
2298                 kfree(s660);
2299                 kfree(p7500);
2300                 return -ENOMEM;
2301         }
2302         s421->num_device_descs = 2;
2303         s421->devices[0] = d421;
2304         s421->devices[1] = d632;
2305         s421->adapter->fe[0].frontend_attach = m88rs2000_frontend_attach;
2306
2307         if (0 == dvb_usb_device_init(intf, &dw2102_properties,
2308                         THIS_MODULE, NULL, adapter_nr) ||
2309             0 == dvb_usb_device_init(intf, &dw2104_properties,
2310                         THIS_MODULE, NULL, adapter_nr) ||
2311             0 == dvb_usb_device_init(intf, &dw3101_properties,
2312                         THIS_MODULE, NULL, adapter_nr) ||
2313             0 == dvb_usb_device_init(intf, &s6x0_properties,
2314                         THIS_MODULE, NULL, adapter_nr) ||
2315             0 == dvb_usb_device_init(intf, p1100,
2316                         THIS_MODULE, NULL, adapter_nr) ||
2317             0 == dvb_usb_device_init(intf, s660,
2318                         THIS_MODULE, NULL, adapter_nr) ||
2319             0 == dvb_usb_device_init(intf, p7500,
2320                         THIS_MODULE, NULL, adapter_nr) ||
2321             0 == dvb_usb_device_init(intf, s421,
2322                         THIS_MODULE, NULL, adapter_nr) ||
2323             0 == dvb_usb_device_init(intf, &su3000_properties,
2324                          THIS_MODULE, NULL, adapter_nr) ||
2325             0 == dvb_usb_device_init(intf, &t220_properties,
2326                          THIS_MODULE, NULL, adapter_nr) ||
2327             0 == dvb_usb_device_init(intf, &tt_s2_4600_properties,
2328                          THIS_MODULE, NULL, adapter_nr))
2329                 return 0;
2330
2331         return -ENODEV;
2332 }
2333
2334 static void dw2102_disconnect(struct usb_interface *intf)
2335 {
2336         struct dvb_usb_device *d = usb_get_intfdata(intf);
2337         struct dw2102_state *st = (struct dw2102_state *)d->priv;
2338         struct i2c_client *client;
2339
2340         /* remove I2C client for tuner */
2341         client = st->i2c_client_tuner;
2342         if (client) {
2343                 module_put(client->dev.driver->owner);
2344                 i2c_unregister_device(client);
2345         }
2346
2347         dvb_usb_device_exit(intf);
2348 }
2349
2350 static struct usb_driver dw2102_driver = {
2351         .name = "dw2102",
2352         .probe = dw2102_probe,
2353         .disconnect = dw2102_disconnect,
2354         .id_table = dw2102_table,
2355 };
2356
2357 module_usb_driver(dw2102_driver);
2358
2359 MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
2360 MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104,"
2361                         " DVB-C 3101 USB2.0,"
2362                         " TeVii S600, S630, S650, S660, S480, S421, S632"
2363                         " Prof 1100, 7500 USB2.0,"
2364                         " Geniatech SU3000, T220,"
2365                         " TechnoTrend S2-4600 devices");
2366 MODULE_VERSION("0.1");
2367 MODULE_LICENSE("GPL");
2368 MODULE_FIRMWARE(DW2101_FIRMWARE);
2369 MODULE_FIRMWARE(DW2102_FIRMWARE);
2370 MODULE_FIRMWARE(DW2104_FIRMWARE);
2371 MODULE_FIRMWARE(DW3101_FIRMWARE);
2372 MODULE_FIRMWARE(S630_FIRMWARE);
2373 MODULE_FIRMWARE(S660_FIRMWARE);
2374 MODULE_FIRMWARE(P1100_FIRMWARE);
2375 MODULE_FIRMWARE(P7500_FIRMWARE);