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