blob: a8aedb87ae69ec8273d13424cd597fb66d5da1ee [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;
Mauro Carvalho Chehab91651442012-01-21 12:14:13 -0300207 unsigned code = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300208
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300209 az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300210
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300211 if (st->data[1] == 0x44) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300212 *state = REMOTE_NO_KEY_PRESSED;
213 return 0;
214 }
215
Mauro Carvalho Chehab91651442012-01-21 12:14:13 -0300216 if ((st->data[1] ^ st->data[2]) == 0xff)
217 code = st->data[1];
218 else
219 code = st->data[1] << 8 | st->data[2];
220
221 if ((st->data[3] ^ st->data[4]) == 0xff)
222 code = code << 8 | st->data[3];
223 else
224 code = code << 16 | st->data[3] << 8| st->data[4];
225
226 printk("remote query key: %04x\n", code);
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300227 print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, st->data, 10);
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300228
229 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
Mauro Carvalho Chehab91651442012-01-21 12:14:13 -0300230 if (rc5_custom(&keymap[i]) == code) {
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300231 *event = keymap[i].keycode;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300232 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300233
234 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300235 }
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300236 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300237 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300238}
239
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300240static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300241{
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300242 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300243 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300244
245 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
246 memcpy(mac, st->data, sizeof(mac));
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300247
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300248 if (ret > 0)
249 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
250 __func__, mac[0], mac[1], mac[2],
251 mac[3], mac[4], mac[5]);
252
253 return ret;
254}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300255
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300256static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
257{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300258 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300259
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300260 deb_info("attaching demod drxk");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300261
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300262 adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
263 &adap->dev->i2c_adap);
264 if (!adap->fe_adap[0].fe)
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300265 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300266
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300267 adap->fe_adap[0].fe->sec_priv = adap;
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300268 st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
269 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300270
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300271 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300272}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300273
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300274static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
275{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300276 deb_info("attaching tuner mt2063");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300277
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300278 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300279 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
280 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300281 if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300282 &az6007_mt2063_config,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300283 &adap->dev->i2c_adap))
284 return -EINVAL;
285
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300286 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
287 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300288
289 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300290}
291
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300292int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
293{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300294 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300295 int ret;
296
297 deb_info("%s()\n", __func__);
298
299 if (!st->warm) {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300300 mutex_init(&st->mutex);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300301
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300302 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300303 if (ret < 0)
304 return ret;
305 msleep(60);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300306 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300307 if (ret < 0)
308 return ret;
309 msleep(100);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300310 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300311 if (ret < 0)
312 return ret;
313 msleep(20);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300314 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300315 if (ret < 0)
316 return ret;
317
318 msleep(400);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300319 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300320 if (ret < 0)
321 return ret;
322 msleep (150);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300323 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300324 if (ret < 0)
325 return ret;
326 msleep (430);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300327 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300328 if (ret < 0)
329 return ret;
330
331 st->warm = true;
332
333 return 0;
334 }
335
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300336 if (!onoff)
337 return 0;
338
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300339 az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
340 az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300341
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300342 return 0;
343}
344
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300345/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300346static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
347 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300348{
349 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300350 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300351 int i, j, len;
352 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300353 u16 index;
354 u16 value;
355 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300356 u8 req, addr;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300357
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300358 if (mutex_lock_interruptible(&st->mutex) < 0)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300359 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300360
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300361 for (i = 0; i < num; i++) {
362 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300363 if (((i + 1) < num)
364 && (msgs[i].len == 1)
365 && (!msgs[i].flags & I2C_M_RD)
366 && (msgs[i + 1].flags & I2C_M_RD)
367 && (msgs[i].addr == msgs[i + 1].addr)) {
368 /*
369 * A write + read xfer for the same address, where
370 * the first xfer has just 1 byte length.
371 * Need to join both into one operation
372 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300373 if (dvb_usb_az6007_debug & 2)
374 printk(KERN_DEBUG
375 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
376 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300377 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300378 index = msgs[i].buf[0];
379 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300380 length = 6 + msgs[i + 1].len;
381 len = msgs[i + 1].len;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300382 ret = __az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300383 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300384 if (ret >= len) {
385 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300386 msgs[i + 1].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300387 if (dvb_usb_az6007_debug & 2)
388 printk(KERN_CONT
389 "0x%02x ",
390 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300391 }
392 } else
393 ret = -EIO;
394 i++;
395 } else if (!(msgs[i].flags & I2C_M_RD)) {
396 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300397 if (dvb_usb_az6007_debug & 2)
398 printk(KERN_DEBUG
399 "az6007 I2C xfer write addr=0x%x len=%d: ",
400 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300401 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300402 index = msgs[i].buf[0];
403 value = addr | (1 << 8);
404 length = msgs[i].len - 1;
405 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300406 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300407 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
408 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300409 st->data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300410 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300411 printk(KERN_CONT "0x%02x ", st->data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300412 }
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300413 ret = __az6007_write(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300414 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300415 } else {
416 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300417 if (dvb_usb_az6007_debug & 2)
418 printk(KERN_DEBUG
419 "az6007 I2C xfer read addr=0x%x len=%d: ",
420 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300421 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300422 index = msgs[i].buf[0];
423 value = addr;
424 length = msgs[i].len + 6;
425 len = msgs[i].len;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300426 ret = __az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300427 length);
428 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300429 msgs[i].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300430 if (dvb_usb_az6007_debug & 2)
431 printk(KERN_CONT
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300432 "0x%02x ", st->data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300433 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300434 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300435 if (dvb_usb_az6007_debug & 2)
436 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300437 if (ret < 0)
438 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300439 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300440err:
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300441 mutex_unlock(&st->mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300442
443 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300444 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300445 return ret;
446 }
447 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300448}
449
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300450static u32 az6007_i2c_func(struct i2c_adapter *adapter)
451{
452 return I2C_FUNC_I2C;
453}
454
455static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300456 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300457 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300458};
459
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300460int az6007_identify_state(struct usb_device *udev,
461 struct dvb_usb_device_properties *props,
462 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300463{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300464 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300465 u8 *mac;
466
467 mac = kmalloc(6, GFP_ATOMIC);
468 if (!mac)
469 return -ENOMEM;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300470
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300471 /* Try to read the mac address */
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300472 ret = __az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300473 if (ret == 6)
474 *cold = 0;
475 else
476 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300477
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300478 kfree(mac);
479
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300480 if (*cold) {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300481 __az6007_write(udev, 0x09, 1, 0, NULL, 0);
482 __az6007_write(udev, 0x00, 0, 0, NULL, 0);
483 __az6007_write(udev, 0x00, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300484 }
485
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300486 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300487 return 0;
488}
489
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300490static struct dvb_usb_device_properties az6007_properties;
491
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300492static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300493 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300494{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300495 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300496 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300497}
498
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300499static struct usb_device_id az6007_usb_table[] = {
500 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
501 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
502 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300503};
504
505MODULE_DEVICE_TABLE(usb, az6007_usb_table);
506
507static struct dvb_usb_device_properties az6007_properties = {
508 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
509 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300510 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300511 .no_reconnect = 1,
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300512 .size_of_priv = sizeof(struct az6007_device_state),
513 .identify_state = az6007_identify_state,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300514 .num_adapters = 1,
515 .adapter = {
516 {
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300517 .num_frontends = 1,
518 .fe = {{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300519 .streaming_ctrl = az6007_streaming_ctrl,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300520 .tuner_attach = az6007_tuner_attach,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300521 .frontend_attach = az6007_frontend_attach,
522
523 /* parameter for the MPEG2-data transfer */
524 .stream = {
525 .type = USB_BULK,
526 .count = 10,
527 .endpoint = 0x02,
528 .u = {
529 .bulk = {
530 .buffersize = 4096,
531 }
532 }
533 },
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300534 }}
535 } },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300536 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300537 .read_mac_address = az6007_read_mac_addr,
538
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300539 .rc.legacy = {
540 .rc_map_table = rc_map_az6007_table,
541 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
542 .rc_interval = 400,
543 .rc_query = az6007_rc_query,
544 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300545 .i2c_algo = &az6007_i2c_algo,
546
547 .num_device_descs = 2,
548 .devices = {
549 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
550 .cold_ids = { &az6007_usb_table[0], NULL },
551 .warm_ids = { NULL },
552 },
553 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
554 .cold_ids = { &az6007_usb_table[1], NULL },
555 .warm_ids = { NULL },
556 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300557 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300558 }
559};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300560
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300561/* usb specific object needed to register this driver with the usb subsystem */
562static struct usb_driver az6007_usb_driver = {
563 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300564 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300565 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300566 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300567};
568
569/* module stuff */
570static int __init az6007_usb_module_init(void)
571{
572 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300573 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300574
575 result = usb_register(&az6007_usb_driver);
576 if (result) {
577 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300578 return result;
579 }
580
581 return 0;
582}
583
584static void __exit az6007_usb_module_exit(void)
585{
586 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300587 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300588 usb_deregister(&az6007_usb_driver);
589}
590
591module_init(az6007_usb_module_init);
592module_exit(az6007_usb_module_exit);
593
594MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300595MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300596MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300597MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300598MODULE_LICENSE("GPL");