1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for AzureWave 6007 DVB-C/T USB2.0 and clones
5 * Copyright (c) Henry Wang <Henry.wang@AzureWave.com>
7 * This driver was made publicly available by Terratec, at:
8 * http://linux.terratec.de/files/TERRATEC_H7/20110323_TERRATEC_H7_Linux.tar.gz
9 * The original driver's license is GPL, as declared with MODULE_LICENSE()
11 * Copyright (c) 2010-2012 Mauro Carvalho Chehab
12 * Driver modified by in order to work with upstream drxk driver, and
13 * tons of bugs got fixed, and converted to use dvb-usb-v2.
18 #include <media/dvb_ca_en50221.h>
20 #include "cypress_firmware.h"
22 #define AZ6007_FIRMWARE "/*(DEBLOBBED)*/"
24 static int az6007_xfer_debug;
25 module_param_named(xfer_debug, az6007_xfer_debug, int, 0644);
26 MODULE_PARM_DESC(xfer_debug, "Enable xfer debug");
28 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
30 /* Known requests (Cypress FX2 firmware + az6007 "private" ones*/
33 #define AZ6007_READ_DATA 0xb7
34 #define AZ6007_I2C_RD 0xb9
35 #define AZ6007_POWER 0xbc
36 #define AZ6007_I2C_WR 0xbd
37 #define FX2_SCON1 0xc0
38 #define AZ6007_TS_THROUGH 0xc7
39 #define AZ6007_READ_IR 0xb4
41 struct az6007_device_state {
43 struct mutex ca_mutex;
44 struct dvb_ca_en50221 ca;
46 int (*gate_ctrl) (struct dvb_frontend *, int);
47 unsigned char data[4096];
50 static struct drxk_config terratec_h7_drxk = {
54 .single_master = true,
55 .enable_merr_cfg = true,
56 .no_i2c_bridge = false,
58 .mpeg_out_clk_strength = 0x02,
59 .qam_demod_parameter_count = 2,
60 .microcode_name = "/*(DEBLOBBED)*/",
63 static struct drxk_config cablestar_hdci_drxk = {
67 .single_master = true,
68 .enable_merr_cfg = true,
69 .no_i2c_bridge = false,
71 .mpeg_out_clk_strength = 0x02,
72 .qam_demod_parameter_count = 2,
73 .microcode_name = "/*(DEBLOBBED)*/",
76 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
78 struct az6007_device_state *st = fe_to_priv(fe);
79 struct dvb_usb_adapter *adap = fe->sec_priv;
82 pr_debug("%s: %s\n", __func__, enable ? "enable" : "disable");
88 status = st->gate_ctrl(fe, 1);
90 status = st->gate_ctrl(fe, 0);
95 static struct mt2063_config az6007_mt2063_config = {
96 .tuner_address = 0x60,
100 static int __az6007_read(struct usb_device *udev, u8 req, u16 value,
101 u16 index, u8 *b, int blen)
105 ret = usb_control_msg(udev,
106 usb_rcvctrlpipe(udev, 0),
108 USB_TYPE_VENDOR | USB_DIR_IN,
109 value, index, b, blen, 5000);
111 pr_warn("usb read operation failed. (%d)\n", ret);
115 if (az6007_xfer_debug) {
116 printk(KERN_DEBUG "az6007: IN req: %02x, value: %04x, index: %04x\n",
118 print_hex_dump_bytes("az6007: payload: ",
119 DUMP_PREFIX_NONE, b, blen);
125 static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value,
126 u16 index, u8 *b, int blen)
128 struct az6007_device_state *st = d->priv;
131 if (mutex_lock_interruptible(&st->mutex) < 0)
134 ret = __az6007_read(d->udev, req, value, index, b, blen);
136 mutex_unlock(&st->mutex);
141 static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
142 u16 index, u8 *b, int blen)
146 if (az6007_xfer_debug) {
147 printk(KERN_DEBUG "az6007: OUT req: %02x, value: %04x, index: %04x\n",
149 print_hex_dump_bytes("az6007: payload: ",
150 DUMP_PREFIX_NONE, b, blen);
154 pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
159 ret = usb_control_msg(udev,
160 usb_sndctrlpipe(udev, 0),
162 USB_TYPE_VENDOR | USB_DIR_OUT,
163 value, index, b, blen, 5000);
165 pr_err("usb write operation failed. (%d)\n", ret);
172 static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value,
173 u16 index, u8 *b, int blen)
175 struct az6007_device_state *st = d->priv;
178 if (mutex_lock_interruptible(&st->mutex) < 0)
181 ret = __az6007_write(d->udev, req, value, index, b, blen);
183 mutex_unlock(&st->mutex);
188 static int az6007_streaming_ctrl(struct dvb_frontend *fe, int onoff)
190 struct dvb_usb_device *d = fe_to_d(fe);
192 pr_debug("%s: %s\n", __func__, onoff ? "enable" : "disable");
194 return az6007_write(d, 0xbc, onoff, 0, NULL, 0);
197 #if IS_ENABLED(CONFIG_RC_CORE)
198 /* remote control stuff (does not work with my box) */
199 static int az6007_rc_query(struct dvb_usb_device *d)
201 struct az6007_device_state *st = d_to_priv(d);
205 if (az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10) < 0)
208 if (st->data[1] == 0x44)
211 if ((st->data[3] ^ st->data[4]) == 0xff) {
212 if ((st->data[1] ^ st->data[2]) == 0xff) {
213 code = RC_SCANCODE_NEC(st->data[1], st->data[3]);
214 proto = RC_PROTO_NEC;
216 code = RC_SCANCODE_NECX(st->data[1] << 8 | st->data[2],
218 proto = RC_PROTO_NECX;
221 code = RC_SCANCODE_NEC32(st->data[1] << 24 |
225 proto = RC_PROTO_NEC32;
228 rc_keydown(d->rc_dev, proto, code, st->data[5]);
233 static int az6007_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
235 pr_debug("Getting az6007 Remote Control properties\n");
237 rc->allowed_protos = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
239 rc->query = az6007_rc_query;
245 #define az6007_get_rc_config NULL
248 static int az6007_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
252 struct dvb_usb_device *d = ca->data;
253 struct az6007_device_state *state = d_to_priv(d);
265 b = kmalloc(12, GFP_KERNEL);
269 mutex_lock(&state->ca_mutex);
276 ret = az6007_read(d, req, value, index, b, blen);
278 pr_warn("usb in operation failed. (%d)\n", ret);
284 mutex_unlock(&state->ca_mutex);
289 static int az6007_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
294 struct dvb_usb_device *d = ca->data;
295 struct az6007_device_state *state = d_to_priv(d);
303 pr_debug("%s(), slot %d\n", __func__, slot);
307 mutex_lock(&state->ca_mutex);
313 ret = az6007_write(d, req, value1, index, NULL, blen);
315 pr_warn("usb out operation failed. (%d)\n", ret);
317 mutex_unlock(&state->ca_mutex);
321 static int az6007_ci_read_cam_control(struct dvb_ca_en50221 *ca,
325 struct dvb_usb_device *d = ca->data;
326 struct az6007_device_state *state = d_to_priv(d);
338 b = kmalloc(12, GFP_KERNEL);
342 mutex_lock(&state->ca_mutex);
349 ret = az6007_read(d, req, value, index, b, blen);
351 pr_warn("usb in operation failed. (%d)\n", ret);
355 pr_warn("Read CI IO error\n");
358 pr_debug("read cam data = %x from 0x%x\n", b[1], value);
361 mutex_unlock(&state->ca_mutex);
366 static int az6007_ci_write_cam_control(struct dvb_ca_en50221 *ca,
371 struct dvb_usb_device *d = ca->data;
372 struct az6007_device_state *state = d_to_priv(d);
383 mutex_lock(&state->ca_mutex);
389 ret = az6007_write(d, req, value1, index, NULL, blen);
391 pr_warn("usb out operation failed. (%d)\n", ret);
396 mutex_unlock(&state->ca_mutex);
400 static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot)
402 struct dvb_usb_device *d = ca->data;
411 b = kmalloc(12, GFP_KERNEL);
420 ret = az6007_read(d, req, value, index, b, blen);
422 pr_warn("usb in operation failed. (%d)\n", ret);
431 static int az6007_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
433 struct dvb_usb_device *d = ca->data;
434 struct az6007_device_state *state = d_to_priv(d);
442 mutex_lock(&state->ca_mutex);
449 ret = az6007_write(d, req, value, index, NULL, blen);
451 pr_warn("usb out operation failed. (%d)\n", ret);
461 ret = az6007_write(d, req, value, index, NULL, blen);
463 pr_warn("usb out operation failed. (%d)\n", ret);
467 for (i = 0; i < 15; i++) {
470 if (CI_CamReady(ca, slot)) {
471 pr_debug("CAM Ready\n");
478 mutex_unlock(&state->ca_mutex);
482 static int az6007_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
487 static int az6007_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
489 struct dvb_usb_device *d = ca->data;
490 struct az6007_device_state *state = d_to_priv(d);
498 pr_debug("%s()\n", __func__);
499 mutex_lock(&state->ca_mutex);
505 ret = az6007_write(d, req, value, index, NULL, blen);
507 pr_warn("usb out operation failed. (%d)\n", ret);
512 mutex_unlock(&state->ca_mutex);
516 static int az6007_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
518 struct dvb_usb_device *d = ca->data;
519 struct az6007_device_state *state = d_to_priv(d);
527 b = kmalloc(12, GFP_KERNEL);
530 mutex_lock(&state->ca_mutex);
537 ret = az6007_read(d, req, value, index, b, blen);
539 pr_warn("usb in operation failed. (%d)\n", ret);
544 if (!ret && b[0] == 1) {
545 ret = DVB_CA_EN50221_POLL_CAM_PRESENT |
546 DVB_CA_EN50221_POLL_CAM_READY;
549 mutex_unlock(&state->ca_mutex);
555 static void az6007_ci_uninit(struct dvb_usb_device *d)
557 struct az6007_device_state *state;
559 pr_debug("%s()\n", __func__);
564 state = d_to_priv(d);
568 if (NULL == state->ca.data)
571 dvb_ca_en50221_release(&state->ca);
573 memset(&state->ca, 0, sizeof(state->ca));
577 static int az6007_ci_init(struct dvb_usb_adapter *adap)
579 struct dvb_usb_device *d = adap_to_d(adap);
580 struct az6007_device_state *state = adap_to_priv(adap);
583 pr_debug("%s()\n", __func__);
585 mutex_init(&state->ca_mutex);
586 state->ca.owner = THIS_MODULE;
587 state->ca.read_attribute_mem = az6007_ci_read_attribute_mem;
588 state->ca.write_attribute_mem = az6007_ci_write_attribute_mem;
589 state->ca.read_cam_control = az6007_ci_read_cam_control;
590 state->ca.write_cam_control = az6007_ci_write_cam_control;
591 state->ca.slot_reset = az6007_ci_slot_reset;
592 state->ca.slot_shutdown = az6007_ci_slot_shutdown;
593 state->ca.slot_ts_enable = az6007_ci_slot_ts_enable;
594 state->ca.poll_slot_status = az6007_ci_poll_slot_status;
597 ret = dvb_ca_en50221_init(&adap->dvb_adap,
602 pr_err("Cannot initialize CI: Error %d.\n", ret);
603 memset(&state->ca, 0, sizeof(state->ca));
607 pr_debug("CI initialized.\n");
612 static int az6007_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6])
614 struct dvb_usb_device *d = adap_to_d(adap);
615 struct az6007_device_state *st = adap_to_priv(adap);
618 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
619 memcpy(mac, st->data, 6);
622 pr_debug("%s: mac is %pM\n", __func__, mac);
627 static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
629 struct az6007_device_state *st = adap_to_priv(adap);
630 struct dvb_usb_device *d = adap_to_d(adap);
632 pr_debug("attaching demod drxk\n");
634 adap->fe[0] = dvb_attach(drxk_attach, &terratec_h7_drxk,
639 adap->fe[0]->sec_priv = adap;
640 st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
641 adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
643 az6007_ci_init(adap);
648 static int az6007_cablestar_hdci_frontend_attach(struct dvb_usb_adapter *adap)
650 struct az6007_device_state *st = adap_to_priv(adap);
651 struct dvb_usb_device *d = adap_to_d(adap);
653 pr_debug("attaching demod drxk\n");
655 adap->fe[0] = dvb_attach(drxk_attach, &cablestar_hdci_drxk,
660 adap->fe[0]->sec_priv = adap;
661 st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
662 adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
664 az6007_ci_init(adap);
669 static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
671 struct dvb_usb_device *d = adap_to_d(adap);
673 pr_debug("attaching tuner mt2063\n");
675 /* Attach mt2063 to DVB-C frontend */
676 if (adap->fe[0]->ops.i2c_gate_ctrl)
677 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 1);
678 if (!dvb_attach(mt2063_attach, adap->fe[0],
679 &az6007_mt2063_config,
683 if (adap->fe[0]->ops.i2c_gate_ctrl)
684 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 0);
689 static int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
691 struct az6007_device_state *state = d_to_priv(d);
694 pr_debug("%s()\n", __func__);
697 mutex_init(&state->mutex);
699 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
703 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
707 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
711 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
716 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
720 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
724 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
736 az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
737 az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
743 static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
746 struct dvb_usb_device *d = i2c_get_adapdata(adap);
747 struct az6007_device_state *st = d_to_priv(d);
755 if (mutex_lock_interruptible(&st->mutex) < 0)
758 for (i = 0; i < num; i++) {
759 addr = msgs[i].addr << 1;
761 && (msgs[i].len == 1)
762 && ((msgs[i].flags & I2C_M_RD) != I2C_M_RD)
763 && (msgs[i + 1].flags & I2C_M_RD)
764 && (msgs[i].addr == msgs[i + 1].addr)) {
766 * A write + read xfer for the same address, where
767 * the first xfer has just 1 byte length.
768 * Need to join both into one operation
770 if (az6007_xfer_debug)
771 printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n",
772 addr, msgs[i].len, msgs[i + 1].len);
774 index = msgs[i].buf[0];
775 value = addr | (1 << 8);
776 length = 6 + msgs[i + 1].len;
777 len = msgs[i + 1].len;
778 ret = __az6007_read(d->udev, req, value, index,
781 for (j = 0; j < len; j++)
782 msgs[i + 1].buf[j] = st->data[j + 5];
786 } else if (!(msgs[i].flags & I2C_M_RD)) {
788 if (az6007_xfer_debug)
789 printk(KERN_DEBUG "az6007: I2C W addr=0x%x len=%d\n",
791 if (msgs[i].len < 1) {
796 index = msgs[i].buf[0];
797 value = addr | (1 << 8);
798 length = msgs[i].len - 1;
799 len = msgs[i].len - 1;
800 for (j = 0; j < len; j++)
801 st->data[j] = msgs[i].buf[j + 1];
802 ret = __az6007_write(d->udev, req, value, index,
806 if (az6007_xfer_debug)
807 printk(KERN_DEBUG "az6007: I2C R addr=0x%x len=%d\n",
809 if (msgs[i].len < 1) {
814 index = msgs[i].buf[0];
816 length = msgs[i].len + 6;
818 ret = __az6007_read(d->udev, req, value, index,
820 for (j = 0; j < len; j++)
821 msgs[i].buf[j] = st->data[j + 5];
827 mutex_unlock(&st->mutex);
830 pr_info("%s ERROR: %i\n", __func__, ret);
836 static u32 az6007_i2c_func(struct i2c_adapter *adapter)
841 static struct i2c_algorithm az6007_i2c_algo = {
842 .master_xfer = az6007_i2c_xfer,
843 .functionality = az6007_i2c_func,
846 static int az6007_identify_state(struct dvb_usb_device *d, const char **name)
851 pr_debug("Identifying az6007 state\n");
853 mac = kmalloc(6, GFP_ATOMIC);
857 /* Try to read the mac address */
858 ret = __az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
867 __az6007_write(d->udev, 0x09, 1, 0, NULL, 0);
868 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
869 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
872 pr_debug("Device is on %s state\n",
873 ret == WARM ? "warm" : "cold");
877 static void az6007_usb_disconnect(struct usb_interface *intf)
879 struct dvb_usb_device *d = usb_get_intfdata(intf);
881 dvb_usbv2_disconnect(intf);
884 static int az6007_download_firmware(struct dvb_usb_device *d,
885 const struct firmware *fw)
887 pr_debug("Loading az6007 firmware\n");
889 return cypress_load_firmware(d->udev, fw, CYPRESS_FX2);
892 /* DVB USB Driver stuff */
893 static struct dvb_usb_device_properties az6007_props = {
894 .driver_name = KBUILD_MODNAME,
895 .owner = THIS_MODULE,
896 .firmware = AZ6007_FIRMWARE,
898 .adapter_nr = adapter_nr,
899 .size_of_priv = sizeof(struct az6007_device_state),
900 .i2c_algo = &az6007_i2c_algo,
901 .tuner_attach = az6007_tuner_attach,
902 .frontend_attach = az6007_frontend_attach,
903 .streaming_ctrl = az6007_streaming_ctrl,
904 .get_rc_config = az6007_get_rc_config,
905 .read_mac_address = az6007_read_mac_addr,
906 .download_firmware = az6007_download_firmware,
907 .identify_state = az6007_identify_state,
908 .power_ctrl = az6007_power_ctrl,
911 { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), }
915 static struct dvb_usb_device_properties az6007_cablestar_hdci_props = {
916 .driver_name = KBUILD_MODNAME,
917 .owner = THIS_MODULE,
918 .firmware = AZ6007_FIRMWARE,
920 .adapter_nr = adapter_nr,
921 .size_of_priv = sizeof(struct az6007_device_state),
922 .i2c_algo = &az6007_i2c_algo,
923 .tuner_attach = az6007_tuner_attach,
924 .frontend_attach = az6007_cablestar_hdci_frontend_attach,
925 .streaming_ctrl = az6007_streaming_ctrl,
926 /* ditch get_rc_config as it can't work (TS35 remote, I believe it's rc5) */
927 .get_rc_config = NULL,
928 .read_mac_address = az6007_read_mac_addr,
929 .download_firmware = az6007_download_firmware,
930 .identify_state = az6007_identify_state,
931 .power_ctrl = az6007_power_ctrl,
934 { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), }
938 static const struct usb_device_id az6007_usb_table[] = {
939 {DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007,
940 &az6007_props, "Azurewave 6007", RC_MAP_EMPTY)},
941 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7,
942 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
943 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2,
944 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
945 {DVB_USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_CABLESTAR_HDCI,
946 &az6007_cablestar_hdci_props, "Technisat CableStar Combo HD CI", RC_MAP_EMPTY)},
950 MODULE_DEVICE_TABLE(usb, az6007_usb_table);
952 static int az6007_suspend(struct usb_interface *intf, pm_message_t msg)
954 struct dvb_usb_device *d = usb_get_intfdata(intf);
957 return dvb_usbv2_suspend(intf, msg);
960 static int az6007_resume(struct usb_interface *intf)
962 struct dvb_usb_device *d = usb_get_intfdata(intf);
963 struct dvb_usb_adapter *adap = &d->adapter[0];
965 az6007_ci_init(adap);
966 return dvb_usbv2_resume(intf);
969 /* usb specific object needed to register this driver with the usb subsystem */
970 static struct usb_driver az6007_usb_driver = {
971 .name = KBUILD_MODNAME,
972 .id_table = az6007_usb_table,
973 .probe = dvb_usbv2_probe,
974 .disconnect = az6007_usb_disconnect,
978 * FIXME: need to implement reset_resume, likely with
979 * dvb-usb-v2 core support
981 .suspend = az6007_suspend,
982 .resume = az6007_resume,
985 module_usb_driver(az6007_usb_driver);
987 MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
988 MODULE_AUTHOR("Mauro Carvalho Chehab");
989 MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
990 MODULE_VERSION("2.0");
991 MODULE_LICENSE("GPL");