blob: f0e4c013bb5d6b72407ebc1fa5727fb940a88826 [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 *
10 * Driver modifiyed by Mauro Carvalho Chehab <mchehab@redhat.com> in order
11 * to work with upstream drxk driver, and to fix some bugs.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation under version 2 of the License.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030021 */
22
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030023#include "drxk.h"
24#include "mt2063.h"
25#include "dvb_ca_en50221.h"
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030026
27#define DVB_USB_LOG_PREFIX "az6007"
Mauro Carvalho Chehabc108a5a2011-07-25 10:38:20 -030028#include "dvb-usb.h"
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030029
30/* debug */
31int dvb_usb_az6007_debug;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030032module_param_named(debug, dvb_usb_az6007_debug, int, 0644);
33MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))."
34 DVB_USB_DEBUG_STATUS);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030035
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030036#define deb_info(args...) dprintk(dvb_usb_az6007_debug, 0x01, args)
37#define deb_xfer(args...) dprintk(dvb_usb_az6007_debug, 0x02, args)
38#define deb_rc(args...) dprintk(dvb_usb_az6007_debug, 0x04, args)
39#define deb_fe(args...) dprintk(dvb_usb_az6007_debug, 0x08, args)
40
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030041DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -030043/* Known requests (Cypress FX2 firmware + az6007 "private" ones*/
44
45#define FX2_OED 0xb5
46#define AZ6007_READ_DATA 0xb7
47#define AZ6007_I2C_RD 0xb9
48#define AZ6007_POWER 0xbc
49#define AZ6007_I2C_WR 0xbd
50#define FX2_SCON1 0xc0
51#define AZ6007_TS_THROUGH 0xc7
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -030052#define AZ6007_READ_IR 0xb4
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -030053
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030054struct az6007_device_state {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030055 struct dvb_ca_en50221 ca;
56 struct mutex ca_mutex;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -030057 unsigned warm : 1;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030058
59 /* Due to DRX-K - probably need changes */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030060 int (*gate_ctrl) (struct dvb_frontend *, int);
61 struct semaphore pll_mutex;
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 Chehab6da34702011-07-21 18:31:14 -030097 if (enable) {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030098#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030099 down(&st->pll_mutex);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -0300100#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300101 status = st->gate_ctrl(fe, 1);
102 } else {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -0300103#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300104 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -0300105#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300106 up(&st->pll_mutex);
107 }
108 return status;
109}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300110
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300111static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300112 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300113 .refclock = 36125000,
114};
115
116/* check for mutex FIXME */
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300117static int az6007_read(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300118 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300119{
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300120 int ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300121
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300122 ret = usb_control_msg(udev,
123 usb_rcvctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300124 req,
125 USB_TYPE_VENDOR | USB_DIR_IN,
126 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300127 if (ret < 0) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300128 warn("usb read operation failed. (%d)", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300129 return -EIO;
130 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300131
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300132 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
133 index);
134 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300135
136 return ret;
137}
138
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300139static int az6007_write(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300140 u16 index, u8 *b, int blen)
141{
142 int ret;
143
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300144 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
145 index);
146 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300147
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300148 if (blen > 64) {
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300149 err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
150 blen);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300151 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300152 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300153
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300154 ret = usb_control_msg(udev,
155 usb_sndctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300156 req,
157 USB_TYPE_VENDOR | USB_DIR_OUT,
158 value, index, b, blen, 5000);
159 if (ret != blen) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300160 err("usb write operation failed. (%d)", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300161 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300162 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300163
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300164 return 0;
165}
166
167static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
168{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300169 deb_info("%s: %s", __func__, onoff ? "enable" : "disable");
170
171 return az6007_write(adap->dev->udev, 0xbc, onoff, 0, NULL, 0);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300172}
173
174/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300175static struct rc_map_table rc_map_az6007_table[] = {
176 {0x0001, KEY_1},
177 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300178};
179
180/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300181static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300182{
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300183 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300184 u8 key[10];
185 int i;
186
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300187 /*
188 * FIXME: remove the following return to enabled remote querying
189 * The driver likely needs proper locking to avoid troubles between
190 * this call and other concurrent calls.
191 */
192 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300193
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300194 az6007_read(d->udev, AZ6007_READ_IR, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300195
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300196 if (key[1] == 0x44) {
197 *state = REMOTE_NO_KEY_PRESSED;
198 return 0;
199 }
200
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300201 /*
202 * FIXME: need to make something useful with the keycodes and to
203 * convert it to the non-legacy mode. Yet, it is producing some
204 * debug info already, like:
205 * 88 04 eb 02 fd ff 00 82 63 82 (terratec IR)
206 * 88 04 eb 03 fc 00 00 82 63 82 (terratec IR)
207 * 88 80 7e 0d f2 ff 00 82 63 82 (another NEC-extended based IR)
208 * I suspect that the IR data is at bytes 1 to 4, and byte 5 is parity
209 */
210 deb_rc("remote query key: %x %d\n", key[1], key[1]);
211 print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, key, 10);
212
213 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
214 if (rc5_custom(&keymap[i]) == key[1]) {
215 *event = keymap[i].keycode;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300216 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300217
218 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300219 }
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300220 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300221 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300222}
223
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300224#if 0
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300225int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
226{
227 u8 v = onoff;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300228 return az6007_write(d->udev, AZ6007_POWER, v , 3, NULL, 1);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300229}
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300230#endif
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300231
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300232static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300233{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300234 int ret;
235 ret = az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300236
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300237 if (ret > 0)
238 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
239 __func__, mac[0], mac[1], mac[2],
240 mac[3], mac[4], mac[5]);
241
242 return ret;
243}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300244
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300245static int az6007_led_on_off(struct usb_interface *intf, int onoff)
246{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300247 struct usb_device *udev = interface_to_usbdev(intf);
248 int ret;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300249 /* TS through */
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300250 ret = az6007_write(udev, AZ6007_POWER, onoff, 0, NULL, 0);
251 if (ret < 0)
252 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300253 return ret;
254}
255
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 Chehabcaa1a702011-07-22 10:31:25 -0300260 BUG_ON(!st);
261
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 Chehab6da34702011-07-21 18:31:14 -0300270 /* FIXME: do we need a pll semaphore? */
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300271 sema_init(&st->pll_mutex, 1);
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300272 st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
273 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300274
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300275 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300276}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300277
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300278static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
279{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300280 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300281
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300282 if (st->tuner_attached)
283 return 0;
284
285 st->tuner_attached = true;
286
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300287 deb_info("attaching tuner mt2063");
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300288 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300289 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
290 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300291 if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300292 &az6007_mt2063_config,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300293 &adap->dev->i2c_adap))
294 return -EINVAL;
295
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300296 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
297 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300298
299 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300300}
301
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300302int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
303{
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300304 struct az6007_device_state *st = d->priv;
305 struct usb_device *udev = d->udev;
306 int ret;
307
308 deb_info("%s()\n", __func__);
309
310 if (!st->warm) {
311 u8 data[6];
312
313 az6007_read(udev, FX2_OED, 1, 0, data, 1); /* {0x01} */
314 az6007_read(udev, AZ6007_READ_DATA, 0, 8160, data, 1); /* {0x20} */
315 az6007_read(udev, AZ6007_READ_DATA, 0, 0, data, 5); /* {0x00, 0x00, 0x00, 0x00, 0x0a} */
316 az6007_read(udev, AZ6007_READ_DATA, 0, 4080, data, 6); /* {0x00, 0x08, 0x00, 0x0c, 0x22, 0x38} */
317
318 ret = az6007_write(udev, AZ6007_POWER, 0, 2, NULL, 0);
319 if (ret < 0)
320 return ret;
321 msleep(60);
322 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
323 if (ret < 0)
324 return ret;
325 msleep(100);
326 ret = az6007_write(udev, AZ6007_POWER, 1, 3, NULL, 0);
327 if (ret < 0)
328 return ret;
329 msleep(20);
330 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
331 if (ret < 0)
332 return ret;
333
334 msleep(400);
335 ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
336 if (ret < 0)
337 return ret;
338 msleep (150);
339 ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
340 if (ret < 0)
341 return ret;
342 msleep (430);
343 ret = az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
344 if (ret < 0)
345 return ret;
346
347 st->warm = true;
348
349 return 0;
350 }
351
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300352 if (!onoff)
353 return 0;
354
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300355 az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
356 az6007_write(udev, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300357
358#if 0
359 // Seems to be a poweroff sequence
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300360 az6007_write(udev, 0xbc, 1, 3, NULL, 0);
361 az6007_write(udev, 0xbc, 1, 4, NULL, 0);
362 az6007_write(udev, 0xc0, 0, 3, NULL, 0);
363 az6007_write(udev, 0xc0, 1, 3, NULL, 0);
364 az6007_write(udev, 0xbc, 0, 1, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300365#endif
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300366
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300367 return 0;
368}
369
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300370static struct dvb_usb_device_properties az6007_properties;
371
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300372static void az6007_usb_disconnect(struct usb_interface *intf)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300373{
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300374 dvb_usb_device_exit(intf);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300375}
376
377/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300378static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
379 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300380{
381 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300382 struct az6007_device_state *st = d->priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300383 int i, j, len;
384 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300385 u16 index;
386 u16 value;
387 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300388 u8 req, addr;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300389
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300390 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
391 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300392
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300393 for (i = 0; i < num; i++) {
394 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300395 if (((i + 1) < num)
396 && (msgs[i].len == 1)
397 && (!msgs[i].flags & I2C_M_RD)
398 && (msgs[i + 1].flags & I2C_M_RD)
399 && (msgs[i].addr == msgs[i + 1].addr)) {
400 /*
401 * A write + read xfer for the same address, where
402 * the first xfer has just 1 byte length.
403 * Need to join both into one operation
404 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300405 if (dvb_usb_az6007_debug & 2)
406 printk(KERN_DEBUG
407 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
408 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300409 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300410 index = msgs[i].buf[0];
411 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300412 length = 6 + msgs[i + 1].len;
413 len = msgs[i + 1].len;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300414 ret = az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300415 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300416 if (ret >= len) {
417 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300418 msgs[i + 1].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300419 if (dvb_usb_az6007_debug & 2)
420 printk(KERN_CONT
421 "0x%02x ",
422 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300423 }
424 } else
425 ret = -EIO;
426 i++;
427 } else if (!(msgs[i].flags & I2C_M_RD)) {
428 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300429 if (dvb_usb_az6007_debug & 2)
430 printk(KERN_DEBUG
431 "az6007 I2C xfer write addr=0x%x len=%d: ",
432 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300433 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300434 index = msgs[i].buf[0];
435 value = addr | (1 << 8);
436 length = msgs[i].len - 1;
437 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300438 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300439 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
440 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300441 st->data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300442 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300443 printk(KERN_CONT "0x%02x ", st->data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300444 }
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300445 ret = az6007_write(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300446 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300447 } else {
448 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300449 if (dvb_usb_az6007_debug & 2)
450 printk(KERN_DEBUG
451 "az6007 I2C xfer read addr=0x%x len=%d: ",
452 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300453 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300454 index = msgs[i].buf[0];
455 value = addr;
456 length = msgs[i].len + 6;
457 len = msgs[i].len;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300458 ret = az6007_read(d->udev, req, value, index, st->data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300459 length);
460 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300461 msgs[i].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300462 if (dvb_usb_az6007_debug & 2)
463 printk(KERN_CONT
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300464 "0x%02x ", st->data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300465 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300466 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300467 if (dvb_usb_az6007_debug & 2)
468 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300469 if (ret < 0)
470 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300471 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300472err:
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300473 mutex_unlock(&d->i2c_mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300474
475 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300476 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300477 return ret;
478 }
479 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300480}
481
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300482static u32 az6007_i2c_func(struct i2c_adapter *adapter)
483{
484 return I2C_FUNC_I2C;
485}
486
487static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300488 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300489 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300490};
491
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300492int az6007_identify_state(struct usb_device *udev,
493 struct dvb_usb_device_properties *props,
494 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300495{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300496 int ret;
497 u8 mac[6];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300498
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300499 /* Try to read the mac address */
500 ret = az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
501 if (ret == 6)
502 *cold = 0;
503 else
504 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300505
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300506 if (*cold) {
507 az6007_write(udev, 0x09, 1, 0, NULL, 0);
508 az6007_write(udev, 0x00, 0, 0, NULL, 0);
509 az6007_write(udev, 0x00, 0, 0, NULL, 0);
510 }
511
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300512 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300513 return 0;
514}
515
516static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300517 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300518{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300519 az6007_led_on_off(intf, 0);
520
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300521 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300522 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300523}
524
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300525static struct usb_device_id az6007_usb_table[] = {
526 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
527 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
528 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300529};
530
531MODULE_DEVICE_TABLE(usb, az6007_usb_table);
532
533static struct dvb_usb_device_properties az6007_properties = {
534 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
535 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300536 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300537 .no_reconnect = 1,
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300538 .size_of_priv = sizeof(struct az6007_device_state),
539 .identify_state = az6007_identify_state,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300540 .num_adapters = 1,
541 .adapter = {
542 {
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300543 .num_frontends = 1,
544 .fe = {{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300545 .streaming_ctrl = az6007_streaming_ctrl,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300546 .tuner_attach = az6007_tuner_attach,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300547 .frontend_attach = az6007_frontend_attach,
548
549 /* parameter for the MPEG2-data transfer */
550 .stream = {
551 .type = USB_BULK,
552 .count = 10,
553 .endpoint = 0x02,
554 .u = {
555 .bulk = {
556 .buffersize = 4096,
557 }
558 }
559 },
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300560 }}
561 } },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300562 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300563 .read_mac_address = az6007_read_mac_addr,
564
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300565 .rc.legacy = {
566 .rc_map_table = rc_map_az6007_table,
567 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
568 .rc_interval = 400,
569 .rc_query = az6007_rc_query,
570 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300571 .i2c_algo = &az6007_i2c_algo,
572
573 .num_device_descs = 2,
574 .devices = {
575 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
576 .cold_ids = { &az6007_usb_table[0], NULL },
577 .warm_ids = { NULL },
578 },
579 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
580 .cold_ids = { &az6007_usb_table[1], NULL },
581 .warm_ids = { NULL },
582 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300583 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300584 }
585};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300586
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300587/* usb specific object needed to register this driver with the usb subsystem */
588static struct usb_driver az6007_usb_driver = {
589 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300590 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300591 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300592 /* .disconnect = az6007_usb_disconnect, */
593 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300594};
595
596/* module stuff */
597static int __init az6007_usb_module_init(void)
598{
599 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300600 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300601
602 result = usb_register(&az6007_usb_driver);
603 if (result) {
604 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300605 return result;
606 }
607
608 return 0;
609}
610
611static void __exit az6007_usb_module_exit(void)
612{
613 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300614 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300615 usb_deregister(&az6007_usb_driver);
616}
617
618module_init(az6007_usb_module_init);
619module_exit(az6007_usb_module_exit);
620
621MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
622MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300623MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300624MODULE_LICENSE("GPL");