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