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