blob: 2288916a2a4acbabbc33c5eacfec744386fc6461 [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
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300195/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300196static int az6007_rc_query(struct dvb_usb_device *d)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300197{
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300198 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehab91651442012-01-21 12:14:13 -0300199 unsigned code = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300200
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300201 az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300202
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300203 if (st->data[1] == 0x44)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300204 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300205
Mauro Carvalho Chehab91651442012-01-21 12:14:13 -0300206 if ((st->data[1] ^ st->data[2]) == 0xff)
207 code = st->data[1];
208 else
209 code = st->data[1] << 8 | st->data[2];
210
211 if ((st->data[3] ^ st->data[4]) == 0xff)
212 code = code << 8 | st->data[3];
213 else
214 code = code << 16 | st->data[3] << 8| st->data[4];
215
216 printk("remote query key: %04x\n", code);
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300217
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300218 rc_keydown(d->rc_dev, code, st->data[5]);
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300219
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300220 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300221}
222
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300223static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300224{
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300225 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300226 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300227
228 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
229 memcpy(mac, st->data, sizeof(mac));
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_attach(struct dvb_usb_adapter *adap)
240{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300241 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300242
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300243 deb_info("attaching demod drxk");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300244
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300245 adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
246 &adap->dev->i2c_adap);
247 if (!adap->fe_adap[0].fe)
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300248 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300249
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300250 adap->fe_adap[0].fe->sec_priv = adap;
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300251 st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
252 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300253
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300254 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300255}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300256
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300257static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
258{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300259 deb_info("attaching tuner mt2063");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300260
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300261 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300262 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
263 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300264 if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300265 &az6007_mt2063_config,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300266 &adap->dev->i2c_adap))
267 return -EINVAL;
268
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300269 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
270 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300271
272 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300273}
274
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300275int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
276{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300277 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300278 int ret;
279
280 deb_info("%s()\n", __func__);
281
282 if (!st->warm) {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300283 mutex_init(&st->mutex);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300284
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300285 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300286 if (ret < 0)
287 return ret;
288 msleep(60);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300289 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300290 if (ret < 0)
291 return ret;
292 msleep(100);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300293 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300294 if (ret < 0)
295 return ret;
296 msleep(20);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300297 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300298 if (ret < 0)
299 return ret;
300
301 msleep(400);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300302 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300303 if (ret < 0)
304 return ret;
305 msleep (150);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300306 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300307 if (ret < 0)
308 return ret;
309 msleep (430);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300310 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300311 if (ret < 0)
312 return ret;
313
314 st->warm = true;
315
316 return 0;
317 }
318
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300319 if (!onoff)
320 return 0;
321
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300322 az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
323 az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300324
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300325 return 0;
326}
327
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300328/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300329static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
330 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300331{
332 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300333 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300334 int i, j, len;
335 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300336 u16 index;
337 u16 value;
338 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300339 u8 req, addr;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300340
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300341 if (mutex_lock_interruptible(&st->mutex) < 0)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300342 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300343
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300344 for (i = 0; i < num; i++) {
345 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300346 if (((i + 1) < num)
347 && (msgs[i].len == 1)
348 && (!msgs[i].flags & I2C_M_RD)
349 && (msgs[i + 1].flags & I2C_M_RD)
350 && (msgs[i].addr == msgs[i + 1].addr)) {
351 /*
352 * A write + read xfer for the same address, where
353 * the first xfer has just 1 byte length.
354 * Need to join both into one operation
355 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300356 if (dvb_usb_az6007_debug & 2)
357 printk(KERN_DEBUG
358 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
359 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300360 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300361 index = msgs[i].buf[0];
362 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300363 length = 6 + msgs[i + 1].len;
364 len = msgs[i + 1].len;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300365 ret = __az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300366 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300367 if (ret >= len) {
368 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300369 msgs[i + 1].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300370 if (dvb_usb_az6007_debug & 2)
371 printk(KERN_CONT
372 "0x%02x ",
373 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300374 }
375 } else
376 ret = -EIO;
377 i++;
378 } else if (!(msgs[i].flags & I2C_M_RD)) {
379 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300380 if (dvb_usb_az6007_debug & 2)
381 printk(KERN_DEBUG
382 "az6007 I2C xfer write addr=0x%x len=%d: ",
383 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300384 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300385 index = msgs[i].buf[0];
386 value = addr | (1 << 8);
387 length = msgs[i].len - 1;
388 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300389 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300390 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
391 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300392 st->data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300393 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300394 printk(KERN_CONT "0x%02x ", st->data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300395 }
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300396 ret = __az6007_write(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300397 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300398 } else {
399 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300400 if (dvb_usb_az6007_debug & 2)
401 printk(KERN_DEBUG
402 "az6007 I2C xfer read addr=0x%x len=%d: ",
403 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300404 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300405 index = msgs[i].buf[0];
406 value = addr;
407 length = msgs[i].len + 6;
408 len = msgs[i].len;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300409 ret = __az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300410 length);
411 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300412 msgs[i].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300413 if (dvb_usb_az6007_debug & 2)
414 printk(KERN_CONT
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300415 "0x%02x ", st->data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300416 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300417 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300418 if (dvb_usb_az6007_debug & 2)
419 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300420 if (ret < 0)
421 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300422 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300423err:
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300424 mutex_unlock(&st->mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300425
426 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300427 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300428 return ret;
429 }
430 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300431}
432
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300433static u32 az6007_i2c_func(struct i2c_adapter *adapter)
434{
435 return I2C_FUNC_I2C;
436}
437
438static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300439 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300440 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300441};
442
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300443int az6007_identify_state(struct usb_device *udev,
444 struct dvb_usb_device_properties *props,
445 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300446{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300447 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300448 u8 *mac;
449
450 mac = kmalloc(6, GFP_ATOMIC);
451 if (!mac)
452 return -ENOMEM;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300453
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300454 /* Try to read the mac address */
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300455 ret = __az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300456 if (ret == 6)
457 *cold = 0;
458 else
459 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300460
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300461 kfree(mac);
462
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300463 if (*cold) {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300464 __az6007_write(udev, 0x09, 1, 0, NULL, 0);
465 __az6007_write(udev, 0x00, 0, 0, NULL, 0);
466 __az6007_write(udev, 0x00, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300467 }
468
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300469 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300470 return 0;
471}
472
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300473static struct dvb_usb_device_properties az6007_properties;
474
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300475static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300476 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300477{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300478 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300479 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300480}
481
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300482static struct usb_device_id az6007_usb_table[] = {
483 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
484 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
485 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300486};
487
488MODULE_DEVICE_TABLE(usb, az6007_usb_table);
489
490static struct dvb_usb_device_properties az6007_properties = {
491 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
492 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300493 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300494 .no_reconnect = 1,
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300495 .size_of_priv = sizeof(struct az6007_device_state),
496 .identify_state = az6007_identify_state,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300497 .num_adapters = 1,
498 .adapter = {
499 {
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300500 .num_frontends = 1,
501 .fe = {{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300502 .streaming_ctrl = az6007_streaming_ctrl,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300503 .tuner_attach = az6007_tuner_attach,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300504 .frontend_attach = az6007_frontend_attach,
505
506 /* parameter for the MPEG2-data transfer */
507 .stream = {
508 .type = USB_BULK,
509 .count = 10,
510 .endpoint = 0x02,
511 .u = {
512 .bulk = {
513 .buffersize = 4096,
514 }
515 }
516 },
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300517 }}
518 } },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300519 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300520 .read_mac_address = az6007_read_mac_addr,
521
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300522 .rc.core = {
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300523 .rc_interval = 400,
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300524 .rc_codes = RC_MAP_DIB0700_NEC_TABLE,
525 .module_name = "az6007",
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300526 .rc_query = az6007_rc_query,
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300527 .allowed_protos = RC_TYPE_NEC,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300528 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300529 .i2c_algo = &az6007_i2c_algo,
530
531 .num_device_descs = 2,
532 .devices = {
533 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
534 .cold_ids = { &az6007_usb_table[0], NULL },
535 .warm_ids = { NULL },
536 },
537 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
538 .cold_ids = { &az6007_usb_table[1], NULL },
539 .warm_ids = { NULL },
540 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300541 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300542 }
543};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300544
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300545/* usb specific object needed to register this driver with the usb subsystem */
546static struct usb_driver az6007_usb_driver = {
547 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300548 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300549 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300550 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300551};
552
553/* module stuff */
554static int __init az6007_usb_module_init(void)
555{
556 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300557 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300558
559 result = usb_register(&az6007_usb_driver);
560 if (result) {
561 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300562 return result;
563 }
564
565 return 0;
566}
567
568static void __exit az6007_usb_module_exit(void)
569{
570 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300571 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300572 usb_deregister(&az6007_usb_driver);
573}
574
575module_init(az6007_usb_module_init);
576module_exit(az6007_usb_module_exit);
577
578MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300579MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300580MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300581MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300582MODULE_LICENSE("GPL");