blob: 534d3267c86ea34bfaf271a2fc02d3090048b3a4 [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 Chehab93b32122011-07-23 10:40:08 -030056 struct dvb_ca_en50221 ca;
57 struct mutex ca_mutex;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -030058 unsigned warm : 1;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030059
60 /* Due to DRX-K - probably need changes */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030061 int (*gate_ctrl) (struct dvb_frontend *, int);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -030062 bool tuner_attached;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -030063
64 unsigned char data[4096];
65
66 struct usb_data_stream *stream;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030067};
68
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030069static struct drxk_config terratec_h7_drxk = {
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030070 .adr = 0x29,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030071 .parallel_ts = true,
72 .dynamic_clk = true,
73 .single_master = true,
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -030074 .enable_merr_cfg = true,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030075 .no_i2c_bridge = false,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -030076 .chunk_size = 64,
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -030077 .mpeg_out_clk_strength = 0x02,
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -030078 .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030079};
80
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030081static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
82{
83 struct dvb_usb_adapter *adap = fe->sec_priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030084 struct az6007_device_state *st;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030085 int status = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030086
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030087 deb_info("%s: %s\n", __func__, enable ? "enable" : "disable");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030088
89 if (!adap)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030090 return -EINVAL;
91
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -030092 st = adap->dev->priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030093
94 if (!st)
95 return -EINVAL;
96
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030097 if (enable)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030098 status = st->gate_ctrl(fe, 1);
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030099 else
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300100 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300101
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300102 return status;
103}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300104
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300105static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300106 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300107 .refclock = 36125000,
108};
109
110/* check for mutex FIXME */
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300111static int az6007_read(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300112 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300113{
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300114 int ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300115
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300116 ret = usb_control_msg(udev,
117 usb_rcvctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300118 req,
119 USB_TYPE_VENDOR | USB_DIR_IN,
120 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300121 if (ret < 0) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300122 warn("usb read operation failed. (%d)", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300123 return -EIO;
124 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300125
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300126 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
127 index);
128 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300129
130 return ret;
131}
132
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300133static int az6007_write(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300134 u16 index, u8 *b, int blen)
135{
136 int ret;
137
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300138 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
139 index);
140 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300141
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300142 if (blen > 64) {
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300143 err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
144 blen);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300145 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300146 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300147
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300148 ret = usb_control_msg(udev,
149 usb_sndctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300150 req,
151 USB_TYPE_VENDOR | USB_DIR_OUT,
152 value, index, b, blen, 5000);
153 if (ret != blen) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300154 err("usb write operation failed. (%d)", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300155 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300156 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300157
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300158 return 0;
159}
160
161static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
162{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300163 deb_info("%s: %s", __func__, onoff ? "enable" : "disable");
164
165 return az6007_write(adap->dev->udev, 0xbc, onoff, 0, NULL, 0);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300166}
167
168/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300169static struct rc_map_table rc_map_az6007_table[] = {
170 {0x0001, KEY_1},
171 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300172};
173
174/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300175static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300176{
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300177 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300178 u8 key[10];
179 int i;
180
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300181 /*
182 * FIXME: remove the following return to enabled remote querying
183 * The driver likely needs proper locking to avoid troubles between
184 * this call and other concurrent calls.
185 */
186 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300187
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300188 az6007_read(d->udev, AZ6007_READ_IR, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300189
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300190 if (key[1] == 0x44) {
191 *state = REMOTE_NO_KEY_PRESSED;
192 return 0;
193 }
194
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300195 /*
196 * FIXME: need to make something useful with the keycodes and to
197 * convert it to the non-legacy mode. Yet, it is producing some
198 * debug info already, like:
199 * 88 04 eb 02 fd ff 00 82 63 82 (terratec IR)
200 * 88 04 eb 03 fc 00 00 82 63 82 (terratec IR)
201 * 88 80 7e 0d f2 ff 00 82 63 82 (another NEC-extended based IR)
202 * I suspect that the IR data is at bytes 1 to 4, and byte 5 is parity
203 */
204 deb_rc("remote query key: %x %d\n", key[1], key[1]);
205 print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, key, 10);
206
207 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
208 if (rc5_custom(&keymap[i]) == key[1]) {
209 *event = keymap[i].keycode;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300210 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300211
212 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300213 }
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300214 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300215 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300216}
217
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300218static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300219{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300220 int ret;
221 ret = az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300222
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300223 if (ret > 0)
224 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
225 __func__, mac[0], mac[1], mac[2],
226 mac[3], mac[4], mac[5]);
227
228 return ret;
229}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300230
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300231static int az6007_led_on_off(struct usb_interface *intf, int onoff)
232{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300233 struct usb_device *udev = interface_to_usbdev(intf);
234 int ret;
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300235
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300236 /* TS through */
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300237 ret = az6007_write(udev, AZ6007_POWER, onoff, 0, NULL, 0);
238 if (ret < 0)
239 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300240 return ret;
241}
242
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300243static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
244{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300245 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300246
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300247 deb_info("attaching demod drxk");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300248
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300249 adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
250 &adap->dev->i2c_adap);
251 if (!adap->fe_adap[0].fe)
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300252 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300253
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300254 adap->fe_adap[0].fe->sec_priv = adap;
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300255 st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
256 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300257
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300258 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300259}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300260
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300261static int az6007_tuner_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 Chehab1c9a2842011-07-31 10:11:32 -0300265 if (st->tuner_attached)
266 return 0;
267
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300268 deb_info("attaching tuner mt2063");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300269
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300270 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300271 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
272 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300273 if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300274 &az6007_mt2063_config,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300275 &adap->dev->i2c_adap))
276 return -EINVAL;
277
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300278 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
279 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300280
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300281 st->tuner_attached = true;
282
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300283 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300284}
285
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300286int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
287{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300288 struct az6007_device_state *st = d->priv;
289 struct usb_device *udev = d->udev;
290 int ret;
291
292 deb_info("%s()\n", __func__);
293
294 if (!st->warm) {
295 u8 data[6];
296
297 az6007_read(udev, FX2_OED, 1, 0, data, 1); /* {0x01} */
298 az6007_read(udev, AZ6007_READ_DATA, 0, 8160, data, 1); /* {0x20} */
299 az6007_read(udev, AZ6007_READ_DATA, 0, 0, data, 5); /* {0x00, 0x00, 0x00, 0x00, 0x0a} */
300 az6007_read(udev, AZ6007_READ_DATA, 0, 4080, data, 6); /* {0x00, 0x08, 0x00, 0x0c, 0x22, 0x38} */
301
302 ret = az6007_write(udev, AZ6007_POWER, 0, 2, NULL, 0);
303 if (ret < 0)
304 return ret;
305 msleep(60);
306 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
307 if (ret < 0)
308 return ret;
309 msleep(100);
310 ret = az6007_write(udev, AZ6007_POWER, 1, 3, NULL, 0);
311 if (ret < 0)
312 return ret;
313 msleep(20);
314 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
315 if (ret < 0)
316 return ret;
317
318 msleep(400);
319 ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
320 if (ret < 0)
321 return ret;
322 msleep (150);
323 ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
324 if (ret < 0)
325 return ret;
326 msleep (430);
327 ret = az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
328 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 Chehabb19280c2012-01-20 18:37:01 -0300339 az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
340 az6007_write(udev, 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 Chehab71d67632011-07-21 17:46:41 -0300358 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
359 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 Chehabb19280c2012-01-20 18:37:01 -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 Chehabb19280c2012-01-20 18:37:01 -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 Chehabb19280c2012-01-20 18:37:01 -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 Chehab71d67632011-07-21 17:46:41 -0300441 mutex_unlock(&d->i2c_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;
465 u8 mac[6];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300466
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300467 /* Try to read the mac address */
468 ret = az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
469 if (ret == 6)
470 *cold = 0;
471 else
472 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300473
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300474 if (*cold) {
475 az6007_write(udev, 0x09, 1, 0, NULL, 0);
476 az6007_write(udev, 0x00, 0, 0, NULL, 0);
477 az6007_write(udev, 0x00, 0, 0, NULL, 0);
478 }
479
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300480 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300481 return 0;
482}
483
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300484static struct dvb_usb_device_properties az6007_properties;
485
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300486static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300487 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300488{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300489 az6007_led_on_off(intf, 0);
490
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300491 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300492 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300493}
494
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300495static struct usb_device_id az6007_usb_table[] = {
496 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
497 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
498 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300499};
500
501MODULE_DEVICE_TABLE(usb, az6007_usb_table);
502
503static struct dvb_usb_device_properties az6007_properties = {
504 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
505 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300506 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300507 .no_reconnect = 1,
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300508 .size_of_priv = sizeof(struct az6007_device_state),
509 .identify_state = az6007_identify_state,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300510 .num_adapters = 1,
511 .adapter = {
512 {
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300513 .num_frontends = 1,
514 .fe = {{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300515 .streaming_ctrl = az6007_streaming_ctrl,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300516 .tuner_attach = az6007_tuner_attach,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300517 .frontend_attach = az6007_frontend_attach,
518
519 /* parameter for the MPEG2-data transfer */
520 .stream = {
521 .type = USB_BULK,
522 .count = 10,
523 .endpoint = 0x02,
524 .u = {
525 .bulk = {
526 .buffersize = 4096,
527 }
528 }
529 },
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300530 }}
531 } },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300532 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300533 .read_mac_address = az6007_read_mac_addr,
534
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300535 .rc.legacy = {
536 .rc_map_table = rc_map_az6007_table,
537 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
538 .rc_interval = 400,
539 .rc_query = az6007_rc_query,
540 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300541 .i2c_algo = &az6007_i2c_algo,
542
543 .num_device_descs = 2,
544 .devices = {
545 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
546 .cold_ids = { &az6007_usb_table[0], NULL },
547 .warm_ids = { NULL },
548 },
549 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
550 .cold_ids = { &az6007_usb_table[1], NULL },
551 .warm_ids = { NULL },
552 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300553 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300554 }
555};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300556
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300557/* usb specific object needed to register this driver with the usb subsystem */
558static struct usb_driver az6007_usb_driver = {
559 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300560 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300561 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300562 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300563};
564
565/* module stuff */
566static int __init az6007_usb_module_init(void)
567{
568 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300569 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300570
571 result = usb_register(&az6007_usb_driver);
572 if (result) {
573 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300574 return result;
575 }
576
577 return 0;
578}
579
580static void __exit az6007_usb_module_exit(void)
581{
582 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300583 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300584 usb_deregister(&az6007_usb_driver);
585}
586
587module_init(az6007_usb_module_init);
588module_exit(az6007_usb_module_exit);
589
590MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300591MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300592MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300593MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300594MODULE_LICENSE("GPL");