blob: 81fdc90be449e7d1a5dbc933ad17c200bf0fd010 [file] [log] [blame]
Mauro Carvalho Chehab25c16462011-07-23 10:59:25 -03001/*
2 * Driver for AzureWave 6007 DVB-C/T USB2.0 and clones
3 *
4 * Copyright (c) Henry Wang <Henry.wang@AzureWave.com>
5 *
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()
9 *
10 * Driver modifiyed by Mauro Carvalho Chehab <mchehab@redhat.com> in order
11 * to work with upstream drxk driver, and to fix some bugs.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation under version 2 of the License.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030021 */
22
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030023#include "drxk.h"
24#include "mt2063.h"
25#include "dvb_ca_en50221.h"
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030026
27#define DVB_USB_LOG_PREFIX "az6007"
Mauro Carvalho Chehabc108a5a2011-07-25 10:38:20 -030028#include "dvb-usb.h"
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030029
30/* debug */
31int dvb_usb_az6007_debug;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030032module_param_named(debug, dvb_usb_az6007_debug, int, 0644);
33MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))."
34 DVB_USB_DEBUG_STATUS);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030035
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030036#define deb_info(args...) dprintk(dvb_usb_az6007_debug, 0x01, args)
37#define deb_xfer(args...) dprintk(dvb_usb_az6007_debug, 0x02, args)
38#define deb_rc(args...) dprintk(dvb_usb_az6007_debug, 0x04, args)
39#define deb_fe(args...) dprintk(dvb_usb_az6007_debug, 0x08, args)
40
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030041DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -030043/* Known requests (Cypress FX2 firmware + az6007 "private" ones*/
44
45#define FX2_OED 0xb5
46#define AZ6007_READ_DATA 0xb7
47#define AZ6007_I2C_RD 0xb9
48#define AZ6007_POWER 0xbc
49#define AZ6007_I2C_WR 0xbd
50#define FX2_SCON1 0xc0
51#define AZ6007_TS_THROUGH 0xc7
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -030052#define AZ6007_READ_IR 0xb4
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -030053
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030054struct az6007_device_state {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030055 struct dvb_ca_en50221 ca;
56 struct mutex ca_mutex;
57 u8 power_state;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030058
59 /* Due to DRX-K - probably need changes */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030060 int (*gate_ctrl) (struct dvb_frontend *, int);
61 struct semaphore pll_mutex;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -030062 bool tuner_attached;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030063};
64
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030065static struct drxk_config terratec_h7_drxk = {
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030066 .adr = 0x29,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030067 .parallel_ts = true,
68 .dynamic_clk = true,
69 .single_master = true,
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -030070 .enable_merr_cfg = true,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030071 .no_i2c_bridge = false,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -030072 .chunk_size = 64,
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -030073 .mpeg_out_clk_strength = 0x02,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030074 .microcode_name = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030075};
76
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030077static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
78{
79 struct dvb_usb_adapter *adap = fe->sec_priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030080 struct az6007_device_state *st;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030081 int status = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030082
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030083 deb_info("%s: %s\n", __func__, enable ? "enable" : "disable");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030084
85 if (!adap)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030086 return -EINVAL;
87
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -030088 st = adap->dev->priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030089
90 if (!st)
91 return -EINVAL;
92
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030093 if (enable) {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030094#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030095 down(&st->pll_mutex);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030096#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030097 status = st->gate_ctrl(fe, 1);
98 } else {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030099#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300100 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -0300101#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300102 up(&st->pll_mutex);
103 }
104 return status;
105}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300106
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300107static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300108 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300109 .refclock = 36125000,
110};
111
112/* check for mutex FIXME */
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300113static int az6007_read(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300114 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300115{
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300116 int ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300117
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300118 ret = usb_control_msg(udev,
119 usb_rcvctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300120 req,
121 USB_TYPE_VENDOR | USB_DIR_IN,
122 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300123 if (ret < 0) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300124 warn("usb read operation failed. (%d)", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300125 return -EIO;
126 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300127
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300128 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
129 index);
130 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300131
132 return ret;
133}
134
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300135static int az6007_write(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300136 u16 index, u8 *b, int blen)
137{
138 int ret;
139
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300140 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
141 index);
142 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300143
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300144 if (blen > 64) {
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300145 err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
146 blen);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300147 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300148 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300149
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300150 ret = usb_control_msg(udev,
151 usb_sndctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300152 req,
153 USB_TYPE_VENDOR | USB_DIR_OUT,
154 value, index, b, blen, 5000);
155 if (ret != blen) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300156 err("usb write operation failed. (%d)", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300157 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300158 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300159
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300160 return 0;
161}
162
163static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
164{
165 return 0;
166}
167
168/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300169static struct rc_map_table rc_map_az6007_table[] = {
170 {0x0001, KEY_1},
171 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300172};
173
174/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300175static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300176{
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300177 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300178 u8 key[10];
179 int i;
180
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300181 /*
182 * FIXME: remove the following return to enabled remote querying
183 * The driver likely needs proper locking to avoid troubles between
184 * this call and other concurrent calls.
185 */
186 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300187
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300188 az6007_read(d->udev, AZ6007_READ_IR, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300189
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300190 if (key[1] == 0x44) {
191 *state = REMOTE_NO_KEY_PRESSED;
192 return 0;
193 }
194
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300195 /*
196 * FIXME: need to make something useful with the keycodes and to
197 * convert it to the non-legacy mode. Yet, it is producing some
198 * debug info already, like:
199 * 88 04 eb 02 fd ff 00 82 63 82 (terratec IR)
200 * 88 04 eb 03 fc 00 00 82 63 82 (terratec IR)
201 * 88 80 7e 0d f2 ff 00 82 63 82 (another NEC-extended based IR)
202 * I suspect that the IR data is at bytes 1 to 4, and byte 5 is parity
203 */
204 deb_rc("remote query key: %x %d\n", key[1], key[1]);
205 print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, key, 10);
206
207 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
208 if (rc5_custom(&keymap[i]) == key[1]) {
209 *event = keymap[i].keycode;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300210 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300211
212 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300213 }
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300214 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300215 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300216}
217
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300218#if 0
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300219int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
220{
221 u8 v = onoff;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300222 return az6007_write(d->udev, AZ6007_POWER, v , 3, NULL, 1);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300223}
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300224#endif
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300225
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300226static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300227{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300228 int ret;
229 ret = az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300230
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300231 if (ret > 0)
232 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
233 __func__, mac[0], mac[1], mac[2],
234 mac[3], mac[4], mac[5]);
235
236 return ret;
237}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300238
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300239static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
240{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300241 int ret;
242 struct usb_device *udev = adap->dev->udev;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300243
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300244 deb_info("%s: adap=%p adap->dev=%p\n", __func__, adap, adap->dev);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300245
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300246 ret = az6007_write(udev, AZ6007_POWER, 0, 2, NULL, 0);
247 if (ret < 0)
248 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300249 msleep(150);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300250 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
251 if (ret < 0)
252 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300253 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300254 ret = az6007_write(udev, AZ6007_POWER, 1, 3, NULL, 0);
255 if (ret < 0)
256 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300257 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300258 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
259 if (ret < 0)
260 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300261 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300262 ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
263 if (ret < 0)
264 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300265 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300266 ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
267 if (ret < 0)
268 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300269 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300270 ret = az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300271
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300272error:
273 if (ret < 0)
274 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300275
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300276 return ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300277}
278
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300279static int az6007_led_on_off(struct usb_interface *intf, int onoff)
280{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300281 struct usb_device *udev = interface_to_usbdev(intf);
282 int ret;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300283 /* TS through */
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300284 ret = az6007_write(udev, AZ6007_POWER, onoff, 0, NULL, 0);
285 if (ret < 0)
286 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300287 return ret;
288}
289
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300290static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
291{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300292 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300293
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300294 BUG_ON(!st);
295
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300296 az6007_frontend_poweron(adap);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300297
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300298 info("attaching demod drxk");
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300299 adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
300 &adap->dev->i2c_adap);
301 if (!adap->fe_adap[0].fe)
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300302 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300303
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300304 adap->fe_adap[0].fe->sec_priv = adap;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300305 /* FIXME: do we need a pll semaphore? */
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300306 sema_init(&st->pll_mutex, 1);
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300307 st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
308 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300309
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300310 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300311}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300312
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300313static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
314{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300315 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300316
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300317 if (st->tuner_attached)
318 return 0;
319
320 st->tuner_attached = true;
321
322 info("attaching tuner mt2063");
323 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300324 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
325 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
326 if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
327 &az6007_mt2063_config,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300328 &adap->dev->i2c_adap))
329 return -EINVAL;
330
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300331 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
332 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300333
334 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300335}
336
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300337int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
338{
339 if (!onoff)
340 return 0;
341
342
343 info("Sending poweron sequence");
344
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300345 az6007_write(d->udev, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300346
347#if 0
348 // Seems to be a poweroff sequence
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300349 az6007_write(d->udev, 0xbc, 1, 3, NULL, 0);
350 az6007_write(d->udev, 0xbc, 1, 4, NULL, 0);
351 az6007_write(d->udev, 0xc0, 0, 3, NULL, 0);
352 az6007_write(d->udev, 0xc0, 1, 3, NULL, 0);
353 az6007_write(d->udev, 0xbc, 0, 1, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300354#endif
355 return 0;
356}
357
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300358static struct dvb_usb_device_properties az6007_properties;
359
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300360static void az6007_usb_disconnect(struct usb_interface *intf)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300361{
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300362 dvb_usb_device_exit(intf);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300363}
364
365/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300366static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
367 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300368{
369 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300370 int i, j, len;
371 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300372 u16 index;
373 u16 value;
374 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300375 u8 req, addr;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300376 u8 data[512];
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300377
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300378 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
379 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300380
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300381 for (i = 0; i < num; i++) {
382 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300383 if (((i + 1) < num)
384 && (msgs[i].len == 1)
385 && (!msgs[i].flags & I2C_M_RD)
386 && (msgs[i + 1].flags & I2C_M_RD)
387 && (msgs[i].addr == msgs[i + 1].addr)) {
388 /*
389 * A write + read xfer for the same address, where
390 * the first xfer has just 1 byte length.
391 * Need to join both into one operation
392 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300393 if (dvb_usb_az6007_debug & 2)
394 printk(KERN_DEBUG
395 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
396 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300397 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300398 index = msgs[i].buf[0];
399 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300400 length = 6 + msgs[i + 1].len;
401 len = msgs[i + 1].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300402 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300403 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300404 if (ret >= len) {
405 for (j = 0; j < len; j++) {
406 msgs[i + 1].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300407 if (dvb_usb_az6007_debug & 2)
408 printk(KERN_CONT
409 "0x%02x ",
410 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300411 }
412 } else
413 ret = -EIO;
414 i++;
415 } else if (!(msgs[i].flags & I2C_M_RD)) {
416 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300417 if (dvb_usb_az6007_debug & 2)
418 printk(KERN_DEBUG
419 "az6007 I2C xfer write addr=0x%x len=%d: ",
420 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300421 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300422 index = msgs[i].buf[0];
423 value = addr | (1 << 8);
424 length = msgs[i].len - 1;
425 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300426 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300427 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
428 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300429 data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300430 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300431 printk(KERN_CONT "0x%02x ", data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300432 }
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300433 ret = az6007_write(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300434 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300435 } else {
436 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300437 if (dvb_usb_az6007_debug & 2)
438 printk(KERN_DEBUG
439 "az6007 I2C xfer read addr=0x%x len=%d: ",
440 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300441 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300442 index = msgs[i].buf[0];
443 value = addr;
444 length = msgs[i].len + 6;
445 len = msgs[i].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300446 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300447 length);
448 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300449 msgs[i].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300450 if (dvb_usb_az6007_debug & 2)
451 printk(KERN_CONT
452 "0x%02x ", data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300453 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300454 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300455 if (dvb_usb_az6007_debug & 2)
456 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300457 if (ret < 0)
458 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300459 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300460err:
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300461 mutex_unlock(&d->i2c_mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300462
463 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300464 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300465 return ret;
466 }
467 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300468}
469
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300470static u32 az6007_i2c_func(struct i2c_adapter *adapter)
471{
472 return I2C_FUNC_I2C;
473}
474
475static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300476 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300477 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300478};
479
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300480int az6007_identify_state(struct usb_device *udev,
481 struct dvb_usb_device_properties *props,
482 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300483{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300484 int ret;
485 u8 mac[6];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300486
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300487 /* Try to read the mac address */
488 ret = az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
489 if (ret == 6)
490 *cold = 0;
491 else
492 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300493
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300494 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300495 return 0;
496}
497
498static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300499 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300500{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300501 az6007_led_on_off(intf, 0);
502
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300503 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300504 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300505}
506
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300507static struct usb_device_id az6007_usb_table[] = {
508 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
509 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
510 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300511};
512
513MODULE_DEVICE_TABLE(usb, az6007_usb_table);
514
515static struct dvb_usb_device_properties az6007_properties = {
516 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
517 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300518 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300519 .no_reconnect = 1,
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300520 .size_of_priv = sizeof(struct az6007_device_state),
521 .identify_state = az6007_identify_state,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300522 .num_adapters = 1,
523 .adapter = {
524 {
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300525 .num_frontends = 1,
526 .fe = {{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300527 .streaming_ctrl = az6007_streaming_ctrl,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300528 .tuner_attach = az6007_tuner_attach,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300529 .frontend_attach = az6007_frontend_attach,
530
531 /* parameter for the MPEG2-data transfer */
532 .stream = {
533 .type = USB_BULK,
534 .count = 10,
535 .endpoint = 0x02,
536 .u = {
537 .bulk = {
538 .buffersize = 4096,
539 }
540 }
541 },
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300542 }}
543 } },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300544 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300545 .read_mac_address = az6007_read_mac_addr,
546
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300547 .rc.legacy = {
548 .rc_map_table = rc_map_az6007_table,
549 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
550 .rc_interval = 400,
551 .rc_query = az6007_rc_query,
552 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300553 .i2c_algo = &az6007_i2c_algo,
554
555 .num_device_descs = 2,
556 .devices = {
557 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
558 .cold_ids = { &az6007_usb_table[0], NULL },
559 .warm_ids = { NULL },
560 },
561 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
562 .cold_ids = { &az6007_usb_table[1], NULL },
563 .warm_ids = { NULL },
564 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300565 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300566 }
567};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300568
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300569/* usb specific object needed to register this driver with the usb subsystem */
570static struct usb_driver az6007_usb_driver = {
571 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300572 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300573 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300574 /* .disconnect = az6007_usb_disconnect, */
575 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300576};
577
578/* module stuff */
579static int __init az6007_usb_module_init(void)
580{
581 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300582 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300583
584 result = usb_register(&az6007_usb_driver);
585 if (result) {
586 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300587 return result;
588 }
589
590 return 0;
591}
592
593static void __exit az6007_usb_module_exit(void)
594{
595 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300596 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300597 usb_deregister(&az6007_usb_driver);
598}
599
600module_init(az6007_usb_module_init);
601module_exit(az6007_usb_module_exit);
602
603MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
604MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300605MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300606MODULE_LICENSE("GPL");