FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / media / dvb / dvb-usb / af9015.c
blob100ebc37e99e2293699816a15556fdc672640e05
1 /*
2 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
6 * Thanks to Afatech who kindly provided information.
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.
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.
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.
24 #include <linux/hash.h>
25 #include <linux/slab.h>
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"
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);
45 static DEFINE_MUTEX(af9015_usb_mutex);
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);
51 static struct af9013_config af9015_af9013_config[] = {
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,
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,
67 static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
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 */
78 if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
79 return -EAGAIN;
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;
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;
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;
126 /* write requested */
127 if (write) {
128 memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
129 msg_len += req->data_len;
132 deb_xfer(">>> ");
133 debug_dump(buf, msg_len, deb_xfer);
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;
146 /* no ack for those packets */
147 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
148 goto exit_unlock;
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;
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;
164 deb_xfer("<<< ");
165 debug_dump(buf, act_len, deb_xfer);
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 */
174 /* check status */
175 if (buf[1]) {
176 err("command failed:%d", buf[1]);
177 ret = -1;
178 goto error_unlock;
181 /* read request, copy returned data to return buf */
182 if (!write)
183 memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
185 error_unlock:
186 exit_unlock:
187 mutex_unlock(&af9015_usb_mutex);
189 return ret;
192 static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
194 return af9015_rw_udev(d->udev, req);
197 static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
198 u8 len)
200 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
201 val};
202 return af9015_ctrl_msg(d, &req);
205 static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
207 return af9015_write_regs(d, addr, &val, 1);
210 static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
212 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
213 val};
214 return af9015_ctrl_msg(d, &req);
217 static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
219 return af9015_read_regs(d, addr, val, 1);
222 static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
223 u8 val)
225 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
227 if (addr == af9015_af9013_config[0].demod_address ||
228 addr == af9015_af9013_config[1].demod_address)
229 req.addr_len = 3;
231 return af9015_ctrl_msg(d, &req);
234 static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
235 u8 *val)
237 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
239 if (addr == af9015_af9013_config[0].demod_address ||
240 addr == af9015_af9013_config[1].demod_address)
241 req.addr_len = 3;
243 return af9015_ctrl_msg(d, &req);
246 static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
247 int num)
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;
255 /* TODO: implement bus lock
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.
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 |____________| |____________|
278 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
279 return -EAGAIN;
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 */
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;
338 if (ret)
339 goto error;
342 ret = i;
344 error:
345 mutex_unlock(&d->i2c_mutex);
347 return ret;
350 static u32 af9015_i2c_func(struct i2c_adapter *adapter)
352 return I2C_FUNC_I2C;
355 static struct i2c_algorithm af9015_i2c_algo = {
356 .master_xfer = af9015_i2c_xfer,
357 .functionality = af9015_i2c_func,
360 static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
362 int ret;
363 u8 val, mask = 0x01;
365 ret = af9015_read_reg(d, addr, &val);
366 if (ret)
367 return ret;
369 mask <<= bit;
370 if (op) {
371 /* set bit */
372 val |= mask;
373 } else {
374 /* clear bit */
375 mask ^= 0xff;
376 val &= mask;
379 return af9015_write_reg(d, addr, val);
382 static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
384 return af9015_do_reg_bit(d, addr, bit, 1);
387 static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
389 return af9015_do_reg_bit(d, addr, bit, 0);
392 static int af9015_init_endpoint(struct dvb_usb_device *d)
394 int ret;
395 u16 frame_size;
396 u8 packet_size;
397 deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
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
403 #define TS_USB20_PACKET_COUNT 87
404 #define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
406 #define TS_USB11_PACKET_COUNT 5
407 #define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
409 #define TS_USB20_MAX_PACKET_SIZE 512
410 #define TS_USB11_MAX_PACKET_SIZE 64
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;
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;
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;
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;
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);
483 error:
484 if (ret)
485 err("endpoint init failed:%d", ret);
486 return ret;
489 static int af9015_copy_firmware(struct dvb_usb_device *d)
491 int ret;
492 u8 fw_params[4];
493 u8 val, i;
494 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
495 fw_params };
496 deb_info("%s:\n", __func__);
498 fw_params[0] = af9015_config.firmware_size >> 8;
499 fw_params[1] = af9015_config.firmware_size & 0xff;
500 fw_params[2] = af9015_config.firmware_checksum >> 8;
501 fw_params[3] = af9015_config.firmware_checksum & 0xff;
503 /* wait 2nd demodulator ready */
504 msleep(100);
506 ret = af9015_read_reg_i2c(d,
507 af9015_af9013_config[1].demod_address, 0x98be, &val);
508 if (ret)
509 goto error;
510 else
511 deb_info("%s: firmware status:%02x\n", __func__, val);
513 if (val == 0x0c) /* fw is running, no need for download */
514 goto exit;
516 /* set I2C master clock to fast (to speed up firmware copy) */
517 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
518 if (ret)
519 goto error;
521 msleep(50);
523 /* copy firmware */
524 ret = af9015_ctrl_msg(d, &req);
525 if (ret)
526 err("firmware copy cmd failed:%d", ret);
527 deb_info("%s: firmware copy done\n", __func__);
529 /* set I2C master clock back to normal */
530 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
531 if (ret)
532 goto error;
534 /* request boot firmware */
535 ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
536 0xe205, 1);
537 deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
538 if (ret)
539 goto error;
541 for (i = 0; i < 15; i++) {
542 msleep(100);
544 /* check firmware status */
545 ret = af9015_read_reg_i2c(d,
546 af9015_af9013_config[1].demod_address, 0x98be, &val);
547 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
548 __func__, ret, val);
549 if (ret)
550 goto error;
552 if (val == 0x0c || val == 0x04) /* success or fail */
553 break;
556 if (val == 0x04) {
557 err("firmware did not run");
558 ret = -1;
559 } else if (val != 0x0c) {
560 err("firmware boot timeout");
561 ret = -1;
564 error:
565 exit:
566 return ret;
569 /* hash (and dump) eeprom */
570 static int af9015_eeprom_hash(struct usb_device *udev)
572 static const unsigned int eeprom_size = 256;
573 unsigned int reg;
574 int ret;
575 u8 val, *eeprom;
576 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
578 eeprom = kmalloc(eeprom_size, GFP_KERNEL);
579 if (eeprom == NULL)
580 return -ENOMEM;
582 for (reg = 0; reg < eeprom_size; reg++) {
583 req.addr = reg;
584 ret = af9015_rw_udev(udev, &req);
585 if (ret)
586 goto free;
587 eeprom[reg] = val;
590 if (dvb_usb_af9015_debug & 0x01)
591 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, eeprom,
592 eeprom_size);
594 BUG_ON(eeprom_size % 4);
596 af9015_config.eeprom_sum = 0;
597 for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
598 af9015_config.eeprom_sum *= GOLDEN_RATIO_PRIME_32;
599 af9015_config.eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
602 deb_info("%s: eeprom sum=%.8x\n", __func__, af9015_config.eeprom_sum);
604 ret = 0;
605 free:
606 kfree(eeprom);
607 return ret;
610 static int af9015_init(struct dvb_usb_device *d)
612 int ret;
613 deb_info("%s:\n", __func__);
615 /* init RC canary */
616 ret = af9015_write_reg(d, 0x98e9, 0xff);
617 if (ret)
618 goto error;
620 ret = af9015_init_endpoint(d);
621 if (ret)
622 goto error;
624 error:
625 return ret;
628 static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
630 int ret;
631 deb_info("%s: onoff:%d\n", __func__, onoff);
633 if (onoff)
634 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
635 else
636 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
638 return ret;
641 static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
642 int onoff)
644 int ret;
645 u8 idx;
647 deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
648 __func__, index, pid, onoff);
650 ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
651 if (ret)
652 goto error;
654 ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
655 if (ret)
656 goto error;
658 idx = ((index & 0x1f) | (1 << 5));
659 ret = af9015_write_reg(adap->dev, 0xd504, idx);
661 error:
662 return ret;
665 static int af9015_download_firmware(struct usb_device *udev,
666 const struct firmware *fw)
668 int i, len, remaining, ret;
669 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
670 u16 checksum = 0;
672 deb_info("%s:\n", __func__);
674 /* calc checksum */
675 for (i = 0; i < fw->size; i++)
676 checksum += fw->data[i];
678 af9015_config.firmware_size = fw->size;
679 af9015_config.firmware_checksum = checksum;
681 #define FW_ADDR 0x5100 /* firmware start address */
682 #define LEN_MAX 55 /* max packet size */
683 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
684 len = remaining;
685 if (len > LEN_MAX)
686 len = LEN_MAX;
688 req.data_len = len;
689 req.data = (u8 *) &fw->data[fw->size - remaining];
690 req.addr = FW_ADDR + fw->size - remaining;
692 ret = af9015_rw_udev(udev, &req);
693 if (ret) {
694 err("firmware download failed:%d", ret);
695 goto error;
699 /* firmware loaded, request boot */
700 req.cmd = BOOT;
701 ret = af9015_rw_udev(udev, &req);
702 if (ret) {
703 err("firmware boot failed:%d", ret);
704 goto error;
707 error:
708 return ret;
711 struct af9015_rc_setup {
712 unsigned int id;
713 char *rc_codes;
716 static char *af9015_rc_setup_match(unsigned int id,
717 const struct af9015_rc_setup *table)
719 for (; table->rc_codes; table++)
720 if (table->id == id)
721 return table->rc_codes;
722 return NULL;
725 static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
726 { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
727 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
728 { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
729 { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
730 { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
734 static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
735 { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
736 { 0xa3703d00, RC_MAP_ALINK_DTU_M },
737 { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
741 static const struct af9015_rc_setup af9015_rc_setup_usbids[] = {
742 { (USB_VID_TERRATEC << 16) + USB_PID_TERRATEC_CINERGY_T_STICK_RC,
743 RC_MAP_TERRATEC_SLIM_2 },
744 { (USB_VID_TERRATEC << 16) + USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
745 RC_MAP_TERRATEC_SLIM },
746 { (USB_VID_VISIONPLUS << 16) + USB_PID_AZUREWAVE_AD_TU700,
747 RC_MAP_AZUREWAVE_AD_TU700 },
748 { (USB_VID_VISIONPLUS << 16) + USB_PID_TINYTWIN,
749 RC_MAP_AZUREWAVE_AD_TU700 },
750 { (USB_VID_MSI_2 << 16) + USB_PID_MSI_DIGI_VOX_MINI_III,
751 RC_MAP_MSI_DIGIVOX_III },
752 { (USB_VID_LEADTEK << 16) + USB_PID_WINFAST_DTV_DONGLE_GOLD,
753 RC_MAP_LEADTEK_Y04G0051 },
754 { (USB_VID_AVERMEDIA << 16) + USB_PID_AVERMEDIA_VOLAR_X,
755 RC_MAP_AVERMEDIA_M135A },
756 { (USB_VID_AFATECH << 16) + USB_PID_TREKSTOR_DVBT,
757 RC_MAP_TREKSTOR },
758 { (USB_VID_KWORLD_2 << 16) + USB_PID_TINYTWIN_2,
759 RC_MAP_DIGITALNOW_TINYTWIN },
760 { (USB_VID_GTEK << 16) + USB_PID_TINYTWIN_3,
761 RC_MAP_DIGITALNOW_TINYTWIN },
765 static void af9015_set_remote_config(struct usb_device *udev,
766 struct dvb_usb_device_properties *props)
768 u16 vid = le16_to_cpu(udev->descriptor.idVendor);
769 u16 pid = le16_to_cpu(udev->descriptor.idProduct);
771 /* try to load remote based module param */
772 props->rc.core.rc_codes = af9015_rc_setup_match(
773 dvb_usb_af9015_remote, af9015_rc_setup_modparam);
775 /* try to load remote based eeprom hash */
776 if (!props->rc.core.rc_codes)
777 props->rc.core.rc_codes = af9015_rc_setup_match(
778 af9015_config.eeprom_sum, af9015_rc_setup_hashes);
780 /* try to load remote based USB ID */
781 if (!props->rc.core.rc_codes)
782 props->rc.core.rc_codes = af9015_rc_setup_match(
783 (vid << 16) + pid, af9015_rc_setup_usbids);
785 /* try to load remote based USB iManufacturer string */
786 if (!props->rc.core.rc_codes && vid == USB_VID_AFATECH) {
787 /* Check USB manufacturer and product strings and try
788 to determine correct remote in case of chip vendor
789 reference IDs are used.
790 DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */
791 char manufacturer[10];
792 memset(manufacturer, 0, sizeof(manufacturer));
793 usb_string(udev, udev->descriptor.iManufacturer,
794 manufacturer, sizeof(manufacturer));
795 if (!strcmp("MSI", manufacturer)) {
796 /* iManufacturer 1 MSI
797 iProduct 2 MSI K-VOX */
798 props->rc.core.rc_codes = af9015_rc_setup_match(
799 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
800 af9015_rc_setup_modparam);
804 /* finally load "empty" just for leaving IR receiver enabled */
805 if (!props->rc.core.rc_codes)
806 props->rc.core.rc_codes = RC_MAP_EMPTY;
808 return;
811 static int af9015_read_config(struct usb_device *udev)
813 int ret;
814 u8 val, i, offset = 0;
815 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
817 /* IR remote controller */
818 req.addr = AF9015_EEPROM_IR_MODE;
819 /* first message will timeout often due to possible hw bug */
820 for (i = 0; i < 4; i++) {
821 ret = af9015_rw_udev(udev, &req);
822 if (!ret)
823 break;
825 if (ret)
826 goto error;
828 ret = af9015_eeprom_hash(udev);
829 if (ret)
830 goto error;
832 deb_info("%s: IR mode:%d\n", __func__, val);
833 for (i = 0; i < af9015_properties_count; i++) {
834 if (val == AF9015_IR_MODE_DISABLED)
835 af9015_properties[i].rc.core.rc_codes = NULL;
836 else
837 af9015_set_remote_config(udev, &af9015_properties[i]);
840 /* TS mode - one or two receivers */
841 req.addr = AF9015_EEPROM_TS_MODE;
842 ret = af9015_rw_udev(udev, &req);
843 if (ret)
844 goto error;
845 af9015_config.dual_mode = val;
846 deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
848 /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
849 size can be static because it is enabled only USB2.0 */
850 for (i = 0; i < af9015_properties_count; i++) {
851 /* USB1.1 set smaller buffersize and disable 2nd adapter */
852 if (udev->speed == USB_SPEED_FULL) {
853 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
854 = TS_USB11_FRAME_SIZE;
855 /* disable 2nd adapter because we don't have
856 PID-filters */
857 af9015_config.dual_mode = 0;
858 } else {
859 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
860 = TS_USB20_FRAME_SIZE;
864 if (af9015_config.dual_mode) {
865 /* read 2nd demodulator I2C address */
866 req.addr = AF9015_EEPROM_DEMOD2_I2C;
867 ret = af9015_rw_udev(udev, &req);
868 if (ret)
869 goto error;
870 af9015_af9013_config[1].demod_address = val;
872 /* enable 2nd adapter */
873 for (i = 0; i < af9015_properties_count; i++)
874 af9015_properties[i].num_adapters = 2;
876 } else {
877 /* disable 2nd adapter */
878 for (i = 0; i < af9015_properties_count; i++)
879 af9015_properties[i].num_adapters = 1;
882 for (i = 0; i < af9015_properties[0].num_adapters; i++) {
883 if (i == 1)
884 offset = AF9015_EEPROM_OFFSET;
885 /* xtal */
886 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
887 ret = af9015_rw_udev(udev, &req);
888 if (ret)
889 goto error;
890 switch (val) {
891 case 0:
892 af9015_af9013_config[i].adc_clock = 28800;
893 break;
894 case 1:
895 af9015_af9013_config[i].adc_clock = 20480;
896 break;
897 case 2:
898 af9015_af9013_config[i].adc_clock = 28000;
899 break;
900 case 3:
901 af9015_af9013_config[i].adc_clock = 25000;
902 break;
904 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
905 val, af9015_af9013_config[i].adc_clock);
907 /* tuner IF */
908 req.addr = AF9015_EEPROM_IF1H + offset;
909 ret = af9015_rw_udev(udev, &req);
910 if (ret)
911 goto error;
912 af9015_af9013_config[i].tuner_if = val << 8;
913 req.addr = AF9015_EEPROM_IF1L + offset;
914 ret = af9015_rw_udev(udev, &req);
915 if (ret)
916 goto error;
917 af9015_af9013_config[i].tuner_if += val;
918 deb_info("%s: [%d] IF1:%d\n", __func__, i,
919 af9015_af9013_config[0].tuner_if);
921 /* MT2060 IF1 */
922 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
923 ret = af9015_rw_udev(udev, &req);
924 if (ret)
925 goto error;
926 af9015_config.mt2060_if1[i] = val << 8;
927 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
928 ret = af9015_rw_udev(udev, &req);
929 if (ret)
930 goto error;
931 af9015_config.mt2060_if1[i] += val;
932 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
933 af9015_config.mt2060_if1[i]);
935 /* tuner */
936 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
937 ret = af9015_rw_udev(udev, &req);
938 if (ret)
939 goto error;
940 switch (val) {
941 case AF9013_TUNER_ENV77H11D5:
942 case AF9013_TUNER_MT2060:
943 case AF9013_TUNER_QT1010:
944 case AF9013_TUNER_UNKNOWN:
945 case AF9013_TUNER_MT2060_2:
946 case AF9013_TUNER_TDA18271:
947 case AF9013_TUNER_QT1010A:
948 case AF9013_TUNER_TDA18218:
949 af9015_af9013_config[i].rf_spec_inv = 1;
950 break;
951 case AF9013_TUNER_MXL5003D:
952 case AF9013_TUNER_MXL5005D:
953 case AF9013_TUNER_MXL5005R:
954 case AF9013_TUNER_MXL5007T:
955 af9015_af9013_config[i].rf_spec_inv = 0;
956 break;
957 case AF9013_TUNER_MC44S803:
958 af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
959 af9015_af9013_config[i].rf_spec_inv = 1;
960 break;
961 default:
962 warn("tuner id:%d not supported, please report!", val);
963 return -ENODEV;
966 af9015_af9013_config[i].tuner = val;
967 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
970 error:
971 if (ret)
972 err("eeprom read failed:%d", ret);
974 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
975 content :-( Override some wrong values here. Ditto for the
976 AVerTV Red HD+ (A850T) device. */
977 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
978 ((le16_to_cpu(udev->descriptor.idProduct) ==
979 USB_PID_AVERMEDIA_A850) ||
980 (le16_to_cpu(udev->descriptor.idProduct) ==
981 USB_PID_AVERMEDIA_A850T))) {
982 deb_info("%s: AverMedia A850: overriding config\n", __func__);
983 /* disable dual mode */
984 af9015_config.dual_mode = 0;
985 /* disable 2nd adapter */
986 for (i = 0; i < af9015_properties_count; i++)
987 af9015_properties[i].num_adapters = 1;
989 /* set correct IF */
990 af9015_af9013_config[0].tuner_if = 4570;
993 return ret;
996 static int af9015_identify_state(struct usb_device *udev,
997 struct dvb_usb_device_properties *props,
998 struct dvb_usb_device_description **desc,
999 int *cold)
1001 int ret;
1002 u8 reply;
1003 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
1005 ret = af9015_rw_udev(udev, &req);
1006 if (ret)
1007 return ret;
1009 deb_info("%s: reply:%02x\n", __func__, reply);
1010 if (reply == 0x02)
1011 *cold = 0;
1012 else
1013 *cold = 1;
1015 return ret;
1018 static int af9015_rc_query(struct dvb_usb_device *d)
1020 struct af9015_state *priv = d->priv;
1021 int ret;
1022 u8 buf[17];
1024 /* read registers needed to detect remote controller code */
1025 ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
1026 if (ret)
1027 goto error;
1029 /* If any of these are non-zero, assume invalid data */
1030 if (buf[1] || buf[2] || buf[3])
1031 return ret;
1033 /* Check for repeat of previous code */
1034 if ((priv->rc_repeat != buf[6] || buf[0]) &&
1035 !memcmp(&buf[12], priv->rc_last, 4)) {
1036 deb_rc("%s: key repeated\n", __func__);
1037 rc_keydown(d->rc_dev, priv->rc_keycode, 0);
1038 priv->rc_repeat = buf[6];
1039 return ret;
1042 /* Only process key if canary killed */
1043 if (buf[16] != 0xff && buf[0] != 0x01) {
1044 deb_rc("%s: key pressed %02x %02x %02x %02x\n", __func__,
1045 buf[12], buf[13], buf[14], buf[15]);
1047 /* Reset the canary */
1048 ret = af9015_write_reg(d, 0x98e9, 0xff);
1049 if (ret)
1050 goto error;
1052 /* Remember this key */
1053 memcpy(priv->rc_last, &buf[12], 4);
1054 if (buf[14] == (u8) ~buf[15]) {
1055 if (buf[12] == (u8) ~buf[13]) {
1056 /* NEC */
1057 priv->rc_keycode = buf[12] << 8 | buf[14];
1058 } else {
1059 /* NEC extended*/
1060 priv->rc_keycode = buf[12] << 16 |
1061 buf[13] << 8 | buf[14];
1063 } else {
1064 /* 32 bit NEC */
1065 priv->rc_keycode = buf[12] << 24 | buf[13] << 16 |
1066 buf[14] << 8 | buf[15];
1068 rc_keydown(d->rc_dev, priv->rc_keycode, 0);
1069 } else {
1070 deb_rc("%s: no key press\n", __func__);
1071 /* Invalidate last keypress */
1072 /* Not really needed, but helps with debug */
1073 priv->rc_last[2] = priv->rc_last[3];
1076 priv->rc_repeat = buf[6];
1078 error:
1079 if (ret)
1080 err("%s: failed:%d", __func__, ret);
1082 return ret;
1085 /* init 2nd I2C adapter */
1086 static int af9015_i2c_init(struct dvb_usb_device *d)
1088 int ret;
1089 struct af9015_state *state = d->priv;
1090 deb_info("%s:\n", __func__);
1092 strncpy(state->i2c_adap.name, d->desc->name,
1093 sizeof(state->i2c_adap.name));
1094 state->i2c_adap.algo = d->props.i2c_algo;
1095 state->i2c_adap.algo_data = NULL;
1096 state->i2c_adap.dev.parent = &d->udev->dev;
1098 i2c_set_adapdata(&state->i2c_adap, d);
1100 ret = i2c_add_adapter(&state->i2c_adap);
1101 if (ret < 0)
1102 err("could not add i2c adapter");
1104 return ret;
1107 static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1109 int ret;
1110 struct af9015_state *state = adap->dev->priv;
1111 struct i2c_adapter *i2c_adap;
1113 if (adap->id == 0) {
1114 /* select I2C adapter */
1115 i2c_adap = &adap->dev->i2c_adap;
1117 deb_info("%s: init I2C\n", __func__);
1118 ret = af9015_i2c_init(adap->dev);
1119 } else {
1120 /* select I2C adapter */
1121 i2c_adap = &state->i2c_adap;
1123 /* copy firmware to 2nd demodulator */
1124 if (af9015_config.dual_mode) {
1125 ret = af9015_copy_firmware(adap->dev);
1126 if (ret) {
1127 err("firmware copy to 2nd frontend " \
1128 "failed, will disable it");
1129 af9015_config.dual_mode = 0;
1130 return -ENODEV;
1132 } else {
1133 return -ENODEV;
1137 /* attach demodulator */
1138 adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1139 i2c_adap);
1141 return adap->fe == NULL ? -ENODEV : 0;
1144 static struct mt2060_config af9015_mt2060_config = {
1145 .i2c_address = 0xc0,
1146 .clock_out = 0,
1149 static struct qt1010_config af9015_qt1010_config = {
1150 .i2c_address = 0xc4,
1153 static struct tda18271_config af9015_tda18271_config = {
1154 .gate = TDA18271_GATE_DIGITAL,
1155 .small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
1158 static struct mxl5005s_config af9015_mxl5003_config = {
1159 .i2c_address = 0xc6,
1160 .if_freq = IF_FREQ_4570000HZ,
1161 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1162 .agc_mode = MXL_SINGLE_AGC,
1163 .tracking_filter = MXL_TF_DEFAULT,
1164 .rssi_enable = MXL_RSSI_ENABLE,
1165 .cap_select = MXL_CAP_SEL_ENABLE,
1166 .div_out = MXL_DIV_OUT_4,
1167 .clock_out = MXL_CLOCK_OUT_DISABLE,
1168 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1169 .top = MXL5005S_TOP_25P2,
1170 .mod_mode = MXL_DIGITAL_MODE,
1171 .if_mode = MXL_ZERO_IF,
1172 .AgcMasterByte = 0x00,
1175 static struct mxl5005s_config af9015_mxl5005_config = {
1176 .i2c_address = 0xc6,
1177 .if_freq = IF_FREQ_4570000HZ,
1178 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1179 .agc_mode = MXL_SINGLE_AGC,
1180 .tracking_filter = MXL_TF_OFF,
1181 .rssi_enable = MXL_RSSI_ENABLE,
1182 .cap_select = MXL_CAP_SEL_ENABLE,
1183 .div_out = MXL_DIV_OUT_4,
1184 .clock_out = MXL_CLOCK_OUT_DISABLE,
1185 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1186 .top = MXL5005S_TOP_25P2,
1187 .mod_mode = MXL_DIGITAL_MODE,
1188 .if_mode = MXL_ZERO_IF,
1189 .AgcMasterByte = 0x00,
1192 static struct mc44s803_config af9015_mc44s803_config = {
1193 .i2c_address = 0xc0,
1194 .dig_out = 1,
1197 static struct tda18218_config af9015_tda18218_config = {
1198 .i2c_address = 0xc0,
1199 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
1202 static struct mxl5007t_config af9015_mxl5007t_config = {
1203 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1204 .if_freq_hz = MxL_IF_4_57_MHZ,
1207 static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1209 struct af9015_state *state = adap->dev->priv;
1210 struct i2c_adapter *i2c_adap;
1211 int ret;
1212 deb_info("%s:\n", __func__);
1214 /* select I2C adapter */
1215 if (adap->id == 0)
1216 i2c_adap = &adap->dev->i2c_adap;
1217 else
1218 i2c_adap = &state->i2c_adap;
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_TDA18218:
1238 ret = dvb_attach(tda18218_attach, adap->fe, i2c_adap,
1239 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
1240 break;
1241 case AF9013_TUNER_MXL5003D:
1242 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1243 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1244 break;
1245 case AF9013_TUNER_MXL5005D:
1246 case AF9013_TUNER_MXL5005R:
1247 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1248 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1249 break;
1250 case AF9013_TUNER_ENV77H11D5:
1251 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1252 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1253 break;
1254 case AF9013_TUNER_MC44S803:
1255 ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
1256 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
1257 break;
1258 case AF9013_TUNER_MXL5007T:
1259 ret = dvb_attach(mxl5007t_attach, adap->fe, i2c_adap,
1260 0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
1261 break;
1262 case AF9013_TUNER_UNKNOWN:
1263 default:
1264 ret = -ENODEV;
1265 err("Unknown tuner id:%d",
1266 af9015_af9013_config[adap->id].tuner);
1268 return ret;
1271 static struct usb_device_id af9015_usb_table[] = {
1272 /* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
1273 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
1274 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1275 {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
1276 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
1277 /* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1278 USB_PID_TINYTWIN)},
1279 {USB_DEVICE(USB_VID_VISIONPLUS,
1280 USB_PID_AZUREWAVE_AD_TU700)},
1281 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1282 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
1283 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1284 /* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1285 {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
1286 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
1287 {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
1288 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
1289 /* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
1290 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
1291 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
1292 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
1293 {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
1294 /* 20 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
1295 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805)},
1296 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU)},
1297 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810)},
1298 {USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03)},
1299 /* 25 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2)},
1300 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T)},
1301 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20)},
1302 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2)},
1303 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS)},
1304 /* 30 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T)},
1305 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4)},
1306 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M)},
1307 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC)},
1308 {USB_DEVICE(USB_VID_TERRATEC,
1309 USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC)},
1310 /* 35 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T)},
1311 {USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3)},
1312 {0},
1314 MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1316 #define AF9015_RC_INTERVAL 500
1317 static struct dvb_usb_device_properties af9015_properties[] = {
1319 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1321 .usb_ctrl = DEVICE_SPECIFIC,
1322 .download_firmware = af9015_download_firmware,
1323 .firmware = "dvb-usb-af9015.fw",
1324 .no_reconnect = 1,
1326 .size_of_priv = sizeof(struct af9015_state),
1328 .num_adapters = 2,
1329 .adapter = {
1331 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1332 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1334 .pid_filter_count = 32,
1335 .pid_filter = af9015_pid_filter,
1336 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1338 .frontend_attach =
1339 af9015_af9013_frontend_attach,
1340 .tuner_attach = af9015_tuner_attach,
1341 .stream = {
1342 .type = USB_BULK,
1343 .count = 6,
1344 .endpoint = 0x84,
1348 .frontend_attach =
1349 af9015_af9013_frontend_attach,
1350 .tuner_attach = af9015_tuner_attach,
1351 .stream = {
1352 .type = USB_BULK,
1353 .count = 6,
1354 .endpoint = 0x85,
1355 .u = {
1356 .bulk = {
1357 .buffersize =
1358 TS_USB20_FRAME_SIZE,
1365 .identify_state = af9015_identify_state,
1367 .rc.core = {
1368 .protocol = RC_TYPE_NEC,
1369 .module_name = "af9015",
1370 .rc_query = af9015_rc_query,
1371 .rc_interval = AF9015_RC_INTERVAL,
1372 .allowed_protos = RC_TYPE_NEC,
1375 .i2c_algo = &af9015_i2c_algo,
1377 .num_device_descs = 12, /* check max from dvb-usb.h */
1378 .devices = {
1380 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1381 .cold_ids = {&af9015_usb_table[0],
1382 &af9015_usb_table[1], NULL},
1383 .warm_ids = {NULL},
1386 .name = "Leadtek WinFast DTV Dongle Gold",
1387 .cold_ids = {&af9015_usb_table[2], NULL},
1388 .warm_ids = {NULL},
1391 .name = "Pinnacle PCTV 71e",
1392 .cold_ids = {&af9015_usb_table[3], NULL},
1393 .warm_ids = {NULL},
1396 .name = "KWorld PlusTV Dual DVB-T Stick " \
1397 "(DVB-T 399U)",
1398 .cold_ids = {&af9015_usb_table[4],
1399 &af9015_usb_table[25], NULL},
1400 .warm_ids = {NULL},
1403 .name = "DigitalNow TinyTwin DVB-T Receiver",
1404 .cold_ids = {&af9015_usb_table[5],
1405 &af9015_usb_table[28],
1406 &af9015_usb_table[36], NULL},
1407 .warm_ids = {NULL},
1410 .name = "TwinHan AzureWave AD-TU700(704J)",
1411 .cold_ids = {&af9015_usb_table[6], NULL},
1412 .warm_ids = {NULL},
1415 .name = "TerraTec Cinergy T USB XE",
1416 .cold_ids = {&af9015_usb_table[7], NULL},
1417 .warm_ids = {NULL},
1420 .name = "KWorld PlusTV Dual DVB-T PCI " \
1421 "(DVB-T PC160-2T)",
1422 .cold_ids = {&af9015_usb_table[8], NULL},
1423 .warm_ids = {NULL},
1426 .name = "AVerMedia AVerTV DVB-T Volar X",
1427 .cold_ids = {&af9015_usb_table[9], NULL},
1428 .warm_ids = {NULL},
1431 .name = "TerraTec Cinergy T Stick RC",
1432 .cold_ids = {&af9015_usb_table[33], NULL},
1433 .warm_ids = {NULL},
1436 .name = "TerraTec Cinergy T Stick Dual RC",
1437 .cold_ids = {&af9015_usb_table[34], NULL},
1438 .warm_ids = {NULL},
1441 .name = "AverMedia AVerTV Red HD+ (A850T)",
1442 .cold_ids = {&af9015_usb_table[35], NULL},
1443 .warm_ids = {NULL},
1446 }, {
1447 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1449 .usb_ctrl = DEVICE_SPECIFIC,
1450 .download_firmware = af9015_download_firmware,
1451 .firmware = "dvb-usb-af9015.fw",
1452 .no_reconnect = 1,
1454 .size_of_priv = sizeof(struct af9015_state),
1456 .num_adapters = 2,
1457 .adapter = {
1459 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1460 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1462 .pid_filter_count = 32,
1463 .pid_filter = af9015_pid_filter,
1464 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1466 .frontend_attach =
1467 af9015_af9013_frontend_attach,
1468 .tuner_attach = af9015_tuner_attach,
1469 .stream = {
1470 .type = USB_BULK,
1471 .count = 6,
1472 .endpoint = 0x84,
1476 .frontend_attach =
1477 af9015_af9013_frontend_attach,
1478 .tuner_attach = af9015_tuner_attach,
1479 .stream = {
1480 .type = USB_BULK,
1481 .count = 6,
1482 .endpoint = 0x85,
1483 .u = {
1484 .bulk = {
1485 .buffersize =
1486 TS_USB20_FRAME_SIZE,
1493 .identify_state = af9015_identify_state,
1495 .rc.core = {
1496 .protocol = RC_TYPE_NEC,
1497 .module_name = "af9015",
1498 .rc_query = af9015_rc_query,
1499 .rc_interval = AF9015_RC_INTERVAL,
1500 .allowed_protos = RC_TYPE_NEC,
1503 .i2c_algo = &af9015_i2c_algo,
1505 .num_device_descs = 9, /* check max from dvb-usb.h */
1506 .devices = {
1508 .name = "Xtensions XD-380",
1509 .cold_ids = {&af9015_usb_table[10], NULL},
1510 .warm_ids = {NULL},
1513 .name = "MSI DIGIVOX Duo",
1514 .cold_ids = {&af9015_usb_table[11], NULL},
1515 .warm_ids = {NULL},
1518 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1519 .cold_ids = {&af9015_usb_table[12], NULL},
1520 .warm_ids = {NULL},
1523 .name = "Telestar Starstick 2",
1524 .cold_ids = {&af9015_usb_table[13], NULL},
1525 .warm_ids = {NULL},
1528 .name = "AVerMedia A309",
1529 .cold_ids = {&af9015_usb_table[14], NULL},
1530 .warm_ids = {NULL},
1533 .name = "MSI Digi VOX mini III",
1534 .cold_ids = {&af9015_usb_table[15], NULL},
1535 .warm_ids = {NULL},
1538 .name = "KWorld USB DVB-T TV Stick II " \
1539 "(VS-DVB-T 395U)",
1540 .cold_ids = {&af9015_usb_table[16],
1541 &af9015_usb_table[17],
1542 &af9015_usb_table[18],
1543 &af9015_usb_table[31], NULL},
1544 .warm_ids = {NULL},
1547 .name = "TrekStor DVB-T USB Stick",
1548 .cold_ids = {&af9015_usb_table[19], NULL},
1549 .warm_ids = {NULL},
1552 .name = "AverMedia AVerTV Volar Black HD " \
1553 "(A850)",
1554 .cold_ids = {&af9015_usb_table[20], NULL},
1555 .warm_ids = {NULL},
1558 }, {
1559 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1561 .usb_ctrl = DEVICE_SPECIFIC,
1562 .download_firmware = af9015_download_firmware,
1563 .firmware = "dvb-usb-af9015.fw",
1564 .no_reconnect = 1,
1566 .size_of_priv = sizeof(struct af9015_state),
1568 .num_adapters = 2,
1569 .adapter = {
1571 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1572 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1574 .pid_filter_count = 32,
1575 .pid_filter = af9015_pid_filter,
1576 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1578 .frontend_attach =
1579 af9015_af9013_frontend_attach,
1580 .tuner_attach = af9015_tuner_attach,
1581 .stream = {
1582 .type = USB_BULK,
1583 .count = 6,
1584 .endpoint = 0x84,
1588 .frontend_attach =
1589 af9015_af9013_frontend_attach,
1590 .tuner_attach = af9015_tuner_attach,
1591 .stream = {
1592 .type = USB_BULK,
1593 .count = 6,
1594 .endpoint = 0x85,
1595 .u = {
1596 .bulk = {
1597 .buffersize =
1598 TS_USB20_FRAME_SIZE,
1605 .identify_state = af9015_identify_state,
1607 .rc.core = {
1608 .protocol = RC_TYPE_NEC,
1609 .module_name = "af9015",
1610 .rc_query = af9015_rc_query,
1611 .rc_interval = AF9015_RC_INTERVAL,
1612 .allowed_protos = RC_TYPE_NEC,
1615 .i2c_algo = &af9015_i2c_algo,
1617 .num_device_descs = 9, /* check max from dvb-usb.h */
1618 .devices = {
1620 .name = "AverMedia AVerTV Volar GPS 805 (A805)",
1621 .cold_ids = {&af9015_usb_table[21], NULL},
1622 .warm_ids = {NULL},
1625 .name = "Conceptronic USB2.0 DVB-T CTVDIGRCU " \
1626 "V3.0",
1627 .cold_ids = {&af9015_usb_table[22], NULL},
1628 .warm_ids = {NULL},
1631 .name = "KWorld Digial MC-810",
1632 .cold_ids = {&af9015_usb_table[23], NULL},
1633 .warm_ids = {NULL},
1636 .name = "Genius TVGo DVB-T03",
1637 .cold_ids = {&af9015_usb_table[24], NULL},
1638 .warm_ids = {NULL},
1641 .name = "KWorld PlusTV DVB-T PCI Pro Card " \
1642 "(DVB-T PC160-T)",
1643 .cold_ids = {&af9015_usb_table[26], NULL},
1644 .warm_ids = {NULL},
1647 .name = "Sveon STV20 Tuner USB DVB-T HDTV",
1648 .cold_ids = {&af9015_usb_table[27], NULL},
1649 .warm_ids = {NULL},
1652 .name = "Leadtek WinFast DTV2000DS",
1653 .cold_ids = {&af9015_usb_table[29], NULL},
1654 .warm_ids = {NULL},
1657 .name = "KWorld USB DVB-T Stick Mobile " \
1658 "(UB383-T)",
1659 .cold_ids = {&af9015_usb_table[30], NULL},
1660 .warm_ids = {NULL},
1663 .name = "AverMedia AVerTV Volar M (A815Mac)",
1664 .cold_ids = {&af9015_usb_table[32], NULL},
1665 .warm_ids = {NULL},
1671 static int af9015_usb_probe(struct usb_interface *intf,
1672 const struct usb_device_id *id)
1674 int ret = 0;
1675 struct dvb_usb_device *d = NULL;
1676 struct usb_device *udev = interface_to_usbdev(intf);
1677 u8 i;
1679 deb_info("%s: interface:%d\n", __func__,
1680 intf->cur_altsetting->desc.bInterfaceNumber);
1682 /* interface 0 is used by DVB-T receiver and
1683 interface 1 is for remote controller (HID) */
1684 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1685 ret = af9015_read_config(udev);
1686 if (ret)
1687 return ret;
1689 for (i = 0; i < af9015_properties_count; i++) {
1690 ret = dvb_usb_device_init(intf, &af9015_properties[i],
1691 THIS_MODULE, &d, adapter_nr);
1692 if (!ret)
1693 break;
1694 if (ret != -ENODEV)
1695 return ret;
1697 if (ret)
1698 return ret;
1700 if (d)
1701 ret = af9015_init(d);
1704 return ret;
1707 static void af9015_i2c_exit(struct dvb_usb_device *d)
1709 struct af9015_state *state = d->priv;
1710 deb_info("%s:\n", __func__);
1712 /* remove 2nd I2C adapter */
1713 if (d->state & DVB_USB_STATE_I2C)
1714 i2c_del_adapter(&state->i2c_adap);
1717 static void af9015_usb_device_exit(struct usb_interface *intf)
1719 struct dvb_usb_device *d = usb_get_intfdata(intf);
1720 deb_info("%s:\n", __func__);
1722 /* remove 2nd I2C adapter */
1723 if (d != NULL && d->desc != NULL)
1724 af9015_i2c_exit(d);
1726 dvb_usb_device_exit(intf);
1729 /* usb specific object needed to register this driver with the usb subsystem */
1730 static struct usb_driver af9015_usb_driver = {
1731 .name = "dvb_usb_af9015",
1732 .probe = af9015_usb_probe,
1733 .disconnect = af9015_usb_device_exit,
1734 .id_table = af9015_usb_table,
1737 /* module stuff */
1738 static int __init af9015_usb_module_init(void)
1740 int ret;
1741 ret = usb_register(&af9015_usb_driver);
1742 if (ret)
1743 err("module init failed:%d", ret);
1745 return ret;
1748 static void __exit af9015_usb_module_exit(void)
1750 /* deregister this driver from the USB subsystem */
1751 usb_deregister(&af9015_usb_driver);
1754 module_init(af9015_usb_module_init);
1755 module_exit(af9015_usb_module_exit);
1757 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1758 MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1759 MODULE_LICENSE("GPL");