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 static int dvb_usb_af9015_dual_mode;
41 module_param_named(dual_mode, dvb_usb_af9015_dual_mode, int, 0644);
42 MODULE_PARM_DESC(dual_mode, "enable dual mode");
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[2];
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,
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)
73 static u8 seq; /* packet sequence number */
75 if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
80 buf[2] = req->i2c_addr;
81 buf[3] = req->addr >> 8;
82 buf[4] = req->addr & 0xff;
84 buf[6] = req->addr_len;
85 buf[7] = req->data_len;
97 buf[2] |= 0x01; /* set I2C direction */
99 buf[0] = READ_WRITE_I2C;
102 if (((req->addr & 0xff00) == 0xff00) ||
103 ((req->addr & 0xae00) == 0xae00))
104 buf[0] = WRITE_VIRTUAL_MEMORY;
105 case WRITE_VIRTUAL_MEMORY:
107 case DOWNLOAD_FIRMWARE:
110 err("unknown command:%d", req->cmd);
115 /* write requested */
117 memcpy(&buf[8], req->data, req->data_len);
118 msg_len += req->data_len;
121 debug_dump(buf, msg_len, deb_xfer);
124 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
125 &act_len, AF9015_USB_TIMEOUT);
127 err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
129 if (act_len != msg_len)
130 ret = -1; /* all data is not send */
134 /* no ack for those packets */
135 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
138 /* receive ack and data if read req */
139 msg_len = 1 + 1 + req->data_len; /* seq + status + data len */
140 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
141 &act_len, AF9015_USB_TIMEOUT);
143 err("recv bulk message failed:%d", ret);
149 debug_dump(buf, act_len, deb_xfer);
151 /* remote controller query status is 1 if remote code is not received */
152 if (req->cmd == GET_IR_CODE && buf[1] == 1) {
153 buf[1] = 0; /* clear command "error" status */
154 memset(&buf[2], 0, req->data_len);
155 buf[3] = 1; /* no remote code received mark */
160 err("command failed:%d", buf[1]);
165 /* read request, copy returned data to return buf */
167 memcpy(req->data, &buf[2], req->data_len);
171 mutex_unlock(&af9015_usb_mutex);
176 static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
178 return af9015_rw_udev(d->udev, req);
181 static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
184 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
186 return af9015_ctrl_msg(d, &req);
189 static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
191 return af9015_write_regs(d, addr, &val, 1);
194 static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
196 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, 1, val};
197 return af9015_ctrl_msg(d, &req);
200 static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
203 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
205 if (addr == af9015_af9013_config[0].demod_address ||
206 addr == af9015_af9013_config[1].demod_address)
209 return af9015_ctrl_msg(d, &req);
212 static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
215 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
217 if (addr == af9015_af9013_config[0].demod_address ||
218 addr == af9015_af9013_config[1].demod_address)
221 return af9015_ctrl_msg(d, &req);
224 static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
227 struct dvb_usb_device *d = i2c_get_adapdata(adap);
233 /* TODO: implement bus lock
235 The bus lock is needed because there is two tuners both using same I2C-address.
236 Due to that the only way to select correct tuner is use demodulator I2C-gate.
238 ................................................
239 . AF9015 includes integrated AF9013 demodulator.
240 . ____________ ____________ . ____________
241 .| uC | | demod | . | tuner |
242 .|------------| |------------| . |------------|
243 .| AF9015 | | AF9013/5 | . | MXL5003 |
244 .| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
245 .| | | | addr 0x38 | . | addr 0xc6 |
246 .|____________| | |____________| . |____________|
247 .................|..............................
248 | ____________ ____________
249 | | demod | | tuner |
250 | |------------| |------------|
251 | | AF9013 | | MXL5003 |
252 +----I2C-------|-----/ -----|-------I2C-------| |
253 | addr 0x3a | | addr 0xc6 |
254 |____________| |____________|
256 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
260 if (msg[i].addr == af9015_af9013_config[0].demod_address ||
261 msg[i].addr == af9015_af9013_config[1].demod_address) {
262 addr = msg[i].buf[0] << 8;
263 addr += msg[i].buf[1];
264 mbox = msg[i].buf[2];
267 addr = msg[i].buf[0];
272 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
274 af9015_af9013_config[0].demod_address)
275 req.cmd = READ_MEMORY;
278 req.i2c_addr = msg[i].addr;
281 req.addr_len = addr_len;
282 req.data_len = msg[i+1].len;
283 req.data = &msg[i+1].buf[0];
284 ret = af9015_ctrl_msg(d, &req);
288 af9015_af9013_config[0].demod_address)
289 req.cmd = WRITE_MEMORY;
292 req.i2c_addr = msg[i].addr;
295 req.addr_len = addr_len;
296 req.data_len = msg[i].len-addr_len;
297 req.data = &msg[i].buf[addr_len];
298 ret = af9015_ctrl_msg(d, &req);
308 mutex_unlock(&d->i2c_mutex);
313 static u32 af9015_i2c_func(struct i2c_adapter *adapter)
318 static struct i2c_algorithm af9015_i2c_algo = {
319 .master_xfer = af9015_i2c_xfer,
320 .functionality = af9015_i2c_func,
323 static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
328 ret = af9015_read_reg(d, addr, &val);
342 return af9015_write_reg(d, addr, val);
345 static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
347 return af9015_do_reg_bit(d, addr, bit, 1);
350 static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
352 return af9015_do_reg_bit(d, addr, bit, 0);
355 static int af9015_init_endpoint(struct dvb_usb_device *d)
360 deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
362 #define TS_PACKET_SIZE 188
364 #define TS_USB20_PACKET_COUNT 348
365 #define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
367 #define TS_USB11_PACKET_COUNT 21
368 #define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
370 #define TS_USB20_MAX_PACKET_SIZE 512
371 #define TS_USB11_MAX_PACKET_SIZE 64
373 if (d->udev->speed == USB_SPEED_FULL) {
374 frame_size = TS_USB11_FRAME_SIZE/4;
375 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
377 frame_size = TS_USB20_FRAME_SIZE/4;
378 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
381 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
384 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
387 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
390 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
393 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
396 if (af9015_config.dual_mode) {
397 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
401 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
404 if (af9015_config.dual_mode) {
405 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
409 /* EP4 xfer length */
410 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
413 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
416 /* EP5 xfer length */
417 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
420 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
423 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
426 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
429 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
432 if (af9015_config.dual_mode) {
433 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
438 /* enable / disable mp2if2 */
439 if (af9015_config.dual_mode)
440 ret = af9015_set_reg_bit(d, 0xd50b, 0);
442 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
445 err("endpoint init failed:%d", ret);
449 static int af9015_copy_firmware(struct dvb_usb_device *d)
454 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
456 deb_info("%s:\n", __func__);
458 fw_params[0] = af9015_config.firmware_size >> 8;
459 fw_params[1] = af9015_config.firmware_size & 0xff;
460 fw_params[2] = af9015_config.firmware_checksum >> 8;
461 fw_params[3] = af9015_config.firmware_checksum & 0xff;
463 /* wait 2nd demodulator ready */
466 ret = af9015_read_reg_i2c(d, 0x3a, 0x98be, &val);
470 deb_info("%s: firmware status:%02x\n", __func__, val);
472 if (val == 0x0c) /* fw is running, no need for download */
475 /* set I2C master clock to fast (to speed up firmware copy) */
476 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
483 ret = af9015_ctrl_msg(d, &req);
485 err("firmware copy cmd failed:%d", ret);
486 deb_info("%s: firmware copy done\n", __func__);
488 /* set I2C master clock back to normal */
489 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
493 /* request boot firmware */
494 ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
496 deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
500 for (i = 0; i < 15; i++) {
503 /* check firmware status */
504 ret = af9015_read_reg_i2c(d,
505 af9015_af9013_config[1].demod_address, 0x98be, &val);
506 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
511 if (val == 0x0c || val == 0x04) /* success or fail */
516 err("firmware did not run");
518 } else if (val != 0x0c) {
519 err("firmware boot timeout");
529 static int af9015_eeprom_dump(struct dvb_usb_device *d)
531 char buf[52], buf2[4];
534 for (reg = 0; ; reg++) {
537 deb_info("%s\n", buf);
538 sprintf(buf, "%02x: ", reg);
540 if (af9015_read_reg_i2c(d, AF9015_I2C_EEPROM, reg, &val) == 0)
541 sprintf(buf2, "%02x ", val);
548 deb_info("%s\n", buf);
552 static int af9015_download_ir_table(struct dvb_usb_device *d)
554 int i, packets = 0, ret;
555 u16 addr = 0x9a56; /* ir-table start address */
556 struct req_t req = {WRITE_MEMORY, 0, 0, 0, 0, 1, NULL};
558 deb_info("%s:\n", __func__);
560 data = af9015_config.ir_table;
561 packets = af9015_config.ir_table_size;
567 /* load remote ir-table */
568 for (i = 0; i < packets; i++) {
571 ret = af9015_ctrl_msg(d, &req);
573 err("ir-table download failed at packet %d with " \
583 static int af9015_init(struct dvb_usb_device *d)
586 deb_info("%s:\n", __func__);
588 ret = af9015_init_endpoint(d);
592 ret = af9015_download_ir_table(d);
600 static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
603 deb_info("%s: onoff:%d\n", __func__, onoff);
606 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
608 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
613 static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
619 deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
620 __func__, index, pid, onoff);
622 ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
626 ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
630 idx = ((index & 0x1f) | (1 << 5));
631 ret = af9015_write_reg(adap->dev, 0xd504, idx);
637 static int af9015_download_firmware(struct usb_device *udev,
638 const struct firmware *fw)
640 int i, len, packets, remainder, ret;
641 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
642 u16 addr = 0x5100; /* firmware start address */
645 deb_info("%s:\n", __func__);
648 for (i = 0; i < fw->size; i++)
649 checksum += fw->data[i];
651 af9015_config.firmware_size = fw->size;
652 af9015_config.firmware_checksum = checksum;
654 #define FW_PACKET_MAX_DATA 55
656 packets = fw->size / FW_PACKET_MAX_DATA;
657 remainder = fw->size % FW_PACKET_MAX_DATA;
658 len = FW_PACKET_MAX_DATA;
659 for (i = 0; i <= packets; i++) {
660 if (i == packets) /* set size of the last packet */
664 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
666 addr += FW_PACKET_MAX_DATA;
668 ret = af9015_rw_udev(udev, &req);
670 err("firmware download failed at packet %d with " \
676 /* firmware loaded, request boot */
678 ret = af9015_rw_udev(udev, &req);
680 err("firmware boot failed:%d", ret);
684 /* firmware is running, reconnect device in the usb bus */
685 req.cmd = RECONNECT_USB;
686 ret = af9015_rw_udev(udev, &req);
688 err("reconnect failed: %d", ret);
694 static int af9015_read_config(struct usb_device *udev)
697 u8 val, i, offset = 0;
698 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
699 char manufacturer[10];
701 /* IR remote controller */
702 req.addr = AF9015_EEPROM_IR_MODE;
703 ret = af9015_rw_udev(udev, &req);
706 deb_info("%s: IR mode:%d\n", __func__, val);
707 for (i = 0; i < af9015_properties_count; i++) {
708 if (val == AF9015_IR_MODE_DISABLED || val == 0x04) {
709 af9015_properties[i].rc_key_map = NULL;
710 af9015_properties[i].rc_key_map_size = 0;
711 } else if (dvb_usb_af9015_remote) {
712 /* load remote defined as module param */
713 switch (dvb_usb_af9015_remote) {
714 case AF9015_REMOTE_A_LINK_DTU_M:
715 af9015_properties[i].rc_key_map =
716 af9015_rc_keys_a_link;
717 af9015_properties[i].rc_key_map_size =
718 ARRAY_SIZE(af9015_rc_keys_a_link);
719 af9015_config.ir_table = af9015_ir_table_a_link;
720 af9015_config.ir_table_size =
721 ARRAY_SIZE(af9015_ir_table_a_link);
723 case AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3:
724 af9015_properties[i].rc_key_map =
726 af9015_properties[i].rc_key_map_size =
727 ARRAY_SIZE(af9015_rc_keys_msi);
728 af9015_config.ir_table = af9015_ir_table_msi;
729 af9015_config.ir_table_size =
730 ARRAY_SIZE(af9015_ir_table_msi);
732 case AF9015_REMOTE_MYGICTV_U718:
733 af9015_properties[i].rc_key_map =
734 af9015_rc_keys_mygictv;
735 af9015_properties[i].rc_key_map_size =
736 ARRAY_SIZE(af9015_rc_keys_mygictv);
737 af9015_config.ir_table =
738 af9015_ir_table_mygictv;
739 af9015_config.ir_table_size =
740 ARRAY_SIZE(af9015_ir_table_mygictv);
744 switch (udev->descriptor.idVendor) {
745 case USB_VID_LEADTEK:
746 af9015_properties[i].rc_key_map =
747 af9015_rc_keys_leadtek;
748 af9015_properties[i].rc_key_map_size =
749 ARRAY_SIZE(af9015_rc_keys_leadtek);
750 af9015_config.ir_table =
751 af9015_ir_table_leadtek;
752 af9015_config.ir_table_size =
753 ARRAY_SIZE(af9015_ir_table_leadtek);
755 case USB_VID_VISIONPLUS:
756 if (udev->descriptor.idProduct ==
757 USB_PID_AZUREWAVE_AD_TU700) {
758 af9015_properties[i].rc_key_map =
759 af9015_rc_keys_twinhan;
760 af9015_properties[i].rc_key_map_size =
761 ARRAY_SIZE(af9015_rc_keys_twinhan);
762 af9015_config.ir_table =
763 af9015_ir_table_twinhan;
764 af9015_config.ir_table_size =
765 ARRAY_SIZE(af9015_ir_table_twinhan);
768 case USB_VID_KWORLD_2:
769 /* TODO: use correct rc keys */
770 af9015_properties[i].rc_key_map =
771 af9015_rc_keys_twinhan;
772 af9015_properties[i].rc_key_map_size =
773 ARRAY_SIZE(af9015_rc_keys_twinhan);
774 af9015_config.ir_table = af9015_ir_table_kworld;
775 af9015_config.ir_table_size =
776 ARRAY_SIZE(af9015_ir_table_kworld);
778 /* Check USB manufacturer and product strings and try
779 to determine correct remote in case of chip vendor
780 reference IDs are used. */
781 case USB_VID_AFATECH:
782 memset(manufacturer, 0, sizeof(manufacturer));
783 usb_string(udev, udev->descriptor.iManufacturer,
784 manufacturer, sizeof(manufacturer));
785 if (!strcmp("Geniatech", manufacturer)) {
786 /* iManufacturer 1 Geniatech
788 af9015_properties[i].rc_key_map =
789 af9015_rc_keys_mygictv;
790 af9015_properties[i].rc_key_map_size =
791 ARRAY_SIZE(af9015_rc_keys_mygictv);
792 af9015_config.ir_table =
793 af9015_ir_table_mygictv;
794 af9015_config.ir_table_size =
795 ARRAY_SIZE(af9015_ir_table_mygictv);
796 } else if (!strcmp("MSI", manufacturer)) {
797 /* iManufacturer 1 MSI
798 iProduct 2 MSI K-VOX */
799 af9015_properties[i].rc_key_map =
801 af9015_properties[i].rc_key_map_size =
802 ARRAY_SIZE(af9015_rc_keys_msi);
803 af9015_config.ir_table =
805 af9015_config.ir_table_size =
806 ARRAY_SIZE(af9015_ir_table_msi);
813 /* TS mode - one or two receivers */
814 req.addr = AF9015_EEPROM_TS_MODE;
815 ret = af9015_rw_udev(udev, &req);
818 af9015_config.dual_mode = val;
819 deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
820 /* disable dual mode by default because it is buggy */
821 if (!dvb_usb_af9015_dual_mode)
822 af9015_config.dual_mode = 0;
824 /* set buffer size according to USB port speed */
825 for (i = 0; i < af9015_properties_count; i++) {
826 /* USB1.1 set smaller buffersize and disable 2nd adapter */
827 if (udev->speed == USB_SPEED_FULL) {
828 af9015_properties[i].adapter->stream.u.bulk.buffersize =
829 TS_USB11_MAX_PACKET_SIZE;
830 /* disable 2nd adapter because we don't have
832 af9015_config.dual_mode = 0;
834 af9015_properties[i].adapter->stream.u.bulk.buffersize =
835 TS_USB20_MAX_PACKET_SIZE;
839 if (af9015_config.dual_mode) {
840 /* read 2nd demodulator I2C address */
841 req.addr = AF9015_EEPROM_DEMOD2_I2C;
842 ret = af9015_rw_udev(udev, &req);
845 af9015_af9013_config[1].demod_address = val;
847 /* enable 2nd adapter */
848 for (i = 0; i < af9015_properties_count; i++)
849 af9015_properties[i].num_adapters = 2;
852 /* disable 2nd adapter */
853 for (i = 0; i < af9015_properties_count; i++)
854 af9015_properties[i].num_adapters = 1;
857 for (i = 0; i < af9015_properties[0].num_adapters; i++) {
859 offset = AF9015_EEPROM_OFFSET;
861 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
862 ret = af9015_rw_udev(udev, &req);
867 af9015_af9013_config[i].adc_clock = 28800;
870 af9015_af9013_config[i].adc_clock = 20480;
873 af9015_af9013_config[i].adc_clock = 28000;
876 af9015_af9013_config[i].adc_clock = 25000;
879 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
880 val, af9015_af9013_config[i].adc_clock);
883 req.addr = AF9015_EEPROM_IF1H + offset;
884 ret = af9015_rw_udev(udev, &req);
887 af9015_af9013_config[i].tuner_if = val << 8;
888 req.addr = AF9015_EEPROM_IF1L + offset;
889 ret = af9015_rw_udev(udev, &req);
892 af9015_af9013_config[i].tuner_if += val;
893 deb_info("%s: [%d] IF1:%d\n", __func__, i,
894 af9015_af9013_config[0].tuner_if);
897 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
898 ret = af9015_rw_udev(udev, &req);
901 af9015_config.mt2060_if1[i] = val << 8;
902 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
903 ret = af9015_rw_udev(udev, &req);
906 af9015_config.mt2060_if1[i] += val;
907 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
908 af9015_config.mt2060_if1[i]);
911 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
912 ret = af9015_rw_udev(udev, &req);
916 case AF9013_TUNER_ENV77H11D5:
917 case AF9013_TUNER_MT2060:
918 case AF9013_TUNER_MC44S803:
919 case AF9013_TUNER_QT1010:
920 case AF9013_TUNER_UNKNOWN:
921 case AF9013_TUNER_MT2060_2:
922 case AF9013_TUNER_TDA18271:
923 case AF9013_TUNER_QT1010A:
924 af9015_af9013_config[i].rf_spec_inv = 1;
926 case AF9013_TUNER_MXL5003D:
927 case AF9013_TUNER_MXL5005D:
928 case AF9013_TUNER_MXL5005R:
929 af9015_af9013_config[i].rf_spec_inv = 0;
932 warn("tuner id:%d not supported, please report!", val);
936 af9015_af9013_config[i].tuner = val;
937 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
942 err("eeprom read failed:%d", ret);
947 static int af9015_identify_state(struct usb_device *udev,
948 struct dvb_usb_device_properties *props,
949 struct dvb_usb_device_description **desc,
954 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
956 ret = af9015_rw_udev(udev, &req);
960 deb_info("%s: reply:%02x\n", __func__, reply);
969 static int af9015_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
972 struct req_t req = {GET_IR_CODE, 0, 0, 0, 0, sizeof(buf), buf};
973 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
976 memset(buf, 0, sizeof(buf));
978 ret = af9015_ctrl_msg(d, &req);
983 *state = REMOTE_NO_KEY_PRESSED;
985 for (i = 0; i < d->props.rc_key_map_size; i++) {
986 if (!buf[1] && keymap[i].custom == buf[0] &&
987 keymap[i].data == buf[2]) {
988 *event = keymap[i].event;
989 *state = REMOTE_KEY_PRESSED;
994 deb_rc("%s: %02x %02x %02x %02x %02x %02x %02x %02x\n",
995 __func__, buf[0], buf[1], buf[2], buf[3], buf[4],
996 buf[5], buf[6], buf[7]);
1001 /* init 2nd I2C adapter */
1002 static int af9015_i2c_init(struct dvb_usb_device *d)
1005 struct af9015_state *state = d->priv;
1006 deb_info("%s:\n", __func__);
1008 strncpy(state->i2c_adap.name, d->desc->name,
1009 sizeof(state->i2c_adap.name));
1010 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
1011 state->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
1013 state->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
1015 state->i2c_adap.algo = d->props.i2c_algo;
1016 state->i2c_adap.algo_data = NULL;
1017 state->i2c_adap.dev.parent = &d->udev->dev;
1019 i2c_set_adapdata(&state->i2c_adap, d);
1021 ret = i2c_add_adapter(&state->i2c_adap);
1023 err("could not add i2c adapter");
1028 static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1031 struct af9015_state *state = adap->dev->priv;
1032 struct i2c_adapter *i2c_adap;
1034 if (adap->id == 0) {
1035 /* select I2C adapter */
1036 i2c_adap = &adap->dev->i2c_adap;
1038 deb_info("%s: init I2C\n", __func__);
1039 ret = af9015_i2c_init(adap->dev);
1041 /* dump eeprom (debug) */
1042 ret = af9015_eeprom_dump(adap->dev);
1046 /* select I2C adapter */
1047 i2c_adap = &state->i2c_adap;
1049 /* copy firmware to 2nd demodulator */
1050 if (af9015_config.dual_mode) {
1051 ret = af9015_copy_firmware(adap->dev);
1053 err("firmware copy to 2nd frontend " \
1054 "failed, will disable it");
1055 af9015_config.dual_mode = 0;
1063 /* attach demodulator */
1064 adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1067 return adap->fe == NULL ? -ENODEV : 0;
1070 static struct mt2060_config af9015_mt2060_config = {
1071 .i2c_address = 0xc0,
1075 static struct qt1010_config af9015_qt1010_config = {
1076 .i2c_address = 0xc4,
1079 static struct tda18271_config af9015_tda18271_config = {
1080 .gate = TDA18271_GATE_DIGITAL,
1084 static struct mxl5005s_config af9015_mxl5003_config = {
1085 .i2c_address = 0xc6,
1086 .if_freq = IF_FREQ_4570000HZ,
1087 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1088 .agc_mode = MXL_SINGLE_AGC,
1089 .tracking_filter = MXL_TF_DEFAULT,
1090 .rssi_enable = MXL_RSSI_ENABLE,
1091 .cap_select = MXL_CAP_SEL_ENABLE,
1092 .div_out = MXL_DIV_OUT_4,
1093 .clock_out = MXL_CLOCK_OUT_DISABLE,
1094 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1095 .top = MXL5005S_TOP_25P2,
1096 .mod_mode = MXL_DIGITAL_MODE,
1097 .if_mode = MXL_ZERO_IF,
1098 .AgcMasterByte = 0x00,
1101 static struct mxl5005s_config af9015_mxl5005_config = {
1102 .i2c_address = 0xc6,
1103 .if_freq = IF_FREQ_4570000HZ,
1104 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1105 .agc_mode = MXL_SINGLE_AGC,
1106 .tracking_filter = MXL_TF_OFF,
1107 .rssi_enable = MXL_RSSI_ENABLE,
1108 .cap_select = MXL_CAP_SEL_ENABLE,
1109 .div_out = MXL_DIV_OUT_4,
1110 .clock_out = MXL_CLOCK_OUT_DISABLE,
1111 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1112 .top = MXL5005S_TOP_25P2,
1113 .mod_mode = MXL_DIGITAL_MODE,
1114 .if_mode = MXL_ZERO_IF,
1115 .AgcMasterByte = 0x00,
1118 static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1120 struct af9015_state *state = adap->dev->priv;
1121 struct i2c_adapter *i2c_adap;
1123 deb_info("%s: \n", __func__);
1125 /* select I2C adapter */
1127 i2c_adap = &adap->dev->i2c_adap;
1129 i2c_adap = &state->i2c_adap;
1131 switch (af9015_af9013_config[adap->id].tuner) {
1132 case AF9013_TUNER_MT2060:
1133 case AF9013_TUNER_MT2060_2:
1134 ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
1135 &af9015_mt2060_config,
1136 af9015_config.mt2060_if1[adap->id])
1137 == NULL ? -ENODEV : 0;
1139 case AF9013_TUNER_QT1010:
1140 case AF9013_TUNER_QT1010A:
1141 ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
1142 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1144 case AF9013_TUNER_TDA18271:
1145 ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
1146 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1148 case AF9013_TUNER_MXL5003D:
1149 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1150 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1152 case AF9013_TUNER_MXL5005D:
1153 case AF9013_TUNER_MXL5005R:
1154 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1155 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1157 case AF9013_TUNER_ENV77H11D5:
1158 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1159 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1161 case AF9013_TUNER_MC44S803:
1163 ret = dvb_attach(mc44s80x_attach, adap->fe, i2c_adap)
1164 == NULL ? -ENODEV : 0;
1167 info("Freescale MC44S803 tuner found but no driver for that" \
1168 "tuner. Look at the Linuxtv.org for tuner driver" \
1172 case AF9013_TUNER_UNKNOWN:
1175 err("Unknown tuner id:%d",
1176 af9015_af9013_config[adap->id].tuner);
1181 static struct usb_device_id af9015_usb_table[] = {
1182 /* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
1183 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
1184 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1185 {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
1186 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
1187 /* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1189 {USB_DEVICE(USB_VID_VISIONPLUS,
1190 USB_PID_AZUREWAVE_AD_TU700)},
1191 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1192 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
1193 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1194 /* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1195 {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
1196 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
1197 {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
1198 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
1199 /* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
1202 MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1204 static struct dvb_usb_device_properties af9015_properties[] = {
1206 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1208 .usb_ctrl = DEVICE_SPECIFIC,
1209 .download_firmware = af9015_download_firmware,
1210 .firmware = "dvb-usb-af9015.fw",
1212 .size_of_priv = sizeof(struct af9015_state), \
1217 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1218 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1220 .pid_filter_count = 32,
1221 .pid_filter = af9015_pid_filter,
1222 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1225 af9015_af9013_frontend_attach,
1226 .tuner_attach = af9015_tuner_attach,
1235 af9015_af9013_frontend_attach,
1236 .tuner_attach = af9015_tuner_attach,
1245 .identify_state = af9015_identify_state,
1247 .rc_query = af9015_rc_query,
1250 .i2c_algo = &af9015_i2c_algo,
1252 .num_device_descs = 9,
1255 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1256 .cold_ids = {&af9015_usb_table[0],
1257 &af9015_usb_table[1], NULL},
1261 .name = "Leadtek WinFast DTV Dongle Gold",
1262 .cold_ids = {&af9015_usb_table[2], NULL},
1266 .name = "Pinnacle PCTV 71e",
1267 .cold_ids = {&af9015_usb_table[3], NULL},
1271 .name = "KWorld PlusTV Dual DVB-T Stick " \
1273 .cold_ids = {&af9015_usb_table[4], NULL},
1277 .name = "DigitalNow TinyTwin DVB-T Receiver",
1278 .cold_ids = {&af9015_usb_table[5], NULL},
1282 .name = "TwinHan AzureWave AD-TU700(704J)",
1283 .cold_ids = {&af9015_usb_table[6], NULL},
1287 .name = "TerraTec Cinergy T USB XE",
1288 .cold_ids = {&af9015_usb_table[7], NULL},
1292 .name = "KWorld PlusTV Dual DVB-T PCI " \
1294 .cold_ids = {&af9015_usb_table[8], NULL},
1298 .name = "AVerMedia AVerTV DVB-T Volar X",
1299 .cold_ids = {&af9015_usb_table[9], NULL},
1304 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1306 .usb_ctrl = DEVICE_SPECIFIC,
1307 .download_firmware = af9015_download_firmware,
1308 .firmware = "dvb-usb-af9015.fw",
1310 .size_of_priv = sizeof(struct af9015_state), \
1315 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1316 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1318 .pid_filter_count = 32,
1319 .pid_filter = af9015_pid_filter,
1320 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1323 af9015_af9013_frontend_attach,
1324 .tuner_attach = af9015_tuner_attach,
1333 af9015_af9013_frontend_attach,
1334 .tuner_attach = af9015_tuner_attach,
1343 .identify_state = af9015_identify_state,
1345 .rc_query = af9015_rc_query,
1348 .i2c_algo = &af9015_i2c_algo,
1350 .num_device_descs = 6,
1353 .name = "Xtensions XD-380",
1354 .cold_ids = {&af9015_usb_table[10], NULL},
1358 .name = "MSI DIGIVOX Duo",
1359 .cold_ids = {&af9015_usb_table[11], NULL},
1363 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1364 .cold_ids = {&af9015_usb_table[12], NULL},
1368 .name = "Telestar Starstick 2",
1369 .cold_ids = {&af9015_usb_table[13], NULL},
1373 .name = "AVerMedia A309",
1374 .cold_ids = {&af9015_usb_table[14], NULL},
1378 .name = "MSI Digi VOX mini III",
1379 .cold_ids = {&af9015_usb_table[15], NULL},
1386 static int af9015_usb_probe(struct usb_interface *intf,
1387 const struct usb_device_id *id)
1390 struct dvb_usb_device *d = NULL;
1391 struct usb_device *udev = interface_to_usbdev(intf);
1394 deb_info("%s: interface:%d\n", __func__,
1395 intf->cur_altsetting->desc.bInterfaceNumber);
1397 /* interface 0 is used by DVB-T receiver and
1398 interface 1 is for remote controller (HID) */
1399 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1400 ret = af9015_read_config(udev);
1404 for (i = 0; i < af9015_properties_count; i++) {
1405 ret = dvb_usb_device_init(intf, &af9015_properties[i],
1406 THIS_MODULE, &d, adapter_nr);
1416 ret = af9015_init(d);
1422 static void af9015_i2c_exit(struct dvb_usb_device *d)
1424 struct af9015_state *state = d->priv;
1425 deb_info("%s: \n", __func__);
1427 /* remove 2nd I2C adapter */
1428 if (d->state & DVB_USB_STATE_I2C)
1429 i2c_del_adapter(&state->i2c_adap);
1432 static void af9015_usb_device_exit(struct usb_interface *intf)
1434 struct dvb_usb_device *d = usb_get_intfdata(intf);
1435 deb_info("%s: \n", __func__);
1437 /* remove 2nd I2C adapter */
1438 if (d != NULL && d->desc != NULL)
1441 dvb_usb_device_exit(intf);
1444 /* usb specific object needed to register this driver with the usb subsystem */
1445 static struct usb_driver af9015_usb_driver = {
1446 .name = "dvb_usb_af9015",
1447 .probe = af9015_usb_probe,
1448 .disconnect = af9015_usb_device_exit,
1449 .id_table = af9015_usb_table,
1453 static int __init af9015_usb_module_init(void)
1456 ret = usb_register(&af9015_usb_driver);
1458 err("module init failed:%d", ret);
1463 static void __exit af9015_usb_module_exit(void)
1465 /* deregister this driver from the USB subsystem */
1466 usb_deregister(&af9015_usb_driver);
1469 module_init(af9015_usb_module_init);
1470 module_exit(af9015_usb_module_exit);
1472 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1473 MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1474 MODULE_LICENSE("GPL");