blob: 142ef7b0c02eb5469e0f526d767d27f9045b41ef [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 *
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030010 * Copyright (c) 2010-2011 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.
Mauro Carvalho Chehab25c16462011-07-23 10:59:25 -030013 *
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.
17 *
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.
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030022 */
23
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030024#include "drxk.h"
25#include "mt2063.h"
26#include "dvb_ca_en50221.h"
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030027
28#define DVB_USB_LOG_PREFIX "az6007"
Mauro Carvalho Chehabc108a5a2011-07-25 10:38:20 -030029#include "dvb-usb.h"
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030030
31/* debug */
32int dvb_usb_az6007_debug;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030033module_param_named(debug, dvb_usb_az6007_debug, int, 0644);
34MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))."
35 DVB_USB_DEBUG_STATUS);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030036
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030037#define deb_info(args...) dprintk(dvb_usb_az6007_debug, 0x01, args)
38#define deb_xfer(args...) dprintk(dvb_usb_az6007_debug, 0x02, args)
39#define deb_rc(args...) dprintk(dvb_usb_az6007_debug, 0x04, args)
40#define deb_fe(args...) dprintk(dvb_usb_az6007_debug, 0x08, args)
41
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030042DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
43
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -030044/* Known requests (Cypress FX2 firmware + az6007 "private" ones*/
45
46#define FX2_OED 0xb5
47#define AZ6007_READ_DATA 0xb7
48#define AZ6007_I2C_RD 0xb9
49#define AZ6007_POWER 0xbc
50#define AZ6007_I2C_WR 0xbd
51#define FX2_SCON1 0xc0
52#define AZ6007_TS_THROUGH 0xc7
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -030053#define AZ6007_READ_IR 0xb4
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -030054
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030055struct az6007_device_state {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -030056 struct mutex mutex;
57 struct dvb_ca_en50221 ca;
58 unsigned warm:1;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030059 int (*gate_ctrl) (struct dvb_frontend *, int);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -030060 unsigned char data[4096];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030061};
62
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030063static struct drxk_config terratec_h7_drxk = {
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030064 .adr = 0x29,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030065 .parallel_ts = true,
66 .dynamic_clk = true,
67 .single_master = true,
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -030068 .enable_merr_cfg = true,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030069 .no_i2c_bridge = false,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -030070 .chunk_size = 64,
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -030071 .mpeg_out_clk_strength = 0x02,
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -030072 .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030073};
74
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030075static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
76{
77 struct dvb_usb_adapter *adap = fe->sec_priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030078 struct az6007_device_state *st;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030079 int status = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030080
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030081 deb_info("%s: %s\n", __func__, enable ? "enable" : "disable");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030082
83 if (!adap)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030084 return -EINVAL;
85
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -030086 st = adap->dev->priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030087
88 if (!st)
89 return -EINVAL;
90
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030091 if (enable)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030092 status = st->gate_ctrl(fe, 1);
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030093 else
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030094 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030095
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030096 return status;
97}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030098
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030099static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300100 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300101 .refclock = 36125000,
102};
103
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300104static int __az6007_read(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300105 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300106{
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300107 int ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300108
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300109 ret = usb_control_msg(udev,
110 usb_rcvctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300111 req,
112 USB_TYPE_VENDOR | USB_DIR_IN,
113 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300114 if (ret < 0) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300115 warn("usb read operation failed. (%d)", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300116 return -EIO;
117 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300118
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300119 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
120 index);
121 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300122
123 return ret;
124}
125
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300126static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value,
127 u16 index, u8 *b, int blen)
128{
129 struct az6007_device_state *st = d->priv;
130 int ret;
131
132 if (mutex_lock_interruptible(&st->mutex) < 0)
133 return -EAGAIN;
134
135 ret = __az6007_read(d->udev, req, value, index, b, blen);
136
137 mutex_unlock(&st->mutex);
138
139 return ret;
140}
141
142static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300143 u16 index, u8 *b, int blen)
144{
145 int ret;
146
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300147 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
148 index);
149 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300150
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300151 if (blen > 64) {
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300152 err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
153 blen);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300154 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300155 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300156
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300157 ret = usb_control_msg(udev,
158 usb_sndctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300159 req,
160 USB_TYPE_VENDOR | USB_DIR_OUT,
161 value, index, b, blen, 5000);
162 if (ret != blen) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300163 err("usb write operation failed. (%d)", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300164 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300165 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300166
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300167 return 0;
168}
169
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300170static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value,
171 u16 index, u8 *b, int blen)
172{
173 struct az6007_device_state *st = d->priv;
174 int ret;
175
176 if (mutex_lock_interruptible(&st->mutex) < 0)
177 return -EAGAIN;
178
179 ret = __az6007_write(d->udev, req, value, index, b, blen);
180
181 mutex_unlock(&st->mutex);
182
183 return ret;
184}
185
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300186static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
187{
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300188 struct dvb_usb_device *d = adap->dev;
189
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300190 deb_info("%s: %s", __func__, onoff ? "enable" : "disable");
191
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300192 return az6007_write(d, 0xbc, onoff, 0, NULL, 0);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300193}
194
195/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300196static struct rc_map_table rc_map_az6007_table[] = {
197 {0x0001, KEY_1},
198 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300199};
200
201/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300202static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300203{
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300204 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300205 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300206 int i;
207
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300208 /*
209 * FIXME: remove the following return to enabled remote querying
210 * The driver likely needs proper locking to avoid troubles between
211 * this call and other concurrent calls.
212 */
213 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300214
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300215 az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300216
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300217 if (st->data[1] == 0x44) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300218 *state = REMOTE_NO_KEY_PRESSED;
219 return 0;
220 }
221
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300222 /*
223 * FIXME: need to make something useful with the keycodes and to
224 * convert it to the non-legacy mode. Yet, it is producing some
225 * debug info already, like:
226 * 88 04 eb 02 fd ff 00 82 63 82 (terratec IR)
227 * 88 04 eb 03 fc 00 00 82 63 82 (terratec IR)
228 * 88 80 7e 0d f2 ff 00 82 63 82 (another NEC-extended based IR)
229 * I suspect that the IR data is at bytes 1 to 4, and byte 5 is parity
230 */
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300231 deb_rc("remote query key: %x %d\n", st->data[1], st->data[1]);
232 print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, st->data, 10);
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300233
234 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300235 if (rc5_custom(&keymap[i]) == st->data[1]) {
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300236 *event = keymap[i].keycode;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300237 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300238
239 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300240 }
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300241 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300242 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300243}
244
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300245static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300246{
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300247 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300248 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300249
250 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
251 memcpy(mac, st->data, sizeof(mac));
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300252
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300253 if (ret > 0)
254 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
255 __func__, mac[0], mac[1], mac[2],
256 mac[3], mac[4], mac[5]);
257
258 return ret;
259}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300260
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300261static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
262{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300263 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300264
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300265 deb_info("attaching demod drxk");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300266
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300267 adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
268 &adap->dev->i2c_adap);
269 if (!adap->fe_adap[0].fe)
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300270 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300271
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300272 adap->fe_adap[0].fe->sec_priv = adap;
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300273 st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
274 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300275
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300276 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300277}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300278
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300279static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
280{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300281 deb_info("attaching tuner mt2063");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300282
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300283 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300284 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
285 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300286 if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300287 &az6007_mt2063_config,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300288 &adap->dev->i2c_adap))
289 return -EINVAL;
290
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300291 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
292 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300293
294 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300295}
296
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300297int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
298{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300299 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300300 int ret;
301
302 deb_info("%s()\n", __func__);
303
304 if (!st->warm) {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300305 mutex_init(&st->mutex);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300306
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300307 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300308 if (ret < 0)
309 return ret;
310 msleep(60);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300311 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300312 if (ret < 0)
313 return ret;
314 msleep(100);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300315 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300316 if (ret < 0)
317 return ret;
318 msleep(20);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300319 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300320 if (ret < 0)
321 return ret;
322
323 msleep(400);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300324 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300325 if (ret < 0)
326 return ret;
327 msleep (150);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300328 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300329 if (ret < 0)
330 return ret;
331 msleep (430);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300332 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300333 if (ret < 0)
334 return ret;
335
336 st->warm = true;
337
338 return 0;
339 }
340
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300341 if (!onoff)
342 return 0;
343
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300344 az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
345 az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300346
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300347 return 0;
348}
349
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300350/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300351static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
352 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300353{
354 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300355 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300356 int i, j, len;
357 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300358 u16 index;
359 u16 value;
360 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300361 u8 req, addr;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300362
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300363 if (mutex_lock_interruptible(&st->mutex) < 0)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300364 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300365
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300366 for (i = 0; i < num; i++) {
367 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300368 if (((i + 1) < num)
369 && (msgs[i].len == 1)
370 && (!msgs[i].flags & I2C_M_RD)
371 && (msgs[i + 1].flags & I2C_M_RD)
372 && (msgs[i].addr == msgs[i + 1].addr)) {
373 /*
374 * A write + read xfer for the same address, where
375 * the first xfer has just 1 byte length.
376 * Need to join both into one operation
377 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300378 if (dvb_usb_az6007_debug & 2)
379 printk(KERN_DEBUG
380 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
381 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300382 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300383 index = msgs[i].buf[0];
384 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300385 length = 6 + msgs[i + 1].len;
386 len = msgs[i + 1].len;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300387 ret = __az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300388 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300389 if (ret >= len) {
390 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300391 msgs[i + 1].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300392 if (dvb_usb_az6007_debug & 2)
393 printk(KERN_CONT
394 "0x%02x ",
395 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300396 }
397 } else
398 ret = -EIO;
399 i++;
400 } else if (!(msgs[i].flags & I2C_M_RD)) {
401 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300402 if (dvb_usb_az6007_debug & 2)
403 printk(KERN_DEBUG
404 "az6007 I2C xfer write addr=0x%x len=%d: ",
405 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300406 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300407 index = msgs[i].buf[0];
408 value = addr | (1 << 8);
409 length = msgs[i].len - 1;
410 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300411 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300412 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
413 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300414 st->data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300415 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300416 printk(KERN_CONT "0x%02x ", st->data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300417 }
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300418 ret = __az6007_write(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300419 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300420 } else {
421 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300422 if (dvb_usb_az6007_debug & 2)
423 printk(KERN_DEBUG
424 "az6007 I2C xfer read addr=0x%x len=%d: ",
425 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300426 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300427 index = msgs[i].buf[0];
428 value = addr;
429 length = msgs[i].len + 6;
430 len = msgs[i].len;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300431 ret = __az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300432 length);
433 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300434 msgs[i].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300435 if (dvb_usb_az6007_debug & 2)
436 printk(KERN_CONT
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300437 "0x%02x ", st->data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300438 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300439 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300440 if (dvb_usb_az6007_debug & 2)
441 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300442 if (ret < 0)
443 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300444 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300445err:
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300446 mutex_unlock(&st->mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300447
448 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300449 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300450 return ret;
451 }
452 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300453}
454
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300455static u32 az6007_i2c_func(struct i2c_adapter *adapter)
456{
457 return I2C_FUNC_I2C;
458}
459
460static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300461 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300462 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300463};
464
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300465int az6007_identify_state(struct usb_device *udev,
466 struct dvb_usb_device_properties *props,
467 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300468{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300469 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300470 u8 *mac;
471
472 mac = kmalloc(6, GFP_ATOMIC);
473 if (!mac)
474 return -ENOMEM;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300475
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300476 /* Try to read the mac address */
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300477 ret = __az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300478 if (ret == 6)
479 *cold = 0;
480 else
481 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300482
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300483 kfree(mac);
484
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300485 if (*cold) {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300486 __az6007_write(udev, 0x09, 1, 0, NULL, 0);
487 __az6007_write(udev, 0x00, 0, 0, NULL, 0);
488 __az6007_write(udev, 0x00, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300489 }
490
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300491 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300492 return 0;
493}
494
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300495static struct dvb_usb_device_properties az6007_properties;
496
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300497static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300498 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300499{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300500 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300501 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300502}
503
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300504static struct usb_device_id az6007_usb_table[] = {
505 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
506 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
507 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300508};
509
510MODULE_DEVICE_TABLE(usb, az6007_usb_table);
511
512static struct dvb_usb_device_properties az6007_properties = {
513 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
514 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300515 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300516 .no_reconnect = 1,
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300517 .size_of_priv = sizeof(struct az6007_device_state),
518 .identify_state = az6007_identify_state,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300519 .num_adapters = 1,
520 .adapter = {
521 {
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300522 .num_frontends = 1,
523 .fe = {{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300524 .streaming_ctrl = az6007_streaming_ctrl,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300525 .tuner_attach = az6007_tuner_attach,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300526 .frontend_attach = az6007_frontend_attach,
527
528 /* parameter for the MPEG2-data transfer */
529 .stream = {
530 .type = USB_BULK,
531 .count = 10,
532 .endpoint = 0x02,
533 .u = {
534 .bulk = {
535 .buffersize = 4096,
536 }
537 }
538 },
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300539 }}
540 } },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300541 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300542 .read_mac_address = az6007_read_mac_addr,
543
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300544 .rc.legacy = {
545 .rc_map_table = rc_map_az6007_table,
546 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
547 .rc_interval = 400,
548 .rc_query = az6007_rc_query,
549 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300550 .i2c_algo = &az6007_i2c_algo,
551
552 .num_device_descs = 2,
553 .devices = {
554 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
555 .cold_ids = { &az6007_usb_table[0], NULL },
556 .warm_ids = { NULL },
557 },
558 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
559 .cold_ids = { &az6007_usb_table[1], NULL },
560 .warm_ids = { NULL },
561 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300562 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300563 }
564};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300565
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300566/* usb specific object needed to register this driver with the usb subsystem */
567static struct usb_driver az6007_usb_driver = {
568 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300569 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300570 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300571 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300572};
573
574/* module stuff */
575static int __init az6007_usb_module_init(void)
576{
577 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300578 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300579
580 result = usb_register(&az6007_usb_driver);
581 if (result) {
582 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300583 return result;
584 }
585
586 return 0;
587}
588
589static void __exit az6007_usb_module_exit(void)
590{
591 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300592 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300593 usb_deregister(&az6007_usb_driver);
594}
595
596module_init(az6007_usb_module_init);
597module_exit(az6007_usb_module_exit);
598
599MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300600MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300601MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300602MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300603MODULE_LICENSE("GPL");