blob: 6177332a7a0e3b7d76f7098be15bb69bb4102685 [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 Chehab2d5161b2011-07-29 11:31:13 -0300204 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300205 u8 key[10];
206 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 Chehaba2c35d32012-01-21 11:19:16 -0300215 az6007_read(d, AZ6007_READ_IR, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300216
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300217 if (key[1] == 0x44) {
218 *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 */
231 deb_rc("remote query key: %x %d\n", key[1], key[1]);
232 print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, key, 10);
233
234 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
235 if (rc5_custom(&keymap[i]) == key[1]) {
236 *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 Chehab3aecf2c2011-07-25 12:45:16 -0300247 int ret;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300248 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300249
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300250 if (ret > 0)
251 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
252 __func__, mac[0], mac[1], mac[2],
253 mac[3], mac[4], mac[5]);
254
255 return ret;
256}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300257
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300258static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
259{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300260 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300261
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300262 deb_info("attaching demod drxk");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300263
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300264 adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
265 &adap->dev->i2c_adap);
266 if (!adap->fe_adap[0].fe)
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300267 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300268
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300269 adap->fe_adap[0].fe->sec_priv = adap;
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300270 st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
271 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300272
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300273 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300274}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300275
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300276static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
277{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300278 deb_info("attaching tuner mt2063");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300279
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300280 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300281 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
282 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300283 if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300284 &az6007_mt2063_config,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300285 &adap->dev->i2c_adap))
286 return -EINVAL;
287
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300288 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
289 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300290
291 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300292}
293
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300294int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
295{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300296 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300297 int ret;
298
299 deb_info("%s()\n", __func__);
300
301 if (!st->warm) {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300302 mutex_init(&st->mutex);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300303
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300304 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300305 if (ret < 0)
306 return ret;
307 msleep(60);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300308 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300309 if (ret < 0)
310 return ret;
311 msleep(100);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300312 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300313 if (ret < 0)
314 return ret;
315 msleep(20);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300316 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300317 if (ret < 0)
318 return ret;
319
320 msleep(400);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300321 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300322 if (ret < 0)
323 return ret;
324 msleep (150);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300325 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300326 if (ret < 0)
327 return ret;
328 msleep (430);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300329 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300330 if (ret < 0)
331 return ret;
332
333 st->warm = true;
334
335 return 0;
336 }
337
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300338 if (!onoff)
339 return 0;
340
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300341 az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
342 az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300343
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300344 return 0;
345}
346
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300347/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300348static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
349 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300350{
351 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300352 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300353 int i, j, len;
354 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300355 u16 index;
356 u16 value;
357 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300358 u8 req, addr;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300359
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300360 if (mutex_lock_interruptible(&st->mutex) < 0)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300361 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300362
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300363 for (i = 0; i < num; i++) {
364 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300365 if (((i + 1) < num)
366 && (msgs[i].len == 1)
367 && (!msgs[i].flags & I2C_M_RD)
368 && (msgs[i + 1].flags & I2C_M_RD)
369 && (msgs[i].addr == msgs[i + 1].addr)) {
370 /*
371 * A write + read xfer for the same address, where
372 * the first xfer has just 1 byte length.
373 * Need to join both into one operation
374 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300375 if (dvb_usb_az6007_debug & 2)
376 printk(KERN_DEBUG
377 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
378 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300379 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300380 index = msgs[i].buf[0];
381 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300382 length = 6 + msgs[i + 1].len;
383 len = msgs[i + 1].len;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300384 ret = __az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300385 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300386 if (ret >= len) {
387 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300388 msgs[i + 1].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300389 if (dvb_usb_az6007_debug & 2)
390 printk(KERN_CONT
391 "0x%02x ",
392 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300393 }
394 } else
395 ret = -EIO;
396 i++;
397 } else if (!(msgs[i].flags & I2C_M_RD)) {
398 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300399 if (dvb_usb_az6007_debug & 2)
400 printk(KERN_DEBUG
401 "az6007 I2C xfer write addr=0x%x len=%d: ",
402 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300403 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300404 index = msgs[i].buf[0];
405 value = addr | (1 << 8);
406 length = msgs[i].len - 1;
407 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300408 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300409 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
410 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300411 st->data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300412 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300413 printk(KERN_CONT "0x%02x ", st->data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300414 }
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300415 ret = __az6007_write(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300416 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300417 } else {
418 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300419 if (dvb_usb_az6007_debug & 2)
420 printk(KERN_DEBUG
421 "az6007 I2C xfer read addr=0x%x len=%d: ",
422 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300423 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300424 index = msgs[i].buf[0];
425 value = addr;
426 length = msgs[i].len + 6;
427 len = msgs[i].len;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300428 ret = __az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300429 length);
430 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300431 msgs[i].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300432 if (dvb_usb_az6007_debug & 2)
433 printk(KERN_CONT
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300434 "0x%02x ", st->data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300435 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300436 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300437 if (dvb_usb_az6007_debug & 2)
438 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300439 if (ret < 0)
440 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300441 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300442err:
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300443 mutex_unlock(&st->mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300444
445 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300446 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300447 return ret;
448 }
449 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300450}
451
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300452static u32 az6007_i2c_func(struct i2c_adapter *adapter)
453{
454 return I2C_FUNC_I2C;
455}
456
457static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300458 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300459 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300460};
461
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300462int az6007_identify_state(struct usb_device *udev,
463 struct dvb_usb_device_properties *props,
464 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300465{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300466 int ret;
467 u8 mac[6];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300468
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300469 /* Try to read the mac address */
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300470 ret = __az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300471 if (ret == 6)
472 *cold = 0;
473 else
474 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300475
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300476 if (*cold) {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300477 __az6007_write(udev, 0x09, 1, 0, NULL, 0);
478 __az6007_write(udev, 0x00, 0, 0, NULL, 0);
479 __az6007_write(udev, 0x00, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300480 }
481
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300482 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300483 return 0;
484}
485
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300486static struct dvb_usb_device_properties az6007_properties;
487
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300488static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300489 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300490{
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300491 struct usb_device *udev = interface_to_usbdev(intf);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300492
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300493 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300494 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300495}
496
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300497static struct usb_device_id az6007_usb_table[] = {
498 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
499 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
500 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300501};
502
503MODULE_DEVICE_TABLE(usb, az6007_usb_table);
504
505static struct dvb_usb_device_properties az6007_properties = {
506 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
507 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300508 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300509 .no_reconnect = 1,
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300510 .size_of_priv = sizeof(struct az6007_device_state),
511 .identify_state = az6007_identify_state,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300512 .num_adapters = 1,
513 .adapter = {
514 {
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300515 .num_frontends = 1,
516 .fe = {{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300517 .streaming_ctrl = az6007_streaming_ctrl,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300518 .tuner_attach = az6007_tuner_attach,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300519 .frontend_attach = az6007_frontend_attach,
520
521 /* parameter for the MPEG2-data transfer */
522 .stream = {
523 .type = USB_BULK,
524 .count = 10,
525 .endpoint = 0x02,
526 .u = {
527 .bulk = {
528 .buffersize = 4096,
529 }
530 }
531 },
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300532 }}
533 } },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300534 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300535 .read_mac_address = az6007_read_mac_addr,
536
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300537 .rc.legacy = {
538 .rc_map_table = rc_map_az6007_table,
539 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
540 .rc_interval = 400,
541 .rc_query = az6007_rc_query,
542 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300543 .i2c_algo = &az6007_i2c_algo,
544
545 .num_device_descs = 2,
546 .devices = {
547 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
548 .cold_ids = { &az6007_usb_table[0], NULL },
549 .warm_ids = { NULL },
550 },
551 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
552 .cold_ids = { &az6007_usb_table[1], NULL },
553 .warm_ids = { NULL },
554 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300555 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300556 }
557};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300558
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300559/* usb specific object needed to register this driver with the usb subsystem */
560static struct usb_driver az6007_usb_driver = {
561 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300562 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300563 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300564 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300565};
566
567/* module stuff */
568static int __init az6007_usb_module_init(void)
569{
570 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300571 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300572
573 result = usb_register(&az6007_usb_driver);
574 if (result) {
575 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300576 return result;
577 }
578
579 return 0;
580}
581
582static void __exit az6007_usb_module_exit(void)
583{
584 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300585 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300586 usb_deregister(&az6007_usb_driver);
587}
588
589module_init(az6007_usb_module_init);
590module_exit(az6007_usb_module_exit);
591
592MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300593MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300594MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300595MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300596MODULE_LICENSE("GPL");