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.
34 static int dvb_usb_af9015_debug;
35 module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
36 MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
37 static int dvb_usb_af9015_remote;
38 module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
39 MODULE_PARM_DESC(remote, "select remote");
40 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42 static DEFINE_MUTEX(af9015_usb_mutex);
44 static struct af9015_config af9015_config;
45 static struct dvb_usb_device_properties af9015_properties[2];
46 static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
48 static struct af9013_config af9015_af9013_config[] = {
50 .demod_address = AF9015_I2C_DEMOD,
51 .output_mode = AF9013_OUTPUT_MODE_USB,
52 .api_version = { 0, 1, 9, 0 },
53 .gpio[0] = AF9013_GPIO_HI,
54 .gpio[3] = AF9013_GPIO_TUNER_ON,
57 .output_mode = AF9013_OUTPUT_MODE_SERIAL,
58 .api_version = { 0, 1, 9, 0 },
59 .gpio[0] = AF9013_GPIO_TUNER_ON,
60 .gpio[1] = AF9013_GPIO_LO,
64 static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
70 static u8 seq; /* packet sequence number */
72 if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
77 buf[2] = req->i2c_addr;
78 buf[3] = req->addr >> 8;
79 buf[4] = req->addr & 0xff;
81 buf[6] = req->addr_len;
82 buf[7] = req->data_len;
94 buf[2] |= 0x01; /* set I2C direction */
96 buf[0] = READ_WRITE_I2C;
99 if (((req->addr & 0xff00) == 0xff00) ||
100 ((req->addr & 0xae00) == 0xae00))
101 buf[0] = WRITE_VIRTUAL_MEMORY;
102 case WRITE_VIRTUAL_MEMORY:
104 case DOWNLOAD_FIRMWARE:
107 err("unknown command:%d", req->cmd);
112 /* write requested */
114 memcpy(&buf[8], req->data, req->data_len);
115 msg_len += req->data_len;
118 debug_dump(buf, msg_len, deb_xfer);
121 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
122 &act_len, AF9015_USB_TIMEOUT);
124 err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
126 if (act_len != msg_len)
127 ret = -1; /* all data is not send */
131 /* no ack for those packets */
132 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
135 /* receive ack and data if read req */
136 msg_len = 1 + 1 + req->data_len; /* seq + status + data len */
137 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
138 &act_len, AF9015_USB_TIMEOUT);
140 err("recv bulk message failed:%d", ret);
146 debug_dump(buf, act_len, deb_xfer);
148 /* remote controller query status is 1 if remote code is not received */
149 if (req->cmd == GET_IR_CODE && buf[1] == 1) {
150 buf[1] = 0; /* clear command "error" status */
151 memset(&buf[2], 0, req->data_len);
152 buf[3] = 1; /* no remote code received mark */
157 err("command failed:%d", buf[1]);
162 /* read request, copy returned data to return buf */
164 memcpy(req->data, &buf[2], req->data_len);
168 mutex_unlock(&af9015_usb_mutex);
173 static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
175 return af9015_rw_udev(d->udev, req);
178 static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
181 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
183 return af9015_ctrl_msg(d, &req);
186 static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
188 return af9015_write_regs(d, addr, &val, 1);
191 static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
193 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, 1, val};
194 return af9015_ctrl_msg(d, &req);
197 static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
200 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
202 if (addr == af9015_af9013_config[0].demod_address ||
203 addr == af9015_af9013_config[1].demod_address)
206 return af9015_ctrl_msg(d, &req);
209 static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
212 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
214 if (addr == af9015_af9013_config[0].demod_address ||
215 addr == af9015_af9013_config[1].demod_address)
218 return af9015_ctrl_msg(d, &req);
221 static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
224 struct dvb_usb_device *d = i2c_get_adapdata(adap);
230 /* TODO: implement bus lock
232 The bus lock is needed because there is two tuners both using same I2C-address.
233 Due to that the only way to select correct tuner is use demodulator I2C-gate.
235 ................................................
236 . AF9015 includes integrated AF9013 demodulator.
237 . ____________ ____________ . ____________
238 .| uC | | demod | . | tuner |
239 .|------------| |------------| . |------------|
240 .| AF9015 | | AF9013/5 | . | MXL5003 |
241 .| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
242 .| | | | addr 0x38 | . | addr 0xc6 |
243 .|____________| | |____________| . |____________|
244 .................|..............................
245 | ____________ ____________
246 | | demod | | tuner |
247 | |------------| |------------|
248 | | AF9013 | | MXL5003 |
249 +----I2C-------|-----/ -----|-------I2C-------| |
250 | addr 0x3a | | addr 0xc6 |
251 |____________| |____________|
253 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
257 if (msg[i].addr == af9015_af9013_config[0].demod_address ||
258 msg[i].addr == af9015_af9013_config[1].demod_address) {
259 addr = msg[i].buf[0] << 8;
260 addr += msg[i].buf[1];
261 mbox = msg[i].buf[2];
264 addr = msg[i].buf[0];
269 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
271 af9015_af9013_config[0].demod_address)
272 req.cmd = READ_MEMORY;
275 req.i2c_addr = msg[i].addr;
278 req.addr_len = addr_len;
279 req.data_len = msg[i+1].len;
280 req.data = &msg[i+1].buf[0];
281 ret = af9015_ctrl_msg(d, &req);
285 af9015_af9013_config[0].demod_address)
286 req.cmd = WRITE_MEMORY;
289 req.i2c_addr = msg[i].addr;
292 req.addr_len = addr_len;
293 req.data_len = msg[i].len-addr_len;
294 req.data = &msg[i].buf[addr_len];
295 ret = af9015_ctrl_msg(d, &req);
305 mutex_unlock(&d->i2c_mutex);
310 static u32 af9015_i2c_func(struct i2c_adapter *adapter)
315 static struct i2c_algorithm af9015_i2c_algo = {
316 .master_xfer = af9015_i2c_xfer,
317 .functionality = af9015_i2c_func,
320 static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
325 ret = af9015_read_reg(d, addr, &val);
339 return af9015_write_reg(d, addr, val);
342 static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
344 return af9015_do_reg_bit(d, addr, bit, 1);
347 static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
349 return af9015_do_reg_bit(d, addr, bit, 0);
352 static int af9015_init_endpoint(struct dvb_usb_device *d)
357 deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
359 #define TS_PACKET_SIZE 188
361 #define TS_USB20_PACKET_COUNT 348
362 #define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
364 #define TS_USB11_PACKET_COUNT 21
365 #define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
367 #define TS_USB20_MAX_PACKET_SIZE 512
368 #define TS_USB11_MAX_PACKET_SIZE 64
370 if (d->udev->speed == USB_SPEED_FULL) {
371 frame_size = TS_USB11_FRAME_SIZE/4;
372 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
374 frame_size = TS_USB20_FRAME_SIZE/4;
375 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
378 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
381 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
384 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
387 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
390 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
393 if (af9015_config.dual_mode) {
394 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
398 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
401 if (af9015_config.dual_mode) {
402 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
406 /* EP4 xfer length */
407 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
410 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
413 /* EP5 xfer length */
414 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
417 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
420 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
423 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
426 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
429 if (af9015_config.dual_mode) {
430 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
435 /* enable / disable mp2if2 */
436 if (af9015_config.dual_mode)
437 ret = af9015_set_reg_bit(d, 0xd50b, 0);
439 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
442 err("endpoint init failed:%d", ret);
446 static int af9015_copy_firmware(struct dvb_usb_device *d)
451 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
453 deb_info("%s:\n", __func__);
455 fw_params[0] = af9015_config.firmware_size >> 8;
456 fw_params[1] = af9015_config.firmware_size & 0xff;
457 fw_params[2] = af9015_config.firmware_checksum >> 8;
458 fw_params[3] = af9015_config.firmware_checksum & 0xff;
460 /* wait 2nd demodulator ready */
463 ret = af9015_read_reg_i2c(d, 0x3a, 0x98be, &val);
467 deb_info("%s: firmware status:%02x\n", __func__, val);
469 if (val == 0x0c) /* fw is running, no need for download */
472 /* set I2C master clock to fast (to speed up firmware copy) */
473 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
480 ret = af9015_ctrl_msg(d, &req);
482 err("firmware copy cmd failed:%d", ret);
483 deb_info("%s: firmware copy done\n", __func__);
485 /* set I2C master clock back to normal */
486 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
490 /* request boot firmware */
491 ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
493 deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
497 for (i = 0; i < 15; i++) {
500 /* check firmware status */
501 ret = af9015_read_reg_i2c(d,
502 af9015_af9013_config[1].demod_address, 0x98be, &val);
503 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
508 if (val == 0x0c || val == 0x04) /* success or fail */
513 err("firmware did not run");
515 } else if (val != 0x0c) {
516 err("firmware boot timeout");
526 static int af9015_eeprom_dump(struct dvb_usb_device *d)
528 char buf[52], buf2[4];
531 for (reg = 0; ; reg++) {
534 deb_info("%s\n", buf);
535 sprintf(buf, "%02x: ", reg);
537 if (af9015_read_reg_i2c(d, AF9015_I2C_EEPROM, reg, &val) == 0)
538 sprintf(buf2, "%02x ", val);
545 deb_info("%s\n", buf);
549 static int af9015_download_ir_table(struct dvb_usb_device *d)
551 int i, packets = 0, ret;
552 u16 addr = 0x9a56; /* ir-table start address */
553 struct req_t req = {WRITE_MEMORY, 0, 0, 0, 0, 1, NULL};
555 deb_info("%s:\n", __func__);
557 data = af9015_config.ir_table;
558 packets = af9015_config.ir_table_size;
564 /* load remote ir-table */
565 for (i = 0; i < packets; i++) {
568 ret = af9015_ctrl_msg(d, &req);
570 err("ir-table download failed at packet %d with " \
580 static int af9015_init(struct dvb_usb_device *d)
583 deb_info("%s:\n", __func__);
585 ret = af9015_init_endpoint(d);
589 ret = af9015_download_ir_table(d);
597 static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
600 deb_info("%s: onoff:%d\n", __func__, onoff);
603 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
605 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
610 static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
616 deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
617 __func__, index, pid, onoff);
619 ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
623 ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
627 idx = ((index & 0x1f) | (1 << 5));
628 ret = af9015_write_reg(adap->dev, 0xd504, idx);
634 static int af9015_download_firmware(struct usb_device *udev,
635 const struct firmware *fw)
637 int i, len, packets, remainder, ret;
638 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
639 u16 addr = 0x5100; /* firmware start address */
642 deb_info("%s:\n", __func__);
645 for (i = 0; i < fw->size; i++)
646 checksum += fw->data[i];
648 af9015_config.firmware_size = fw->size;
649 af9015_config.firmware_checksum = checksum;
651 #define FW_PACKET_MAX_DATA 55
653 packets = fw->size / FW_PACKET_MAX_DATA;
654 remainder = fw->size % FW_PACKET_MAX_DATA;
655 len = FW_PACKET_MAX_DATA;
656 for (i = 0; i <= packets; i++) {
657 if (i == packets) /* set size of the last packet */
661 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
663 addr += FW_PACKET_MAX_DATA;
665 ret = af9015_rw_udev(udev, &req);
667 err("firmware download failed at packet %d with " \
673 /* firmware loaded, request boot */
675 ret = af9015_rw_udev(udev, &req);
677 err("firmware boot failed:%d", ret);
685 static int af9015_read_config(struct usb_device *udev)
688 u8 val, i, offset = 0;
689 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
690 char manufacturer[10];
692 /* IR remote controller */
693 req.addr = AF9015_EEPROM_IR_MODE;
694 /* first message will timeout often due to possible hw bug */
695 for (i = 0; i < 4; i++) {
696 ret = af9015_rw_udev(udev, &req);
702 deb_info("%s: IR mode:%d\n", __func__, val);
703 for (i = 0; i < af9015_properties_count; i++) {
704 if (val == AF9015_IR_MODE_DISABLED || val == 0x04) {
705 af9015_properties[i].rc_key_map = NULL;
706 af9015_properties[i].rc_key_map_size = 0;
707 } else if (dvb_usb_af9015_remote) {
708 /* load remote defined as module param */
709 switch (dvb_usb_af9015_remote) {
710 case AF9015_REMOTE_A_LINK_DTU_M:
711 af9015_properties[i].rc_key_map =
712 af9015_rc_keys_a_link;
713 af9015_properties[i].rc_key_map_size =
714 ARRAY_SIZE(af9015_rc_keys_a_link);
715 af9015_config.ir_table = af9015_ir_table_a_link;
716 af9015_config.ir_table_size =
717 ARRAY_SIZE(af9015_ir_table_a_link);
719 case AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3:
720 af9015_properties[i].rc_key_map =
722 af9015_properties[i].rc_key_map_size =
723 ARRAY_SIZE(af9015_rc_keys_msi);
724 af9015_config.ir_table = af9015_ir_table_msi;
725 af9015_config.ir_table_size =
726 ARRAY_SIZE(af9015_ir_table_msi);
728 case AF9015_REMOTE_MYGICTV_U718:
729 af9015_properties[i].rc_key_map =
730 af9015_rc_keys_mygictv;
731 af9015_properties[i].rc_key_map_size =
732 ARRAY_SIZE(af9015_rc_keys_mygictv);
733 af9015_config.ir_table =
734 af9015_ir_table_mygictv;
735 af9015_config.ir_table_size =
736 ARRAY_SIZE(af9015_ir_table_mygictv);
738 case AF9015_REMOTE_DIGITTRADE_DVB_T:
739 af9015_properties[i].rc_key_map =
740 af9015_rc_keys_digittrade;
741 af9015_properties[i].rc_key_map_size =
742 ARRAY_SIZE(af9015_rc_keys_digittrade);
743 af9015_config.ir_table =
744 af9015_ir_table_digittrade;
745 af9015_config.ir_table_size =
746 ARRAY_SIZE(af9015_ir_table_digittrade);
750 switch (le16_to_cpu(udev->descriptor.idVendor)) {
751 case USB_VID_LEADTEK:
752 af9015_properties[i].rc_key_map =
753 af9015_rc_keys_leadtek;
754 af9015_properties[i].rc_key_map_size =
755 ARRAY_SIZE(af9015_rc_keys_leadtek);
756 af9015_config.ir_table =
757 af9015_ir_table_leadtek;
758 af9015_config.ir_table_size =
759 ARRAY_SIZE(af9015_ir_table_leadtek);
761 case USB_VID_VISIONPLUS:
762 if (udev->descriptor.idProduct ==
763 cpu_to_le16(USB_PID_AZUREWAVE_AD_TU700)) {
764 af9015_properties[i].rc_key_map =
765 af9015_rc_keys_twinhan;
766 af9015_properties[i].rc_key_map_size =
767 ARRAY_SIZE(af9015_rc_keys_twinhan);
768 af9015_config.ir_table =
769 af9015_ir_table_twinhan;
770 af9015_config.ir_table_size =
771 ARRAY_SIZE(af9015_ir_table_twinhan);
774 case USB_VID_KWORLD_2:
775 /* TODO: use correct rc keys */
776 af9015_properties[i].rc_key_map =
777 af9015_rc_keys_twinhan;
778 af9015_properties[i].rc_key_map_size =
779 ARRAY_SIZE(af9015_rc_keys_twinhan);
780 af9015_config.ir_table = af9015_ir_table_kworld;
781 af9015_config.ir_table_size =
782 ARRAY_SIZE(af9015_ir_table_kworld);
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 case USB_VID_AFATECH:
788 memset(manufacturer, 0, sizeof(manufacturer));
789 usb_string(udev, udev->descriptor.iManufacturer,
790 manufacturer, sizeof(manufacturer));
791 if (!strcmp("Geniatech", manufacturer)) {
792 /* iManufacturer 1 Geniatech
794 af9015_properties[i].rc_key_map =
795 af9015_rc_keys_mygictv;
796 af9015_properties[i].rc_key_map_size =
797 ARRAY_SIZE(af9015_rc_keys_mygictv);
798 af9015_config.ir_table =
799 af9015_ir_table_mygictv;
800 af9015_config.ir_table_size =
801 ARRAY_SIZE(af9015_ir_table_mygictv);
802 } else if (!strcmp("MSI", manufacturer)) {
803 /* iManufacturer 1 MSI
804 iProduct 2 MSI K-VOX */
805 af9015_properties[i].rc_key_map =
807 af9015_properties[i].rc_key_map_size =
808 ARRAY_SIZE(af9015_rc_keys_msi);
809 af9015_config.ir_table =
811 af9015_config.ir_table_size =
812 ARRAY_SIZE(af9015_ir_table_msi);
815 case USB_VID_AVERMEDIA:
816 af9015_properties[i].rc_key_map =
817 af9015_rc_keys_avermedia;
818 af9015_properties[i].rc_key_map_size =
819 ARRAY_SIZE(af9015_rc_keys_avermedia);
820 af9015_config.ir_table =
821 af9015_ir_table_avermedia;
822 af9015_config.ir_table_size =
823 ARRAY_SIZE(af9015_ir_table_avermedia);
829 /* TS mode - one or two receivers */
830 req.addr = AF9015_EEPROM_TS_MODE;
831 ret = af9015_rw_udev(udev, &req);
834 af9015_config.dual_mode = val;
835 deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
837 /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
838 size can be static because it is enabled only USB2.0 */
839 for (i = 0; i < af9015_properties_count; i++) {
840 /* USB1.1 set smaller buffersize and disable 2nd adapter */
841 if (udev->speed == USB_SPEED_FULL) {
842 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
843 = TS_USB11_MAX_PACKET_SIZE;
844 /* disable 2nd adapter because we don't have
846 af9015_config.dual_mode = 0;
848 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
849 = TS_USB20_MAX_PACKET_SIZE;
853 if (af9015_config.dual_mode) {
854 /* read 2nd demodulator I2C address */
855 req.addr = AF9015_EEPROM_DEMOD2_I2C;
856 ret = af9015_rw_udev(udev, &req);
859 af9015_af9013_config[1].demod_address = val;
861 /* enable 2nd adapter */
862 for (i = 0; i < af9015_properties_count; i++)
863 af9015_properties[i].num_adapters = 2;
866 /* disable 2nd adapter */
867 for (i = 0; i < af9015_properties_count; i++)
868 af9015_properties[i].num_adapters = 1;
871 for (i = 0; i < af9015_properties[0].num_adapters; i++) {
873 offset = AF9015_EEPROM_OFFSET;
875 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
876 ret = af9015_rw_udev(udev, &req);
881 af9015_af9013_config[i].adc_clock = 28800;
884 af9015_af9013_config[i].adc_clock = 20480;
887 af9015_af9013_config[i].adc_clock = 28000;
890 af9015_af9013_config[i].adc_clock = 25000;
893 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
894 val, af9015_af9013_config[i].adc_clock);
897 req.addr = AF9015_EEPROM_IF1H + offset;
898 ret = af9015_rw_udev(udev, &req);
901 af9015_af9013_config[i].tuner_if = val << 8;
902 req.addr = AF9015_EEPROM_IF1L + offset;
903 ret = af9015_rw_udev(udev, &req);
906 af9015_af9013_config[i].tuner_if += val;
907 deb_info("%s: [%d] IF1:%d\n", __func__, i,
908 af9015_af9013_config[0].tuner_if);
911 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
912 ret = af9015_rw_udev(udev, &req);
915 af9015_config.mt2060_if1[i] = val << 8;
916 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
917 ret = af9015_rw_udev(udev, &req);
920 af9015_config.mt2060_if1[i] += val;
921 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
922 af9015_config.mt2060_if1[i]);
925 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
926 ret = af9015_rw_udev(udev, &req);
930 case AF9013_TUNER_ENV77H11D5:
931 case AF9013_TUNER_MT2060:
932 case AF9013_TUNER_MC44S803:
933 case AF9013_TUNER_QT1010:
934 case AF9013_TUNER_UNKNOWN:
935 case AF9013_TUNER_MT2060_2:
936 case AF9013_TUNER_TDA18271:
937 case AF9013_TUNER_QT1010A:
938 af9015_af9013_config[i].rf_spec_inv = 1;
940 case AF9013_TUNER_MXL5003D:
941 case AF9013_TUNER_MXL5005D:
942 case AF9013_TUNER_MXL5005R:
943 af9015_af9013_config[i].rf_spec_inv = 0;
946 warn("tuner id:%d not supported, please report!", val);
950 af9015_af9013_config[i].tuner = val;
951 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
956 err("eeprom read failed:%d", ret);
961 static int af9015_identify_state(struct usb_device *udev,
962 struct dvb_usb_device_properties *props,
963 struct dvb_usb_device_description **desc,
968 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
970 ret = af9015_rw_udev(udev, &req);
974 deb_info("%s: reply:%02x\n", __func__, reply);
983 static int af9015_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
986 struct req_t req = {GET_IR_CODE, 0, 0, 0, 0, sizeof(buf), buf};
987 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
990 memset(buf, 0, sizeof(buf));
992 ret = af9015_ctrl_msg(d, &req);
997 *state = REMOTE_NO_KEY_PRESSED;
999 for (i = 0; i < d->props.rc_key_map_size; i++) {
1000 if (!buf[1] && keymap[i].custom == buf[0] &&
1001 keymap[i].data == buf[2]) {
1002 *event = keymap[i].event;
1003 *state = REMOTE_KEY_PRESSED;
1008 deb_rc("%s: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1009 __func__, buf[0], buf[1], buf[2], buf[3], buf[4],
1010 buf[5], buf[6], buf[7]);
1015 /* init 2nd I2C adapter */
1016 static int af9015_i2c_init(struct dvb_usb_device *d)
1019 struct af9015_state *state = d->priv;
1020 deb_info("%s:\n", __func__);
1022 strncpy(state->i2c_adap.name, d->desc->name,
1023 sizeof(state->i2c_adap.name));
1024 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
1025 state->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
1027 state->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
1029 state->i2c_adap.algo = d->props.i2c_algo;
1030 state->i2c_adap.algo_data = NULL;
1031 state->i2c_adap.dev.parent = &d->udev->dev;
1033 i2c_set_adapdata(&state->i2c_adap, d);
1035 ret = i2c_add_adapter(&state->i2c_adap);
1037 err("could not add i2c adapter");
1042 static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1045 struct af9015_state *state = adap->dev->priv;
1046 struct i2c_adapter *i2c_adap;
1048 if (adap->id == 0) {
1049 /* select I2C adapter */
1050 i2c_adap = &adap->dev->i2c_adap;
1052 deb_info("%s: init I2C\n", __func__);
1053 ret = af9015_i2c_init(adap->dev);
1055 /* dump eeprom (debug) */
1056 ret = af9015_eeprom_dump(adap->dev);
1060 /* select I2C adapter */
1061 i2c_adap = &state->i2c_adap;
1063 /* copy firmware to 2nd demodulator */
1064 if (af9015_config.dual_mode) {
1065 ret = af9015_copy_firmware(adap->dev);
1067 err("firmware copy to 2nd frontend " \
1068 "failed, will disable it");
1069 af9015_config.dual_mode = 0;
1077 /* attach demodulator */
1078 adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1081 return adap->fe == NULL ? -ENODEV : 0;
1084 static struct mt2060_config af9015_mt2060_config = {
1085 .i2c_address = 0xc0,
1089 static struct qt1010_config af9015_qt1010_config = {
1090 .i2c_address = 0xc4,
1093 static struct tda18271_config af9015_tda18271_config = {
1094 .gate = TDA18271_GATE_DIGITAL,
1098 static struct mxl5005s_config af9015_mxl5003_config = {
1099 .i2c_address = 0xc6,
1100 .if_freq = IF_FREQ_4570000HZ,
1101 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1102 .agc_mode = MXL_SINGLE_AGC,
1103 .tracking_filter = MXL_TF_DEFAULT,
1104 .rssi_enable = MXL_RSSI_ENABLE,
1105 .cap_select = MXL_CAP_SEL_ENABLE,
1106 .div_out = MXL_DIV_OUT_4,
1107 .clock_out = MXL_CLOCK_OUT_DISABLE,
1108 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1109 .top = MXL5005S_TOP_25P2,
1110 .mod_mode = MXL_DIGITAL_MODE,
1111 .if_mode = MXL_ZERO_IF,
1112 .AgcMasterByte = 0x00,
1115 static struct mxl5005s_config af9015_mxl5005_config = {
1116 .i2c_address = 0xc6,
1117 .if_freq = IF_FREQ_4570000HZ,
1118 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1119 .agc_mode = MXL_SINGLE_AGC,
1120 .tracking_filter = MXL_TF_OFF,
1121 .rssi_enable = MXL_RSSI_ENABLE,
1122 .cap_select = MXL_CAP_SEL_ENABLE,
1123 .div_out = MXL_DIV_OUT_4,
1124 .clock_out = MXL_CLOCK_OUT_DISABLE,
1125 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1126 .top = MXL5005S_TOP_25P2,
1127 .mod_mode = MXL_DIGITAL_MODE,
1128 .if_mode = MXL_ZERO_IF,
1129 .AgcMasterByte = 0x00,
1132 static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1134 struct af9015_state *state = adap->dev->priv;
1135 struct i2c_adapter *i2c_adap;
1137 deb_info("%s: \n", __func__);
1139 /* select I2C adapter */
1141 i2c_adap = &adap->dev->i2c_adap;
1143 i2c_adap = &state->i2c_adap;
1145 switch (af9015_af9013_config[adap->id].tuner) {
1146 case AF9013_TUNER_MT2060:
1147 case AF9013_TUNER_MT2060_2:
1148 ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
1149 &af9015_mt2060_config,
1150 af9015_config.mt2060_if1[adap->id])
1151 == NULL ? -ENODEV : 0;
1153 case AF9013_TUNER_QT1010:
1154 case AF9013_TUNER_QT1010A:
1155 ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
1156 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1158 case AF9013_TUNER_TDA18271:
1159 ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
1160 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1162 case AF9013_TUNER_MXL5003D:
1163 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1164 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1166 case AF9013_TUNER_MXL5005D:
1167 case AF9013_TUNER_MXL5005R:
1168 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1169 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1171 case AF9013_TUNER_ENV77H11D5:
1172 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1173 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1175 case AF9013_TUNER_MC44S803:
1177 ret = dvb_attach(mc44s80x_attach, adap->fe, i2c_adap)
1178 == NULL ? -ENODEV : 0;
1181 info("Freescale MC44S803 tuner found but no driver for that" \
1182 "tuner. Look at the Linuxtv.org for tuner driver" \
1186 case AF9013_TUNER_UNKNOWN:
1189 err("Unknown tuner id:%d",
1190 af9015_af9013_config[adap->id].tuner);
1195 static struct usb_device_id af9015_usb_table[] = {
1196 /* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
1197 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
1198 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1199 {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
1200 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
1201 /* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1203 {USB_DEVICE(USB_VID_VISIONPLUS,
1204 USB_PID_AZUREWAVE_AD_TU700)},
1205 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1206 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
1207 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1208 /* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1209 {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
1210 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
1211 {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
1212 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
1213 /* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
1214 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
1215 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
1218 MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1220 static struct dvb_usb_device_properties af9015_properties[] = {
1222 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1224 .usb_ctrl = DEVICE_SPECIFIC,
1225 .download_firmware = af9015_download_firmware,
1226 .firmware = "dvb-usb-af9015.fw",
1229 .size_of_priv = sizeof(struct af9015_state), \
1234 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1235 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1237 .pid_filter_count = 32,
1238 .pid_filter = af9015_pid_filter,
1239 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1242 af9015_af9013_frontend_attach,
1243 .tuner_attach = af9015_tuner_attach,
1252 af9015_af9013_frontend_attach,
1253 .tuner_attach = af9015_tuner_attach,
1261 TS_USB20_MAX_PACKET_SIZE,
1268 .identify_state = af9015_identify_state,
1270 .rc_query = af9015_rc_query,
1273 .i2c_algo = &af9015_i2c_algo,
1275 .num_device_descs = 9,
1278 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1279 .cold_ids = {&af9015_usb_table[0],
1280 &af9015_usb_table[1], NULL},
1284 .name = "Leadtek WinFast DTV Dongle Gold",
1285 .cold_ids = {&af9015_usb_table[2], NULL},
1289 .name = "Pinnacle PCTV 71e",
1290 .cold_ids = {&af9015_usb_table[3], NULL},
1294 .name = "KWorld PlusTV Dual DVB-T Stick " \
1296 .cold_ids = {&af9015_usb_table[4], NULL},
1300 .name = "DigitalNow TinyTwin DVB-T Receiver",
1301 .cold_ids = {&af9015_usb_table[5], NULL},
1305 .name = "TwinHan AzureWave AD-TU700(704J)",
1306 .cold_ids = {&af9015_usb_table[6], NULL},
1310 .name = "TerraTec Cinergy T USB XE",
1311 .cold_ids = {&af9015_usb_table[7], NULL},
1315 .name = "KWorld PlusTV Dual DVB-T PCI " \
1317 .cold_ids = {&af9015_usb_table[8], NULL},
1321 .name = "AVerMedia AVerTV DVB-T Volar X",
1322 .cold_ids = {&af9015_usb_table[9], NULL},
1327 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1329 .usb_ctrl = DEVICE_SPECIFIC,
1330 .download_firmware = af9015_download_firmware,
1331 .firmware = "dvb-usb-af9015.fw",
1334 .size_of_priv = sizeof(struct af9015_state), \
1339 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1340 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1342 .pid_filter_count = 32,
1343 .pid_filter = af9015_pid_filter,
1344 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1347 af9015_af9013_frontend_attach,
1348 .tuner_attach = af9015_tuner_attach,
1357 af9015_af9013_frontend_attach,
1358 .tuner_attach = af9015_tuner_attach,
1366 TS_USB20_MAX_PACKET_SIZE,
1373 .identify_state = af9015_identify_state,
1375 .rc_query = af9015_rc_query,
1378 .i2c_algo = &af9015_i2c_algo,
1380 .num_device_descs = 7,
1383 .name = "Xtensions XD-380",
1384 .cold_ids = {&af9015_usb_table[10], NULL},
1388 .name = "MSI DIGIVOX Duo",
1389 .cold_ids = {&af9015_usb_table[11], NULL},
1393 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1394 .cold_ids = {&af9015_usb_table[12], NULL},
1398 .name = "Telestar Starstick 2",
1399 .cold_ids = {&af9015_usb_table[13], NULL},
1403 .name = "AVerMedia A309",
1404 .cold_ids = {&af9015_usb_table[14], NULL},
1408 .name = "MSI Digi VOX mini III",
1409 .cold_ids = {&af9015_usb_table[15], NULL},
1413 .name = "KWorld USB DVB-T TV Stick II " \
1415 .cold_ids = {&af9015_usb_table[16],
1416 &af9015_usb_table[17], NULL},
1423 static int af9015_usb_probe(struct usb_interface *intf,
1424 const struct usb_device_id *id)
1427 struct dvb_usb_device *d = NULL;
1428 struct usb_device *udev = interface_to_usbdev(intf);
1431 deb_info("%s: interface:%d\n", __func__,
1432 intf->cur_altsetting->desc.bInterfaceNumber);
1434 /* interface 0 is used by DVB-T receiver and
1435 interface 1 is for remote controller (HID) */
1436 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1437 ret = af9015_read_config(udev);
1441 for (i = 0; i < af9015_properties_count; i++) {
1442 ret = dvb_usb_device_init(intf, &af9015_properties[i],
1443 THIS_MODULE, &d, adapter_nr);
1453 ret = af9015_init(d);
1459 static void af9015_i2c_exit(struct dvb_usb_device *d)
1461 struct af9015_state *state = d->priv;
1462 deb_info("%s: \n", __func__);
1464 /* remove 2nd I2C adapter */
1465 if (d->state & DVB_USB_STATE_I2C)
1466 i2c_del_adapter(&state->i2c_adap);
1469 static void af9015_usb_device_exit(struct usb_interface *intf)
1471 struct dvb_usb_device *d = usb_get_intfdata(intf);
1472 deb_info("%s: \n", __func__);
1474 /* remove 2nd I2C adapter */
1475 if (d != NULL && d->desc != NULL)
1478 dvb_usb_device_exit(intf);
1481 /* usb specific object needed to register this driver with the usb subsystem */
1482 static struct usb_driver af9015_usb_driver = {
1483 .name = "dvb_usb_af9015",
1484 .probe = af9015_usb_probe,
1485 .disconnect = af9015_usb_device_exit,
1486 .id_table = af9015_usb_table,
1490 static int __init af9015_usb_module_init(void)
1493 ret = usb_register(&af9015_usb_driver);
1495 err("module init failed:%d", ret);
1500 static void __exit af9015_usb_module_exit(void)
1502 /* deregister this driver from the USB subsystem */
1503 usb_deregister(&af9015_usb_driver);
1506 module_init(af9015_usb_module_init);
1507 module_exit(af9015_usb_module_exit);
1509 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1510 MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1511 MODULE_LICENSE("GPL");