14967464e812e19a05c971dd003a0ac239529b39
[cascardo/linux.git] / drivers / media / dvb / dvb-usb / af9015.c
1 /*
2  * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
3  *
4  * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5  *
6  * Thanks to Afatech who kindly provided information.
7  *
8  *    This program is free software; you can redistribute it and/or modify
9  *    it under the terms of the GNU General Public License as published by
10  *    the Free Software Foundation; either version 2 of the License, or
11  *    (at your option) any later version.
12  *
13  *    This program is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU General Public License for more details.
17  *
18  *    You should have received a copy of the GNU General Public License
19  *    along with this program; if not, write to the Free Software
20  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include "af9015.h"
25 #include "af9013.h"
26 #include "mt2060.h"
27 #include "qt1010.h"
28 #include "tda18271.h"
29 #include "mxl5005s.h"
30 #include "mc44s803.h"
31
32 static int dvb_usb_af9015_debug;
33 module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
34 MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
35 static int dvb_usb_af9015_remote;
36 module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
37 MODULE_PARM_DESC(remote, "select remote");
38 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
39
40 static DEFINE_MUTEX(af9015_usb_mutex);
41
42 static struct af9015_config af9015_config;
43 static struct dvb_usb_device_properties af9015_properties[2];
44 static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
45
46 static struct af9013_config af9015_af9013_config[] = {
47         {
48                 .demod_address = AF9015_I2C_DEMOD,
49                 .output_mode = AF9013_OUTPUT_MODE_USB,
50                 .api_version = { 0, 1, 9, 0 },
51                 .gpio[0] = AF9013_GPIO_HI,
52                 .gpio[3] = AF9013_GPIO_TUNER_ON,
53
54         }, {
55                 .output_mode = AF9013_OUTPUT_MODE_SERIAL,
56                 .api_version = { 0, 1, 9, 0 },
57                 .gpio[0] = AF9013_GPIO_TUNER_ON,
58                 .gpio[1] = AF9013_GPIO_LO,
59         }
60 };
61
62 static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
63 {
64         int act_len, ret;
65         u8 buf[64];
66         u8 write = 1;
67         u8 msg_len = 8;
68         static u8 seq; /* packet sequence number */
69
70         if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
71                 return -EAGAIN;
72
73         buf[0] = req->cmd;
74         buf[1] = seq++;
75         buf[2] = req->i2c_addr;
76         buf[3] = req->addr >> 8;
77         buf[4] = req->addr & 0xff;
78         buf[5] = req->mbox;
79         buf[6] = req->addr_len;
80         buf[7] = req->data_len;
81
82         switch (req->cmd) {
83         case GET_CONFIG:
84         case BOOT:
85         case READ_MEMORY:
86         case RECONNECT_USB:
87         case GET_IR_CODE:
88                 write = 0;
89                 break;
90         case READ_I2C:
91                 write = 0;
92                 buf[2] |= 0x01; /* set I2C direction */
93         case WRITE_I2C:
94                 buf[0] = READ_WRITE_I2C;
95                 break;
96         case WRITE_MEMORY:
97                 if (((req->addr & 0xff00) == 0xff00) ||
98                     ((req->addr & 0xae00) == 0xae00))
99                         buf[0] = WRITE_VIRTUAL_MEMORY;
100         case WRITE_VIRTUAL_MEMORY:
101         case COPY_FIRMWARE:
102         case DOWNLOAD_FIRMWARE:
103                 break;
104         default:
105                 err("unknown command:%d", req->cmd);
106                 ret = -1;
107                 goto error_unlock;
108         }
109
110         /* write requested */
111         if (write) {
112                 memcpy(&buf[8], req->data, req->data_len);
113                 msg_len += req->data_len;
114         }
115         deb_xfer(">>> ");
116         debug_dump(buf, msg_len, deb_xfer);
117
118         /* send req */
119         ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
120         &act_len, AF9015_USB_TIMEOUT);
121         if (ret)
122                 err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
123         else
124                 if (act_len != msg_len)
125                         ret = -1; /* all data is not send */
126         if (ret)
127                 goto error_unlock;
128
129         /* no ack for those packets */
130         if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
131                 goto exit_unlock;
132
133         /* receive ack and data if read req */
134         msg_len = 1 + 1 + req->data_len;  /* seq + status + data len */
135         ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
136                            &act_len, AF9015_USB_TIMEOUT);
137         if (ret) {
138                 err("recv bulk message failed:%d", ret);
139                 ret = -1;
140                 goto error_unlock;
141         }
142
143         deb_xfer("<<< ");
144         debug_dump(buf, act_len, deb_xfer);
145
146         /* remote controller query status is 1 if remote code is not received */
147         if (req->cmd == GET_IR_CODE && buf[1] == 1) {
148                 buf[1] = 0; /* clear command "error" status */
149                 memset(&buf[2], 0, req->data_len);
150                 buf[3] = 1; /* no remote code received mark */
151         }
152
153         /* check status */
154         if (buf[1]) {
155                 err("command failed:%d", buf[1]);
156                 ret = -1;
157                 goto error_unlock;
158         }
159
160         /* read request, copy returned data to return buf */
161         if (!write)
162                 memcpy(req->data, &buf[2], req->data_len);
163
164 error_unlock:
165 exit_unlock:
166         mutex_unlock(&af9015_usb_mutex);
167
168         return ret;
169 }
170
171 static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
172 {
173         return af9015_rw_udev(d->udev, req);
174 }
175
176 static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
177         u8 len)
178 {
179         struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
180                 val};
181         return af9015_ctrl_msg(d, &req);
182 }
183
184 static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
185 {
186         return af9015_write_regs(d, addr, &val, 1);
187 }
188
189 static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
190 {
191         struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, 1, val};
192         return af9015_ctrl_msg(d, &req);
193 }
194
195 static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
196         u8 val)
197 {
198         struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
199
200         if (addr == af9015_af9013_config[0].demod_address ||
201             addr == af9015_af9013_config[1].demod_address)
202                 req.addr_len = 3;
203
204         return af9015_ctrl_msg(d, &req);
205 }
206
207 static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
208         u8 *val)
209 {
210         struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
211
212         if (addr == af9015_af9013_config[0].demod_address ||
213             addr == af9015_af9013_config[1].demod_address)
214                 req.addr_len = 3;
215
216         return af9015_ctrl_msg(d, &req);
217 }
218
219 static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
220         int num)
221 {
222         struct dvb_usb_device *d = i2c_get_adapdata(adap);
223         int ret = 0, i = 0;
224         u16 addr;
225         u8 mbox, addr_len;
226         struct req_t req;
227
228 /* TODO: implement bus lock
229
230 The bus lock is needed because there is two tuners both using same I2C-address.
231 Due to that the only way to select correct tuner is use demodulator I2C-gate.
232
233 ................................................
234 . AF9015 includes integrated AF9013 demodulator.
235 . ____________                   ____________  .                ____________
236 .|     uC     |                 |   demod    | .               |    tuner   |
237 .|------------|                 |------------| .               |------------|
238 .|   AF9015   |                 |  AF9013/5  | .               |   MXL5003  |
239 .|            |--+----I2C-------|-----/ -----|-.-----I2C-------|            |
240 .|            |  |              | addr 0x38  | .               |  addr 0xc6 |
241 .|____________|  |              |____________| .               |____________|
242 .................|..............................
243                  |               ____________                   ____________
244                  |              |   demod    |                 |    tuner   |
245                  |              |------------|                 |------------|
246                  |              |   AF9013   |                 |   MXL5003  |
247                  +----I2C-------|-----/ -----|-------I2C-------|            |
248                                 | addr 0x3a  |                 |  addr 0xc6 |
249                                 |____________|                 |____________|
250 */
251         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
252                 return -EAGAIN;
253
254         while (i < num) {
255                 if (msg[i].addr == af9015_af9013_config[0].demod_address ||
256                     msg[i].addr == af9015_af9013_config[1].demod_address) {
257                         addr = msg[i].buf[0] << 8;
258                         addr += msg[i].buf[1];
259                         mbox = msg[i].buf[2];
260                         addr_len = 3;
261                 } else {
262                         addr = msg[i].buf[0];
263                         addr_len = 1;
264                         mbox = 0;
265                 }
266
267                 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
268                         if (msg[i].addr ==
269                                 af9015_af9013_config[0].demod_address)
270                                 req.cmd = READ_MEMORY;
271                         else
272                                 req.cmd = READ_I2C;
273                         req.i2c_addr = msg[i].addr;
274                         req.addr = addr;
275                         req.mbox = mbox;
276                         req.addr_len = addr_len;
277                         req.data_len = msg[i+1].len;
278                         req.data = &msg[i+1].buf[0];
279                         ret = af9015_ctrl_msg(d, &req);
280                         i += 2;
281                 } else if (msg[i].flags & I2C_M_RD) {
282                         ret = -EINVAL;
283                         if (msg[i].addr ==
284                                 af9015_af9013_config[0].demod_address)
285                                 goto error;
286                         else
287                                 req.cmd = READ_I2C;
288                         req.i2c_addr = msg[i].addr;
289                         req.addr = addr;
290                         req.mbox = mbox;
291                         req.addr_len = addr_len;
292                         req.data_len = msg[i].len;
293                         req.data = &msg[i].buf[0];
294                         ret = af9015_ctrl_msg(d, &req);
295                         i += 1;
296                 } else {
297                         if (msg[i].addr ==
298                                 af9015_af9013_config[0].demod_address)
299                                 req.cmd = WRITE_MEMORY;
300                         else
301                                 req.cmd = WRITE_I2C;
302                         req.i2c_addr = msg[i].addr;
303                         req.addr = addr;
304                         req.mbox = mbox;
305                         req.addr_len = addr_len;
306                         req.data_len = msg[i].len-addr_len;
307                         req.data = &msg[i].buf[addr_len];
308                         ret = af9015_ctrl_msg(d, &req);
309                         i += 1;
310                 }
311                 if (ret)
312                         goto error;
313
314         }
315         ret = i;
316
317 error:
318         mutex_unlock(&d->i2c_mutex);
319
320         return ret;
321 }
322
323 static u32 af9015_i2c_func(struct i2c_adapter *adapter)
324 {
325         return I2C_FUNC_I2C;
326 }
327
328 static struct i2c_algorithm af9015_i2c_algo = {
329         .master_xfer = af9015_i2c_xfer,
330         .functionality = af9015_i2c_func,
331 };
332
333 static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
334 {
335         int ret;
336         u8 val, mask = 0x01;
337
338         ret = af9015_read_reg(d, addr, &val);
339         if (ret)
340                 return ret;
341
342         mask <<= bit;
343         if (op) {
344                 /* set bit */
345                 val |= mask;
346         } else {
347                 /* clear bit */
348                 mask ^= 0xff;
349                 val &= mask;
350         }
351
352         return af9015_write_reg(d, addr, val);
353 }
354
355 static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
356 {
357         return af9015_do_reg_bit(d, addr, bit, 1);
358 }
359
360 static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
361 {
362         return af9015_do_reg_bit(d, addr, bit, 0);
363 }
364
365 static int af9015_init_endpoint(struct dvb_usb_device *d)
366 {
367         int ret;
368         u16 frame_size;
369         u8  packet_size;
370         deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
371
372 #define TS_PACKET_SIZE            188
373
374 #define TS_USB20_PACKET_COUNT     348
375 #define TS_USB20_FRAME_SIZE       (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
376
377 #define TS_USB11_PACKET_COUNT      21
378 #define TS_USB11_FRAME_SIZE       (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
379
380 #define TS_USB20_MAX_PACKET_SIZE  512
381 #define TS_USB11_MAX_PACKET_SIZE   64
382
383         if (d->udev->speed == USB_SPEED_FULL) {
384                 frame_size = TS_USB11_FRAME_SIZE/4;
385                 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
386         } else {
387                 frame_size = TS_USB20_FRAME_SIZE/4;
388                 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
389         }
390
391         ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
392         if (ret)
393                 goto error;
394         ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
395         if (ret)
396                 goto error;
397         ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
398         if (ret)
399                 goto error;
400         ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
401         if (ret)
402                 goto error;
403         ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
404         if (ret)
405                 goto error;
406         if (af9015_config.dual_mode) {
407                 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
408                 if (ret)
409                         goto error;
410         }
411         ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
412         if (ret)
413                 goto error;
414         if (af9015_config.dual_mode) {
415                 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
416                 if (ret)
417                         goto error;
418         }
419         /* EP4 xfer length */
420         ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
421         if (ret)
422                 goto error;
423         ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
424         if (ret)
425                 goto error;
426         /* EP5 xfer length */
427         ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
428         if (ret)
429                 goto error;
430         ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
431         if (ret)
432                 goto error;
433         ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
434         if (ret)
435                 goto error;
436         ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
437         if (ret)
438                 goto error;
439         ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
440         if (ret)
441                 goto error;
442         if (af9015_config.dual_mode) {
443                 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
444                 if (ret)
445                         goto error;
446         }
447
448         /* enable / disable mp2if2 */
449         if (af9015_config.dual_mode)
450                 ret = af9015_set_reg_bit(d, 0xd50b, 0);
451         else
452                 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
453 error:
454         if (ret)
455                 err("endpoint init failed:%d", ret);
456         return ret;
457 }
458
459 static int af9015_copy_firmware(struct dvb_usb_device *d)
460 {
461         int ret;
462         u8 fw_params[4];
463         u8 val, i;
464         struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
465                 fw_params };
466         deb_info("%s:\n", __func__);
467
468         fw_params[0] = af9015_config.firmware_size >> 8;
469         fw_params[1] = af9015_config.firmware_size & 0xff;
470         fw_params[2] = af9015_config.firmware_checksum >> 8;
471         fw_params[3] = af9015_config.firmware_checksum & 0xff;
472
473         /* wait 2nd demodulator ready */
474         msleep(100);
475
476         ret = af9015_read_reg_i2c(d, 0x3a, 0x98be, &val);
477         if (ret)
478                 goto error;
479         else
480                 deb_info("%s: firmware status:%02x\n", __func__, val);
481
482         if (val == 0x0c) /* fw is running, no need for download */
483                 goto exit;
484
485         /* set I2C master clock to fast (to speed up firmware copy) */
486         ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
487         if (ret)
488                 goto error;
489
490         msleep(50);
491
492         /* copy firmware */
493         ret = af9015_ctrl_msg(d, &req);
494         if (ret)
495                 err("firmware copy cmd failed:%d", ret);
496         deb_info("%s: firmware copy done\n", __func__);
497
498         /* set I2C master clock back to normal */
499         ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
500         if (ret)
501                 goto error;
502
503         /* request boot firmware */
504         ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
505                 0xe205, 1);
506         deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
507         if (ret)
508                 goto error;
509
510         for (i = 0; i < 15; i++) {
511                 msleep(100);
512
513                 /* check firmware status */
514                 ret = af9015_read_reg_i2c(d,
515                         af9015_af9013_config[1].demod_address, 0x98be, &val);
516                 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
517                         __func__, ret, val);
518                 if (ret)
519                         goto error;
520
521                 if (val == 0x0c || val == 0x04) /* success or fail */
522                         break;
523         }
524
525         if (val == 0x04) {
526                 err("firmware did not run");
527                 ret = -1;
528         } else if (val != 0x0c) {
529                 err("firmware boot timeout");
530                 ret = -1;
531         }
532
533 error:
534 exit:
535         return ret;
536 }
537
538 /* dump eeprom */
539 static int af9015_eeprom_dump(struct dvb_usb_device *d)
540 {
541         char buf[52], buf2[4];
542         u8 reg, val;
543
544         for (reg = 0; ; reg++) {
545                 if (reg % 16 == 0) {
546                         if (reg)
547                                 deb_info("%s\n", buf);
548                         sprintf(buf, "%02x: ", reg);
549                 }
550                 if (af9015_read_reg_i2c(d, AF9015_I2C_EEPROM, reg, &val) == 0)
551                         sprintf(buf2, "%02x ", val);
552                 else
553                         strcpy(buf2, "-- ");
554                 strcat(buf, buf2);
555                 if (reg == 0xff)
556                         break;
557         }
558         deb_info("%s\n", buf);
559         return 0;
560 }
561
562 static int af9015_download_ir_table(struct dvb_usb_device *d)
563 {
564         int i, packets = 0, ret;
565         u16 addr = 0x9a56; /* ir-table start address */
566         struct req_t req = {WRITE_MEMORY, 0, 0, 0, 0, 1, NULL};
567         u8 *data = NULL;
568         deb_info("%s:\n", __func__);
569
570         data = af9015_config.ir_table;
571         packets = af9015_config.ir_table_size;
572
573         /* no remote */
574         if (!packets)
575                 goto exit;
576
577         /* load remote ir-table */
578         for (i = 0; i < packets; i++) {
579                 req.addr = addr + i;
580                 req.data = &data[i];
581                 ret = af9015_ctrl_msg(d, &req);
582                 if (ret) {
583                         err("ir-table download failed at packet %d with " \
584                                 "code %d", i, ret);
585                         return ret;
586                 }
587         }
588
589 exit:
590         return 0;
591 }
592
593 static int af9015_init(struct dvb_usb_device *d)
594 {
595         int ret;
596         deb_info("%s:\n", __func__);
597
598         ret = af9015_init_endpoint(d);
599         if (ret)
600                 goto error;
601
602         ret = af9015_download_ir_table(d);
603         if (ret)
604                 goto error;
605
606 error:
607         return ret;
608 }
609
610 static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
611 {
612         int ret;
613         deb_info("%s: onoff:%d\n", __func__, onoff);
614
615         if (onoff)
616                 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
617         else
618                 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
619
620         return ret;
621 }
622
623 static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
624         int onoff)
625 {
626         int ret;
627         u8 idx;
628
629         deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
630                 __func__, index, pid, onoff);
631
632         ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
633         if (ret)
634                 goto error;
635
636         ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
637         if (ret)
638                 goto error;
639
640         idx = ((index & 0x1f) | (1 << 5));
641         ret = af9015_write_reg(adap->dev, 0xd504, idx);
642
643 error:
644         return ret;
645 }
646
647 static int af9015_download_firmware(struct usb_device *udev,
648         const struct firmware *fw)
649 {
650         int i, len, packets, remainder, ret;
651         struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
652         u16 addr = 0x5100; /* firmware start address */
653         u16 checksum = 0;
654
655         deb_info("%s:\n", __func__);
656
657         /* calc checksum */
658         for (i = 0; i < fw->size; i++)
659                 checksum += fw->data[i];
660
661         af9015_config.firmware_size = fw->size;
662         af9015_config.firmware_checksum = checksum;
663
664         #define FW_PACKET_MAX_DATA  55
665
666         packets = fw->size / FW_PACKET_MAX_DATA;
667         remainder = fw->size % FW_PACKET_MAX_DATA;
668         len = FW_PACKET_MAX_DATA;
669         for (i = 0; i <= packets; i++) {
670                 if (i == packets)  /* set size of the last packet */
671                         len = remainder;
672
673                 req.data_len = len;
674                 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
675                 req.addr = addr;
676                 addr += FW_PACKET_MAX_DATA;
677
678                 ret = af9015_rw_udev(udev, &req);
679                 if (ret) {
680                         err("firmware download failed at packet %d with " \
681                                 "code %d", i, ret);
682                         goto error;
683                 }
684         }
685
686         /* firmware loaded, request boot */
687         req.cmd = BOOT;
688         ret = af9015_rw_udev(udev, &req);
689         if (ret) {
690                 err("firmware boot failed:%d", ret);
691                 goto error;
692         }
693
694 error:
695         return ret;
696 }
697
698 static int af9015_read_config(struct usb_device *udev)
699 {
700         int ret;
701         u8 val, i, offset = 0;
702         struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
703         char manufacturer[10];
704
705         /* IR remote controller */
706         req.addr = AF9015_EEPROM_IR_MODE;
707         /* first message will timeout often due to possible hw bug */
708         for (i = 0; i < 4; i++) {
709                 ret = af9015_rw_udev(udev, &req);
710                 if (!ret)
711                         break;
712         }
713         if (ret)
714                 goto error;
715         deb_info("%s: IR mode:%d\n", __func__, val);
716         for (i = 0; i < af9015_properties_count; i++) {
717                 if (val == AF9015_IR_MODE_DISABLED || val == 0x04) {
718                         af9015_properties[i].rc_key_map = NULL;
719                         af9015_properties[i].rc_key_map_size  = 0;
720                 } else if (dvb_usb_af9015_remote) {
721                         /* load remote defined as module param */
722                         switch (dvb_usb_af9015_remote) {
723                         case AF9015_REMOTE_A_LINK_DTU_M:
724                                 af9015_properties[i].rc_key_map =
725                                   af9015_rc_keys_a_link;
726                                 af9015_properties[i].rc_key_map_size =
727                                   ARRAY_SIZE(af9015_rc_keys_a_link);
728                                 af9015_config.ir_table = af9015_ir_table_a_link;
729                                 af9015_config.ir_table_size =
730                                   ARRAY_SIZE(af9015_ir_table_a_link);
731                                 break;
732                         case AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3:
733                                 af9015_properties[i].rc_key_map =
734                                   af9015_rc_keys_msi;
735                                 af9015_properties[i].rc_key_map_size =
736                                   ARRAY_SIZE(af9015_rc_keys_msi);
737                                 af9015_config.ir_table = af9015_ir_table_msi;
738                                 af9015_config.ir_table_size =
739                                   ARRAY_SIZE(af9015_ir_table_msi);
740                                 break;
741                         case AF9015_REMOTE_MYGICTV_U718:
742                                 af9015_properties[i].rc_key_map =
743                                   af9015_rc_keys_mygictv;
744                                 af9015_properties[i].rc_key_map_size =
745                                   ARRAY_SIZE(af9015_rc_keys_mygictv);
746                                 af9015_config.ir_table =
747                                   af9015_ir_table_mygictv;
748                                 af9015_config.ir_table_size =
749                                   ARRAY_SIZE(af9015_ir_table_mygictv);
750                                 break;
751                         case AF9015_REMOTE_DIGITTRADE_DVB_T:
752                                 af9015_properties[i].rc_key_map =
753                                   af9015_rc_keys_digittrade;
754                                 af9015_properties[i].rc_key_map_size =
755                                   ARRAY_SIZE(af9015_rc_keys_digittrade);
756                                 af9015_config.ir_table =
757                                   af9015_ir_table_digittrade;
758                                 af9015_config.ir_table_size =
759                                   ARRAY_SIZE(af9015_ir_table_digittrade);
760                                 break;
761                         case AF9015_REMOTE_AVERMEDIA_KS:
762                                 af9015_properties[i].rc_key_map =
763                                   af9015_rc_keys_avermedia;
764                                 af9015_properties[i].rc_key_map_size =
765                                   ARRAY_SIZE(af9015_rc_keys_avermedia);
766                                 af9015_config.ir_table =
767                                   af9015_ir_table_avermedia_ks;
768                                 af9015_config.ir_table_size =
769                                   ARRAY_SIZE(af9015_ir_table_avermedia_ks);
770                                 break;
771                         }
772                 } else {
773                         switch (le16_to_cpu(udev->descriptor.idVendor)) {
774                         case USB_VID_LEADTEK:
775                                 af9015_properties[i].rc_key_map =
776                                   af9015_rc_keys_leadtek;
777                                 af9015_properties[i].rc_key_map_size =
778                                   ARRAY_SIZE(af9015_rc_keys_leadtek);
779                                 af9015_config.ir_table =
780                                   af9015_ir_table_leadtek;
781                                 af9015_config.ir_table_size =
782                                   ARRAY_SIZE(af9015_ir_table_leadtek);
783                                 break;
784                         case USB_VID_VISIONPLUS:
785                                 if (udev->descriptor.idProduct ==
786                                 cpu_to_le16(USB_PID_AZUREWAVE_AD_TU700)) {
787                                         af9015_properties[i].rc_key_map =
788                                           af9015_rc_keys_twinhan;
789                                         af9015_properties[i].rc_key_map_size =
790                                           ARRAY_SIZE(af9015_rc_keys_twinhan);
791                                         af9015_config.ir_table =
792                                           af9015_ir_table_twinhan;
793                                         af9015_config.ir_table_size =
794                                           ARRAY_SIZE(af9015_ir_table_twinhan);
795                                 }
796                                 break;
797                         case USB_VID_KWORLD_2:
798                                 /* TODO: use correct rc keys */
799                                 af9015_properties[i].rc_key_map =
800                                   af9015_rc_keys_twinhan;
801                                 af9015_properties[i].rc_key_map_size =
802                                   ARRAY_SIZE(af9015_rc_keys_twinhan);
803                                 af9015_config.ir_table = af9015_ir_table_kworld;
804                                 af9015_config.ir_table_size =
805                                   ARRAY_SIZE(af9015_ir_table_kworld);
806                                 break;
807                         /* Check USB manufacturer and product strings and try
808                            to determine correct remote in case of chip vendor
809                            reference IDs are used. */
810                         case USB_VID_AFATECH:
811                                 memset(manufacturer, 0, sizeof(manufacturer));
812                                 usb_string(udev, udev->descriptor.iManufacturer,
813                                         manufacturer, sizeof(manufacturer));
814                                 if (!strcmp("Geniatech", manufacturer)) {
815                                         /* iManufacturer 1 Geniatech
816                                            iProduct      2 AF9015 */
817                                         af9015_properties[i].rc_key_map =
818                                           af9015_rc_keys_mygictv;
819                                         af9015_properties[i].rc_key_map_size =
820                                           ARRAY_SIZE(af9015_rc_keys_mygictv);
821                                         af9015_config.ir_table =
822                                           af9015_ir_table_mygictv;
823                                         af9015_config.ir_table_size =
824                                           ARRAY_SIZE(af9015_ir_table_mygictv);
825                                 } else if (!strcmp("MSI", manufacturer)) {
826                                         /* iManufacturer 1 MSI
827                                            iProduct      2 MSI K-VOX */
828                                         af9015_properties[i].rc_key_map =
829                                           af9015_rc_keys_msi;
830                                         af9015_properties[i].rc_key_map_size =
831                                           ARRAY_SIZE(af9015_rc_keys_msi);
832                                         af9015_config.ir_table =
833                                           af9015_ir_table_msi;
834                                         af9015_config.ir_table_size =
835                                           ARRAY_SIZE(af9015_ir_table_msi);
836                                 } else if (udev->descriptor.idProduct ==
837                                         cpu_to_le16(USB_PID_TREKSTOR_DVBT)) {
838                                         af9015_properties[i].rc_key_map =
839                                           af9015_rc_keys_trekstor;
840                                         af9015_properties[i].rc_key_map_size =
841                                           ARRAY_SIZE(af9015_rc_keys_trekstor);
842                                         af9015_config.ir_table =
843                                           af9015_ir_table_trekstor;
844                                         af9015_config.ir_table_size =
845                                           ARRAY_SIZE(af9015_ir_table_trekstor);
846                                 }
847                                 break;
848                         case USB_VID_AVERMEDIA:
849                                 af9015_properties[i].rc_key_map =
850                                   af9015_rc_keys_avermedia;
851                                 af9015_properties[i].rc_key_map_size =
852                                   ARRAY_SIZE(af9015_rc_keys_avermedia);
853                                 af9015_config.ir_table =
854                                   af9015_ir_table_avermedia;
855                                 af9015_config.ir_table_size =
856                                   ARRAY_SIZE(af9015_ir_table_avermedia);
857                                 break;
858                         }
859                 }
860         }
861
862         /* TS mode - one or two receivers */
863         req.addr = AF9015_EEPROM_TS_MODE;
864         ret = af9015_rw_udev(udev, &req);
865         if (ret)
866                 goto error;
867         af9015_config.dual_mode = val;
868         deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
869
870         /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
871            size can be static because it is enabled only USB2.0 */
872         for (i = 0; i < af9015_properties_count; i++) {
873                 /* USB1.1 set smaller buffersize and disable 2nd adapter */
874                 if (udev->speed == USB_SPEED_FULL) {
875                         af9015_properties[i].adapter[0].stream.u.bulk.buffersize
876                                 = TS_USB11_MAX_PACKET_SIZE;
877                         /* disable 2nd adapter because we don't have
878                            PID-filters */
879                         af9015_config.dual_mode = 0;
880                 } else {
881                         af9015_properties[i].adapter[0].stream.u.bulk.buffersize
882                                 = TS_USB20_MAX_PACKET_SIZE;
883                 }
884         }
885
886         if (af9015_config.dual_mode) {
887                 /* read 2nd demodulator I2C address */
888                 req.addr = AF9015_EEPROM_DEMOD2_I2C;
889                 ret = af9015_rw_udev(udev, &req);
890                 if (ret)
891                         goto error;
892                 af9015_af9013_config[1].demod_address = val;
893
894                 /* enable 2nd adapter */
895                 for (i = 0; i < af9015_properties_count; i++)
896                         af9015_properties[i].num_adapters = 2;
897
898         } else {
899                  /* disable 2nd adapter */
900                 for (i = 0; i < af9015_properties_count; i++)
901                         af9015_properties[i].num_adapters = 1;
902         }
903
904         for (i = 0; i < af9015_properties[0].num_adapters; i++) {
905                 if (i == 1)
906                         offset = AF9015_EEPROM_OFFSET;
907                 /* xtal */
908                 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
909                 ret = af9015_rw_udev(udev, &req);
910                 if (ret)
911                         goto error;
912                 switch (val) {
913                 case 0:
914                         af9015_af9013_config[i].adc_clock = 28800;
915                         break;
916                 case 1:
917                         af9015_af9013_config[i].adc_clock = 20480;
918                         break;
919                 case 2:
920                         af9015_af9013_config[i].adc_clock = 28000;
921                         break;
922                 case 3:
923                         af9015_af9013_config[i].adc_clock = 25000;
924                         break;
925                 };
926                 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
927                         val, af9015_af9013_config[i].adc_clock);
928
929                 /* tuner IF */
930                 req.addr = AF9015_EEPROM_IF1H + offset;
931                 ret = af9015_rw_udev(udev, &req);
932                 if (ret)
933                         goto error;
934                 af9015_af9013_config[i].tuner_if = val << 8;
935                 req.addr = AF9015_EEPROM_IF1L + offset;
936                 ret = af9015_rw_udev(udev, &req);
937                 if (ret)
938                         goto error;
939                 af9015_af9013_config[i].tuner_if += val;
940                 deb_info("%s: [%d] IF1:%d\n", __func__, i,
941                         af9015_af9013_config[0].tuner_if);
942
943                 /* MT2060 IF1 */
944                 req.addr = AF9015_EEPROM_MT2060_IF1H  + offset;
945                 ret = af9015_rw_udev(udev, &req);
946                 if (ret)
947                         goto error;
948                 af9015_config.mt2060_if1[i] = val << 8;
949                 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
950                 ret = af9015_rw_udev(udev, &req);
951                 if (ret)
952                         goto error;
953                 af9015_config.mt2060_if1[i] += val;
954                 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
955                         af9015_config.mt2060_if1[i]);
956
957                 /* tuner */
958                 req.addr =  AF9015_EEPROM_TUNER_ID1 + offset;
959                 ret = af9015_rw_udev(udev, &req);
960                 if (ret)
961                         goto error;
962                 switch (val) {
963                 case AF9013_TUNER_ENV77H11D5:
964                 case AF9013_TUNER_MT2060:
965                 case AF9013_TUNER_QT1010:
966                 case AF9013_TUNER_UNKNOWN:
967                 case AF9013_TUNER_MT2060_2:
968                 case AF9013_TUNER_TDA18271:
969                 case AF9013_TUNER_QT1010A:
970                         af9015_af9013_config[i].rf_spec_inv = 1;
971                         break;
972                 case AF9013_TUNER_MXL5003D:
973                 case AF9013_TUNER_MXL5005D:
974                 case AF9013_TUNER_MXL5005R:
975                         af9015_af9013_config[i].rf_spec_inv = 0;
976                         break;
977                 case AF9013_TUNER_MC44S803:
978                         af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
979                         af9015_af9013_config[i].rf_spec_inv = 1;
980                         break;
981                 default:
982                         warn("tuner id:%d not supported, please report!", val);
983                         return -ENODEV;
984                 };
985
986                 af9015_af9013_config[i].tuner = val;
987                 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
988         }
989
990 error:
991         if (ret)
992                 err("eeprom read failed:%d", ret);
993
994         return ret;
995 }
996
997 static int af9015_identify_state(struct usb_device *udev,
998                                  struct dvb_usb_device_properties *props,
999                                  struct dvb_usb_device_description **desc,
1000                                  int *cold)
1001 {
1002         int ret;
1003         u8 reply;
1004         struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
1005
1006         ret = af9015_rw_udev(udev, &req);
1007         if (ret)
1008                 return ret;
1009
1010         deb_info("%s: reply:%02x\n", __func__, reply);
1011         if (reply == 0x02)
1012                 *cold = 0;
1013         else
1014                 *cold = 1;
1015
1016         return ret;
1017 }
1018
1019 static int af9015_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
1020 {
1021         u8 buf[8];
1022         struct req_t req = {GET_IR_CODE, 0, 0, 0, 0, sizeof(buf), buf};
1023         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
1024         int i, ret;
1025
1026         memset(buf, 0, sizeof(buf));
1027
1028         ret = af9015_ctrl_msg(d, &req);
1029         if (ret)
1030                 return ret;
1031
1032         *event = 0;
1033         *state = REMOTE_NO_KEY_PRESSED;
1034
1035         for (i = 0; i < d->props.rc_key_map_size; i++) {
1036                 if (!buf[1] && keymap[i].custom == buf[0] &&
1037                     keymap[i].data == buf[2]) {
1038                         *event = keymap[i].event;
1039                         *state = REMOTE_KEY_PRESSED;
1040                         break;
1041                 }
1042         }
1043         if (!buf[1])
1044                 deb_rc("%s: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1045                         __func__, buf[0], buf[1], buf[2], buf[3], buf[4],
1046                         buf[5], buf[6], buf[7]);
1047
1048         return 0;
1049 }
1050
1051 /* init 2nd I2C adapter */
1052 static int af9015_i2c_init(struct dvb_usb_device *d)
1053 {
1054         int ret;
1055         struct af9015_state *state = d->priv;
1056         deb_info("%s:\n", __func__);
1057
1058         strncpy(state->i2c_adap.name, d->desc->name,
1059                 sizeof(state->i2c_adap.name));
1060 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
1061         state->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
1062 #else
1063         state->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
1064 #endif
1065         state->i2c_adap.algo      = d->props.i2c_algo;
1066         state->i2c_adap.algo_data = NULL;
1067         state->i2c_adap.dev.parent = &d->udev->dev;
1068
1069         i2c_set_adapdata(&state->i2c_adap, d);
1070
1071         ret = i2c_add_adapter(&state->i2c_adap);
1072         if (ret < 0)
1073                 err("could not add i2c adapter");
1074
1075         return ret;
1076 }
1077
1078 static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1079 {
1080         int ret;
1081         struct af9015_state *state = adap->dev->priv;
1082         struct i2c_adapter *i2c_adap;
1083
1084         if (adap->id == 0) {
1085                 /* select I2C adapter */
1086                 i2c_adap = &adap->dev->i2c_adap;
1087
1088                 deb_info("%s: init I2C\n", __func__);
1089                 ret = af9015_i2c_init(adap->dev);
1090
1091                 /* dump eeprom (debug) */
1092                 ret = af9015_eeprom_dump(adap->dev);
1093                 if (ret)
1094                         return ret;
1095         } else {
1096                 /* select I2C adapter */
1097                 i2c_adap = &state->i2c_adap;
1098
1099                 /* copy firmware to 2nd demodulator */
1100                 if (af9015_config.dual_mode) {
1101                         ret = af9015_copy_firmware(adap->dev);
1102                         if (ret) {
1103                                 err("firmware copy to 2nd frontend " \
1104                                         "failed, will disable it");
1105                                 af9015_config.dual_mode = 0;
1106                                 return -ENODEV;
1107                         }
1108                 } else {
1109                         return -ENODEV;
1110                 }
1111         }
1112
1113         /* attach demodulator */
1114         adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1115                 i2c_adap);
1116
1117         return adap->fe == NULL ? -ENODEV : 0;
1118 }
1119
1120 static struct mt2060_config af9015_mt2060_config = {
1121         .i2c_address = 0xc0,
1122         .clock_out = 0,
1123 };
1124
1125 static struct qt1010_config af9015_qt1010_config = {
1126         .i2c_address = 0xc4,
1127 };
1128
1129 static struct tda18271_config af9015_tda18271_config = {
1130         .gate = TDA18271_GATE_DIGITAL,
1131         .small_i2c = 1,
1132 };
1133
1134 static struct mxl5005s_config af9015_mxl5003_config = {
1135         .i2c_address     = 0xc6,
1136         .if_freq         = IF_FREQ_4570000HZ,
1137         .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
1138         .agc_mode        = MXL_SINGLE_AGC,
1139         .tracking_filter = MXL_TF_DEFAULT,
1140         .rssi_enable     = MXL_RSSI_ENABLE,
1141         .cap_select      = MXL_CAP_SEL_ENABLE,
1142         .div_out         = MXL_DIV_OUT_4,
1143         .clock_out       = MXL_CLOCK_OUT_DISABLE,
1144         .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1145         .top             = MXL5005S_TOP_25P2,
1146         .mod_mode        = MXL_DIGITAL_MODE,
1147         .if_mode         = MXL_ZERO_IF,
1148         .AgcMasterByte   = 0x00,
1149 };
1150
1151 static struct mxl5005s_config af9015_mxl5005_config = {
1152         .i2c_address     = 0xc6,
1153         .if_freq         = IF_FREQ_4570000HZ,
1154         .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
1155         .agc_mode        = MXL_SINGLE_AGC,
1156         .tracking_filter = MXL_TF_OFF,
1157         .rssi_enable     = MXL_RSSI_ENABLE,
1158         .cap_select      = MXL_CAP_SEL_ENABLE,
1159         .div_out         = MXL_DIV_OUT_4,
1160         .clock_out       = MXL_CLOCK_OUT_DISABLE,
1161         .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1162         .top             = MXL5005S_TOP_25P2,
1163         .mod_mode        = MXL_DIGITAL_MODE,
1164         .if_mode         = MXL_ZERO_IF,
1165         .AgcMasterByte   = 0x00,
1166 };
1167
1168 static struct mc44s803_config af9015_mc44s803_config = {
1169         .i2c_address = 0xc0,
1170         .dig_out = 1,
1171 };
1172
1173 static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1174 {
1175         struct af9015_state *state = adap->dev->priv;
1176         struct i2c_adapter *i2c_adap;
1177         int ret;
1178         deb_info("%s: \n", __func__);
1179
1180         /* select I2C adapter */
1181         if (adap->id == 0)
1182                 i2c_adap = &adap->dev->i2c_adap;
1183         else
1184                 i2c_adap = &state->i2c_adap;
1185
1186         switch (af9015_af9013_config[adap->id].tuner) {
1187         case AF9013_TUNER_MT2060:
1188         case AF9013_TUNER_MT2060_2:
1189                 ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
1190                         &af9015_mt2060_config,
1191                         af9015_config.mt2060_if1[adap->id])
1192                         == NULL ? -ENODEV : 0;
1193                 break;
1194         case AF9013_TUNER_QT1010:
1195         case AF9013_TUNER_QT1010A:
1196                 ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
1197                         &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1198                 break;
1199         case AF9013_TUNER_TDA18271:
1200                 ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
1201                         &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1202                 break;
1203         case AF9013_TUNER_MXL5003D:
1204                 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1205                         &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1206                 break;
1207         case AF9013_TUNER_MXL5005D:
1208         case AF9013_TUNER_MXL5005R:
1209                 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1210                         &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1211                 break;
1212         case AF9013_TUNER_ENV77H11D5:
1213                 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1214                         DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1215                 break;
1216         case AF9013_TUNER_MC44S803:
1217                 ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
1218                         &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
1219                 break;
1220         case AF9013_TUNER_UNKNOWN:
1221         default:
1222                 ret = -ENODEV;
1223                 err("Unknown tuner id:%d",
1224                         af9015_af9013_config[adap->id].tuner);
1225         }
1226         return ret;
1227 }
1228
1229 static struct usb_device_id af9015_usb_table[] = {
1230 /*  0 */{USB_DEVICE(USB_VID_AFATECH,   USB_PID_AFATECH_AF9015_9015)},
1231         {USB_DEVICE(USB_VID_AFATECH,   USB_PID_AFATECH_AF9015_9016)},
1232         {USB_DEVICE(USB_VID_LEADTEK,   USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1233         {USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV71E)},
1234         {USB_DEVICE(USB_VID_KWORLD_2,  USB_PID_KWORLD_399U)},
1235 /*  5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1236                 USB_PID_TINYTWIN)},
1237         {USB_DEVICE(USB_VID_VISIONPLUS,
1238                 USB_PID_AZUREWAVE_AD_TU700)},
1239         {USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1240         {USB_DEVICE(USB_VID_KWORLD_2,  USB_PID_KWORLD_PC160_2T)},
1241         {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1242 /* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1243         {USB_DEVICE(USB_VID_MSI_2,     USB_PID_MSI_DIGIVOX_DUO)},
1244         {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
1245         {USB_DEVICE(USB_VID_TELESTAR,  USB_PID_TELESTAR_STARSTICK_2)},
1246         {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
1247 /* 15 */{USB_DEVICE(USB_VID_MSI_2,     USB_PID_MSI_DIGI_VOX_MINI_III)},
1248         {USB_DEVICE(USB_VID_KWORLD_2,  USB_PID_KWORLD_395U)},
1249         {USB_DEVICE(USB_VID_KWORLD_2,  USB_PID_KWORLD_395U_2)},
1250         {USB_DEVICE(USB_VID_KWORLD_2,  USB_PID_KWORLD_395U_3)},
1251         {USB_DEVICE(USB_VID_AFATECH,   USB_PID_TREKSTOR_DVBT)},
1252         {0},
1253 };
1254 MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1255
1256 static struct dvb_usb_device_properties af9015_properties[] = {
1257         {
1258                 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1259
1260                 .usb_ctrl = DEVICE_SPECIFIC,
1261                 .download_firmware = af9015_download_firmware,
1262                 .firmware = "dvb-usb-af9015.fw",
1263                 .no_reconnect = 1,
1264
1265                 .size_of_priv = sizeof(struct af9015_state), \
1266
1267                 .num_adapters = 2,
1268                 .adapter = {
1269                         {
1270                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1271                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1272
1273                                 .pid_filter_count = 32,
1274                                 .pid_filter       = af9015_pid_filter,
1275                                 .pid_filter_ctrl  = af9015_pid_filter_ctrl,
1276
1277                                 .frontend_attach =
1278                                         af9015_af9013_frontend_attach,
1279                                 .tuner_attach    = af9015_tuner_attach,
1280                                 .stream = {
1281                                         .type = USB_BULK,
1282                                         .count = 6,
1283                                         .endpoint = 0x84,
1284                                 },
1285                         },
1286                         {
1287                                 .frontend_attach =
1288                                         af9015_af9013_frontend_attach,
1289                                 .tuner_attach    = af9015_tuner_attach,
1290                                 .stream = {
1291                                         .type = USB_BULK,
1292                                         .count = 6,
1293                                         .endpoint = 0x85,
1294                                         .u = {
1295                                                 .bulk = {
1296                                                         .buffersize =
1297                                                 TS_USB20_MAX_PACKET_SIZE,
1298                                                 }
1299                                         }
1300                                 },
1301                         }
1302                 },
1303
1304                 .identify_state = af9015_identify_state,
1305
1306                 .rc_query         = af9015_rc_query,
1307                 .rc_interval      = 150,
1308
1309                 .i2c_algo = &af9015_i2c_algo,
1310
1311                 .num_device_descs = 9,
1312                 .devices = {
1313                         {
1314                                 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1315                                 .cold_ids = {&af9015_usb_table[0],
1316                                              &af9015_usb_table[1], NULL},
1317                                 .warm_ids = {NULL},
1318                         },
1319                         {
1320                                 .name = "Leadtek WinFast DTV Dongle Gold",
1321                                 .cold_ids = {&af9015_usb_table[2], NULL},
1322                                 .warm_ids = {NULL},
1323                         },
1324                         {
1325                                 .name = "Pinnacle PCTV 71e",
1326                                 .cold_ids = {&af9015_usb_table[3], NULL},
1327                                 .warm_ids = {NULL},
1328                         },
1329                         {
1330                                 .name = "KWorld PlusTV Dual DVB-T Stick " \
1331                                         "(DVB-T 399U)",
1332                                 .cold_ids = {&af9015_usb_table[4], NULL},
1333                                 .warm_ids = {NULL},
1334                         },
1335                         {
1336                                 .name = "DigitalNow TinyTwin DVB-T Receiver",
1337                                 .cold_ids = {&af9015_usb_table[5], NULL},
1338                                 .warm_ids = {NULL},
1339                         },
1340                         {
1341                                 .name = "TwinHan AzureWave AD-TU700(704J)",
1342                                 .cold_ids = {&af9015_usb_table[6], NULL},
1343                                 .warm_ids = {NULL},
1344                         },
1345                         {
1346                                 .name = "TerraTec Cinergy T USB XE",
1347                                 .cold_ids = {&af9015_usb_table[7], NULL},
1348                                 .warm_ids = {NULL},
1349                         },
1350                         {
1351                                 .name = "KWorld PlusTV Dual DVB-T PCI " \
1352                                         "(DVB-T PC160-2T)",
1353                                 .cold_ids = {&af9015_usb_table[8], NULL},
1354                                 .warm_ids = {NULL},
1355                         },
1356                         {
1357                                 .name = "AVerMedia AVerTV DVB-T Volar X",
1358                                 .cold_ids = {&af9015_usb_table[9], NULL},
1359                                 .warm_ids = {NULL},
1360                         },
1361                 }
1362         }, {
1363                 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1364
1365                 .usb_ctrl = DEVICE_SPECIFIC,
1366                 .download_firmware = af9015_download_firmware,
1367                 .firmware = "dvb-usb-af9015.fw",
1368                 .no_reconnect = 1,
1369
1370                 .size_of_priv = sizeof(struct af9015_state), \
1371
1372                 .num_adapters = 2,
1373                 .adapter = {
1374                         {
1375                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1376                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1377
1378                                 .pid_filter_count = 32,
1379                                 .pid_filter       = af9015_pid_filter,
1380                                 .pid_filter_ctrl  = af9015_pid_filter_ctrl,
1381
1382                                 .frontend_attach =
1383                                         af9015_af9013_frontend_attach,
1384                                 .tuner_attach    = af9015_tuner_attach,
1385                                 .stream = {
1386                                         .type = USB_BULK,
1387                                         .count = 6,
1388                                         .endpoint = 0x84,
1389                                 },
1390                         },
1391                         {
1392                                 .frontend_attach =
1393                                         af9015_af9013_frontend_attach,
1394                                 .tuner_attach    = af9015_tuner_attach,
1395                                 .stream = {
1396                                         .type = USB_BULK,
1397                                         .count = 6,
1398                                         .endpoint = 0x85,
1399                                         .u = {
1400                                                 .bulk = {
1401                                                         .buffersize =
1402                                                 TS_USB20_MAX_PACKET_SIZE,
1403                                                 }
1404                                         }
1405                                 },
1406                         }
1407                 },
1408
1409                 .identify_state = af9015_identify_state,
1410
1411                 .rc_query         = af9015_rc_query,
1412                 .rc_interval      = 150,
1413
1414                 .i2c_algo = &af9015_i2c_algo,
1415
1416                 .num_device_descs = 8,
1417                 .devices = {
1418                         {
1419                                 .name = "Xtensions XD-380",
1420                                 .cold_ids = {&af9015_usb_table[10], NULL},
1421                                 .warm_ids = {NULL},
1422                         },
1423                         {
1424                                 .name = "MSI DIGIVOX Duo",
1425                                 .cold_ids = {&af9015_usb_table[11], NULL},
1426                                 .warm_ids = {NULL},
1427                         },
1428                         {
1429                                 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1430                                 .cold_ids = {&af9015_usb_table[12], NULL},
1431                                 .warm_ids = {NULL},
1432                         },
1433                         {
1434                                 .name = "Telestar Starstick 2",
1435                                 .cold_ids = {&af9015_usb_table[13], NULL},
1436                                 .warm_ids = {NULL},
1437                         },
1438                         {
1439                                 .name = "AVerMedia A309",
1440                                 .cold_ids = {&af9015_usb_table[14], NULL},
1441                                 .warm_ids = {NULL},
1442                         },
1443                         {
1444                                 .name = "MSI Digi VOX mini III",
1445                                 .cold_ids = {&af9015_usb_table[15], NULL},
1446                                 .warm_ids = {NULL},
1447                         },
1448                         {
1449                                 .name = "KWorld USB DVB-T TV Stick II " \
1450                                         "(VS-DVB-T 395U)",
1451                                 .cold_ids = {&af9015_usb_table[16],
1452                                              &af9015_usb_table[17],
1453                                              &af9015_usb_table[18], NULL},
1454                                 .warm_ids = {NULL},
1455                         },
1456                         {
1457                                 .name = "TrekStor DVB-T USB Stick",
1458                                 .cold_ids = {&af9015_usb_table[19], NULL},
1459                                 .warm_ids = {NULL},
1460                         },
1461                 }
1462         }
1463 };
1464
1465 static int af9015_usb_probe(struct usb_interface *intf,
1466                             const struct usb_device_id *id)
1467 {
1468         int ret = 0;
1469         struct dvb_usb_device *d = NULL;
1470         struct usb_device *udev = interface_to_usbdev(intf);
1471         u8 i;
1472
1473         deb_info("%s: interface:%d\n", __func__,
1474                 intf->cur_altsetting->desc.bInterfaceNumber);
1475
1476         /* interface 0 is used by DVB-T receiver and
1477            interface 1 is for remote controller (HID) */
1478         if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1479                 ret = af9015_read_config(udev);
1480                 if (ret)
1481                         return ret;
1482
1483                 for (i = 0; i < af9015_properties_count; i++) {
1484                         ret = dvb_usb_device_init(intf, &af9015_properties[i],
1485                                 THIS_MODULE, &d, adapter_nr);
1486                         if (!ret)
1487                                 break;
1488                         if (ret != -ENODEV)
1489                                 return ret;
1490                 }
1491                 if (ret)
1492                         return ret;
1493
1494                 if (d)
1495                         ret = af9015_init(d);
1496         }
1497
1498         return ret;
1499 }
1500
1501 static void af9015_i2c_exit(struct dvb_usb_device *d)
1502 {
1503         struct af9015_state *state = d->priv;
1504         deb_info("%s: \n", __func__);
1505
1506         /* remove 2nd I2C adapter */
1507         if (d->state & DVB_USB_STATE_I2C)
1508                 i2c_del_adapter(&state->i2c_adap);
1509 }
1510
1511 static void af9015_usb_device_exit(struct usb_interface *intf)
1512 {
1513         struct dvb_usb_device *d = usb_get_intfdata(intf);
1514         deb_info("%s: \n", __func__);
1515
1516         /* remove 2nd I2C adapter */
1517         if (d != NULL && d->desc != NULL)
1518                 af9015_i2c_exit(d);
1519
1520         dvb_usb_device_exit(intf);
1521 }
1522
1523 /* usb specific object needed to register this driver with the usb subsystem */
1524 static struct usb_driver af9015_usb_driver = {
1525         .name = "dvb_usb_af9015",
1526         .probe = af9015_usb_probe,
1527         .disconnect = af9015_usb_device_exit,
1528         .id_table = af9015_usb_table,
1529 };
1530
1531 /* module stuff */
1532 static int __init af9015_usb_module_init(void)
1533 {
1534         int ret;
1535         ret = usb_register(&af9015_usb_driver);
1536         if (ret)
1537                 err("module init failed:%d", ret);
1538
1539         return ret;
1540 }
1541
1542 static void __exit af9015_usb_module_exit(void)
1543 {
1544         /* deregister this driver from the USB subsystem */
1545         usb_deregister(&af9015_usb_driver);
1546 }
1547
1548 module_init(af9015_usb_module_init);
1549 module_exit(af9015_usb_module_exit);
1550
1551 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1552 MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1553 MODULE_LICENSE("GPL");