2 * Driver for AzureWave 6007 DVB-C/T USB2.0 and clones
4 * Copyright (c) Henry Wang <Henry.wang@AzureWave.com>
6 * This driver was made publicly available by Terratec, at:
7 * http://linux.terratec.de/files/TERRATEC_H7/20110323_TERRATEC_H7_Linux.tar.gz
8 * The original driver's license is GPL, as declared with MODULE_LICENSE()
10 * Copyright (c) 2010-2012 Mauro Carvalho Chehab <mchehab@redhat.com>
11 * Driver modified by in order to work with upstream drxk driver, and
12 * tons of bugs got fixed, and converted to use dvb-usb-v2.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation under version 2 of the License.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
26 #include "dvb_ca_en50221.h"
28 #include "cypress_firmware.h"
30 #define AZ6007_FIRMWARE "dvb-usb-terratec-h7-az6007.fw"
32 static int az6007_xfer_debug;
33 module_param_named(xfer_debug, az6007_xfer_debug, int, 0644);
34 MODULE_PARM_DESC(xfer_debug, "Enable xfer debug");
36 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
38 /* Known requests (Cypress FX2 firmware + az6007 "private" ones*/
41 #define AZ6007_READ_DATA 0xb7
42 #define AZ6007_I2C_RD 0xb9
43 #define AZ6007_POWER 0xbc
44 #define AZ6007_I2C_WR 0xbd
45 #define FX2_SCON1 0xc0
46 #define AZ6007_TS_THROUGH 0xc7
47 #define AZ6007_READ_IR 0xb4
49 struct az6007_device_state {
51 struct mutex ca_mutex;
52 struct dvb_ca_en50221 ca;
54 int (*gate_ctrl) (struct dvb_frontend *, int);
55 unsigned char data[4096];
58 static struct drxk_config terratec_h7_drxk = {
62 .single_master = true,
63 .enable_merr_cfg = true,
64 .no_i2c_bridge = false,
66 .mpeg_out_clk_strength = 0x02,
67 .qam_demod_parameter_count = 2,
68 .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
71 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
73 struct az6007_device_state *st = fe_to_priv(fe);
74 struct dvb_usb_adapter *adap = fe->sec_priv;
77 pr_debug("%s: %s\n", __func__, enable ? "enable" : "disable");
83 status = st->gate_ctrl(fe, 1);
85 status = st->gate_ctrl(fe, 0);
90 static struct mt2063_config az6007_mt2063_config = {
91 .tuner_address = 0x60,
95 static int __az6007_read(struct usb_device *udev, u8 req, u16 value,
96 u16 index, u8 *b, int blen)
100 ret = usb_control_msg(udev,
101 usb_rcvctrlpipe(udev, 0),
103 USB_TYPE_VENDOR | USB_DIR_IN,
104 value, index, b, blen, 5000);
106 pr_warn("usb read operation failed. (%d)\n", ret);
110 if (az6007_xfer_debug) {
111 printk(KERN_DEBUG "az6007: IN req: %02x, value: %04x, index: %04x\n",
113 print_hex_dump_bytes("az6007: payload: ",
114 DUMP_PREFIX_NONE, b, blen);
120 static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value,
121 u16 index, u8 *b, int blen)
123 struct az6007_device_state *st = d->priv;
126 if (mutex_lock_interruptible(&st->mutex) < 0)
129 ret = __az6007_read(d->udev, req, value, index, b, blen);
131 mutex_unlock(&st->mutex);
136 static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
137 u16 index, u8 *b, int blen)
141 if (az6007_xfer_debug) {
142 printk(KERN_DEBUG "az6007: OUT req: %02x, value: %04x, index: %04x\n",
144 print_hex_dump_bytes("az6007: payload: ",
145 DUMP_PREFIX_NONE, b, blen);
149 pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
154 ret = usb_control_msg(udev,
155 usb_sndctrlpipe(udev, 0),
157 USB_TYPE_VENDOR | USB_DIR_OUT,
158 value, index, b, blen, 5000);
160 pr_err("usb write operation failed. (%d)\n", ret);
167 static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value,
168 u16 index, u8 *b, int blen)
170 struct az6007_device_state *st = d->priv;
173 if (mutex_lock_interruptible(&st->mutex) < 0)
176 ret = __az6007_write(d->udev, req, value, index, b, blen);
178 mutex_unlock(&st->mutex);
183 static int az6007_streaming_ctrl(struct dvb_frontend *fe, int onoff)
185 struct dvb_usb_device *d = fe_to_d(fe);
187 pr_debug("%s: %s\n", __func__, onoff ? "enable" : "disable");
189 return az6007_write(d, 0xbc, onoff, 0, NULL, 0);
192 #if IS_ENABLED(CONFIG_RC_CORE)
193 /* remote control stuff (does not work with my box) */
194 static int az6007_rc_query(struct dvb_usb_device *d)
196 struct az6007_device_state *st = d_to_priv(d);
199 az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
201 if (st->data[1] == 0x44)
204 if ((st->data[1] ^ st->data[2]) == 0xff)
207 code = st->data[1] << 8 | st->data[2];
209 if ((st->data[3] ^ st->data[4]) == 0xff)
210 code = code << 8 | st->data[3];
212 code = code << 16 | st->data[3] << 8 | st->data[4];
214 rc_keydown(d->rc_dev, code, st->data[5]);
219 static int az6007_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
221 pr_debug("Getting az6007 Remote Control properties\n");
223 rc->allowed_protos = RC_BIT_NEC;
224 rc->query = az6007_rc_query;
230 #define az6007_get_rc_config NULL
233 static int az6007_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
237 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
238 struct az6007_device_state *state = d_to_priv(d);
250 b = kmalloc(12, GFP_KERNEL);
254 mutex_lock(&state->ca_mutex);
261 ret = az6007_read(d, req, value, index, b, blen);
263 pr_warn("usb in operation failed. (%d)\n", ret);
269 mutex_unlock(&state->ca_mutex);
274 static int az6007_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
279 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
280 struct az6007_device_state *state = d_to_priv(d);
288 pr_debug("%s(), slot %d\n", __func__, slot);
292 mutex_lock(&state->ca_mutex);
298 ret = az6007_write(d, req, value1, index, NULL, blen);
300 pr_warn("usb out operation failed. (%d)\n", ret);
302 mutex_unlock(&state->ca_mutex);
306 static int az6007_ci_read_cam_control(struct dvb_ca_en50221 *ca,
310 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
311 struct az6007_device_state *state = d_to_priv(d);
323 b = kmalloc(12, GFP_KERNEL);
327 mutex_lock(&state->ca_mutex);
334 ret = az6007_read(d, req, value, index, b, blen);
336 pr_warn("usb in operation failed. (%d)\n", ret);
340 pr_warn("Read CI IO error\n");
343 pr_debug("read cam data = %x from 0x%x\n", b[1], value);
346 mutex_unlock(&state->ca_mutex);
351 static int az6007_ci_write_cam_control(struct dvb_ca_en50221 *ca,
356 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
357 struct az6007_device_state *state = d_to_priv(d);
368 mutex_lock(&state->ca_mutex);
374 ret = az6007_write(d, req, value1, index, NULL, blen);
376 pr_warn("usb out operation failed. (%d)\n", ret);
381 mutex_unlock(&state->ca_mutex);
385 static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot)
387 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
396 b = kmalloc(12, GFP_KERNEL);
405 ret = az6007_read(d, req, value, index, b, blen);
407 pr_warn("usb in operation failed. (%d)\n", ret);
416 static int az6007_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
418 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
419 struct az6007_device_state *state = d_to_priv(d);
427 mutex_lock(&state->ca_mutex);
434 ret = az6007_write(d, req, value, index, NULL, blen);
436 pr_warn("usb out operation failed. (%d)\n", ret);
446 ret = az6007_write(d, req, value, index, NULL, blen);
448 pr_warn("usb out operation failed. (%d)\n", ret);
452 for (i = 0; i < 15; i++) {
455 if (CI_CamReady(ca, slot)) {
456 pr_debug("CAM Ready\n");
463 mutex_unlock(&state->ca_mutex);
467 static int az6007_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
472 static int az6007_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
474 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
475 struct az6007_device_state *state = d_to_priv(d);
483 pr_debug("%s()\n", __func__);
484 mutex_lock(&state->ca_mutex);
490 ret = az6007_write(d, req, value, index, NULL, blen);
492 pr_warn("usb out operation failed. (%d)\n", ret);
497 mutex_unlock(&state->ca_mutex);
501 static int az6007_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
503 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
504 struct az6007_device_state *state = d_to_priv(d);
512 b = kmalloc(12, GFP_KERNEL);
515 mutex_lock(&state->ca_mutex);
522 ret = az6007_read(d, req, value, index, b, blen);
524 pr_warn("usb in operation failed. (%d)\n", ret);
529 if (!ret && b[0] == 1) {
530 ret = DVB_CA_EN50221_POLL_CAM_PRESENT |
531 DVB_CA_EN50221_POLL_CAM_READY;
534 mutex_unlock(&state->ca_mutex);
540 static void az6007_ci_uninit(struct dvb_usb_device *d)
542 struct az6007_device_state *state;
544 pr_debug("%s()\n", __func__);
549 state = d_to_priv(d);
553 if (NULL == state->ca.data)
556 dvb_ca_en50221_release(&state->ca);
558 memset(&state->ca, 0, sizeof(state->ca));
562 static int az6007_ci_init(struct dvb_usb_adapter *adap)
564 struct dvb_usb_device *d = adap_to_d(adap);
565 struct az6007_device_state *state = adap_to_priv(adap);
568 pr_debug("%s()\n", __func__);
570 mutex_init(&state->ca_mutex);
571 state->ca.owner = THIS_MODULE;
572 state->ca.read_attribute_mem = az6007_ci_read_attribute_mem;
573 state->ca.write_attribute_mem = az6007_ci_write_attribute_mem;
574 state->ca.read_cam_control = az6007_ci_read_cam_control;
575 state->ca.write_cam_control = az6007_ci_write_cam_control;
576 state->ca.slot_reset = az6007_ci_slot_reset;
577 state->ca.slot_shutdown = az6007_ci_slot_shutdown;
578 state->ca.slot_ts_enable = az6007_ci_slot_ts_enable;
579 state->ca.poll_slot_status = az6007_ci_poll_slot_status;
582 ret = dvb_ca_en50221_init(&adap->dvb_adap,
587 pr_err("Cannot initialize CI: Error %d.\n", ret);
588 memset(&state->ca, 0, sizeof(state->ca));
592 pr_debug("CI initialized.\n");
597 static int az6007_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6])
599 struct dvb_usb_device *d = adap_to_d(adap);
600 struct az6007_device_state *st = adap_to_priv(adap);
603 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
604 memcpy(mac, st->data, 6);
607 pr_debug("%s: mac is %pM\n", __func__, mac);
612 static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
614 struct az6007_device_state *st = adap_to_priv(adap);
615 struct dvb_usb_device *d = adap_to_d(adap);
617 pr_debug("attaching demod drxk\n");
619 adap->fe[0] = dvb_attach(drxk_attach, &terratec_h7_drxk,
624 adap->fe[0]->sec_priv = adap;
625 st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
626 adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
628 az6007_ci_init(adap);
633 static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
635 struct dvb_usb_device *d = adap_to_d(adap);
637 pr_debug("attaching tuner mt2063\n");
639 /* Attach mt2063 to DVB-C frontend */
640 if (adap->fe[0]->ops.i2c_gate_ctrl)
641 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 1);
642 if (!dvb_attach(mt2063_attach, adap->fe[0],
643 &az6007_mt2063_config,
647 if (adap->fe[0]->ops.i2c_gate_ctrl)
648 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 0);
653 static int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
655 struct az6007_device_state *state = d_to_priv(d);
658 pr_debug("%s()\n", __func__);
661 mutex_init(&state->mutex);
663 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
667 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
671 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
675 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
680 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
684 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
688 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
700 az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
701 az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
707 static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
710 struct dvb_usb_device *d = i2c_get_adapdata(adap);
711 struct az6007_device_state *st = d_to_priv(d);
719 if (mutex_lock_interruptible(&st->mutex) < 0)
722 for (i = 0; i < num; i++) {
723 addr = msgs[i].addr << 1;
725 && (msgs[i].len == 1)
726 && ((msgs[i].flags & I2C_M_RD) != I2C_M_RD)
727 && (msgs[i + 1].flags & I2C_M_RD)
728 && (msgs[i].addr == msgs[i + 1].addr)) {
730 * A write + read xfer for the same address, where
731 * the first xfer has just 1 byte length.
732 * Need to join both into one operation
734 if (az6007_xfer_debug)
735 printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n",
736 addr, msgs[i].len, msgs[i + 1].len);
738 index = msgs[i].buf[0];
739 value = addr | (1 << 8);
740 length = 6 + msgs[i + 1].len;
741 len = msgs[i + 1].len;
742 ret = __az6007_read(d->udev, req, value, index,
745 for (j = 0; j < len; j++)
746 msgs[i + 1].buf[j] = st->data[j + 5];
750 } else if (!(msgs[i].flags & I2C_M_RD)) {
752 if (az6007_xfer_debug)
753 printk(KERN_DEBUG "az6007: I2C W addr=0x%x len=%d\n",
756 index = msgs[i].buf[0];
757 value = addr | (1 << 8);
758 length = msgs[i].len - 1;
759 len = msgs[i].len - 1;
760 for (j = 0; j < len; j++)
761 st->data[j] = msgs[i].buf[j + 1];
762 ret = __az6007_write(d->udev, req, value, index,
766 if (az6007_xfer_debug)
767 printk(KERN_DEBUG "az6007: I2C R addr=0x%x len=%d\n",
770 index = msgs[i].buf[0];
772 length = msgs[i].len + 6;
774 ret = __az6007_read(d->udev, req, value, index,
776 for (j = 0; j < len; j++)
777 msgs[i].buf[j] = st->data[j + 5];
783 mutex_unlock(&st->mutex);
786 pr_info("%s ERROR: %i\n", __func__, ret);
792 static u32 az6007_i2c_func(struct i2c_adapter *adapter)
797 static struct i2c_algorithm az6007_i2c_algo = {
798 .master_xfer = az6007_i2c_xfer,
799 .functionality = az6007_i2c_func,
802 static int az6007_identify_state(struct dvb_usb_device *d, const char **name)
807 pr_debug("Identifying az6007 state\n");
809 mac = kmalloc(6, GFP_ATOMIC);
813 /* Try to read the mac address */
814 ret = __az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
823 __az6007_write(d->udev, 0x09, 1, 0, NULL, 0);
824 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
825 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
828 pr_debug("Device is on %s state\n",
829 ret == WARM ? "warm" : "cold");
833 static void az6007_usb_disconnect(struct usb_interface *intf)
835 struct dvb_usb_device *d = usb_get_intfdata(intf);
837 dvb_usbv2_disconnect(intf);
840 static int az6007_download_firmware(struct dvb_usb_device *d,
841 const struct firmware *fw)
843 pr_debug("Loading az6007 firmware\n");
845 return cypress_load_firmware(d->udev, fw, CYPRESS_FX2);
848 /* DVB USB Driver stuff */
849 static struct dvb_usb_device_properties az6007_props = {
850 .driver_name = KBUILD_MODNAME,
851 .owner = THIS_MODULE,
852 .firmware = AZ6007_FIRMWARE,
854 .adapter_nr = adapter_nr,
855 .size_of_priv = sizeof(struct az6007_device_state),
856 .i2c_algo = &az6007_i2c_algo,
857 .tuner_attach = az6007_tuner_attach,
858 .frontend_attach = az6007_frontend_attach,
859 .streaming_ctrl = az6007_streaming_ctrl,
860 .get_rc_config = az6007_get_rc_config,
861 .read_mac_address = az6007_read_mac_addr,
862 .download_firmware = az6007_download_firmware,
863 .identify_state = az6007_identify_state,
864 .power_ctrl = az6007_power_ctrl,
867 { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), }
871 static struct usb_device_id az6007_usb_table[] = {
872 {DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007,
873 &az6007_props, "Azurewave 6007", RC_MAP_EMPTY)},
874 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7,
875 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
876 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2,
877 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
881 MODULE_DEVICE_TABLE(usb, az6007_usb_table);
883 static int az6007_suspend(struct usb_interface *intf, pm_message_t msg)
885 struct dvb_usb_device *d = usb_get_intfdata(intf);
888 return dvb_usbv2_suspend(intf, msg);
891 static int az6007_resume(struct usb_interface *intf)
893 struct dvb_usb_device *d = usb_get_intfdata(intf);
894 struct dvb_usb_adapter *adap = &d->adapter[0];
896 az6007_ci_init(adap);
897 return dvb_usbv2_resume(intf);
900 /* usb specific object needed to register this driver with the usb subsystem */
901 static struct usb_driver az6007_usb_driver = {
902 .name = KBUILD_MODNAME,
903 .id_table = az6007_usb_table,
904 .probe = dvb_usbv2_probe,
905 .disconnect = az6007_usb_disconnect,
909 * FIXME: need to implement reset_resume, likely with
910 * dvb-usb-v2 core support
912 .suspend = az6007_suspend,
913 .resume = az6007_resume,
916 module_usb_driver(az6007_usb_driver);
918 MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
919 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
920 MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
921 MODULE_VERSION("2.0");
922 MODULE_LICENSE("GPL");
923 MODULE_FIRMWARE(AZ6007_FIRMWARE);