blob: bf8d20151b05e4482d1eec2171a7b13aa79bb256 [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;
57 u8 power_state;
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 Chehab71d67632011-07-21 17:46:41 -030063};
64
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030065static struct drxk_config terratec_h7_drxk = {
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030066 .adr = 0x29,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030067 .parallel_ts = true,
68 .dynamic_clk = true,
69 .single_master = true,
70 .no_i2c_bridge = false,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -030071 .chunk_size = 64,
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -030072 .mpeg_out_clk_strength = 0x02,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030073 .microcode_name = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030074};
75
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030076static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
77{
78 struct dvb_usb_adapter *adap = fe->sec_priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030079 struct az6007_device_state *st;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030080 int status = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030081
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030082 deb_info("%s: %s\n", __func__, enable ? "enable" : "disable");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030083
84 if (!adap)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030085 return -EINVAL;
86
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -030087 st = adap->dev->priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030088
89 if (!st)
90 return -EINVAL;
91
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030092 if (enable) {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030093#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030094 down(&st->pll_mutex);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030095#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030096 status = st->gate_ctrl(fe, 1);
97 } else {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030098#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030099 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -0300100#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300101 up(&st->pll_mutex);
102 }
103 return status;
104}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300105
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300106static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300107 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300108 .refclock = 36125000,
109};
110
111/* check for mutex FIXME */
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300112static int az6007_read(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300113 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300114{
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300115 int ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300116
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300117 ret = usb_control_msg(udev,
118 usb_rcvctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300119 req,
120 USB_TYPE_VENDOR | USB_DIR_IN,
121 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300122 if (ret < 0) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300123 warn("usb read operation failed. (%d)", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300124 return -EIO;
125 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300126
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300127 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
128 index);
129 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300130
131 return ret;
132}
133
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300134static int az6007_write(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300135 u16 index, u8 *b, int blen)
136{
137 int ret;
138
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300139 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
140 index);
141 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300142
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300143 if (blen > 64) {
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300144 err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
145 blen);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300146 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300147 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300148
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300149 ret = usb_control_msg(udev,
150 usb_sndctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300151 req,
152 USB_TYPE_VENDOR | USB_DIR_OUT,
153 value, index, b, blen, 5000);
154 if (ret != blen) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300155 err("usb write operation failed. (%d)", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300156 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300157 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300158
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300159 return 0;
160}
161
162static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
163{
164 return 0;
165}
166
167/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300168static struct rc_map_table rc_map_az6007_table[] = {
169 {0x0001, KEY_1},
170 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300171};
172
173/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300174static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300175{
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300176 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300177 u8 key[10];
178 int i;
179
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300180 /*
181 * FIXME: remove the following return to enabled remote querying
182 * The driver likely needs proper locking to avoid troubles between
183 * this call and other concurrent calls.
184 */
185 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300186
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300187 az6007_read(d->udev, AZ6007_READ_IR, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300188
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300189 if (key[1] == 0x44) {
190 *state = REMOTE_NO_KEY_PRESSED;
191 return 0;
192 }
193
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300194 /*
195 * FIXME: need to make something useful with the keycodes and to
196 * convert it to the non-legacy mode. Yet, it is producing some
197 * debug info already, like:
198 * 88 04 eb 02 fd ff 00 82 63 82 (terratec IR)
199 * 88 04 eb 03 fc 00 00 82 63 82 (terratec IR)
200 * 88 80 7e 0d f2 ff 00 82 63 82 (another NEC-extended based IR)
201 * I suspect that the IR data is at bytes 1 to 4, and byte 5 is parity
202 */
203 deb_rc("remote query key: %x %d\n", key[1], key[1]);
204 print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, key, 10);
205
206 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
207 if (rc5_custom(&keymap[i]) == key[1]) {
208 *event = keymap[i].keycode;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300209 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300210
211 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300212 }
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300213 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300214 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300215}
216
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300217#if 0
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300218int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
219{
220 u8 v = onoff;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300221 return az6007_write(d->udev, AZ6007_POWER, v , 3, NULL, 1);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300222}
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300223#endif
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300224
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300225static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300226{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300227 int ret;
228 ret = az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300229
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300230 if (ret > 0)
231 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
232 __func__, mac[0], mac[1], mac[2],
233 mac[3], mac[4], mac[5]);
234
235 return ret;
236}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300237
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300238static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
239{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300240 int ret;
241 struct usb_device *udev = adap->dev->udev;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300242
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300243 deb_info("%s: adap=%p adap->dev=%p\n", __func__, adap, adap->dev);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300244
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300245 ret = az6007_write(udev, AZ6007_POWER, 0, 2, NULL, 0);
246 if (ret < 0)
247 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300248 msleep(150);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300249 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
250 if (ret < 0)
251 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300252 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300253 ret = az6007_write(udev, AZ6007_POWER, 1, 3, NULL, 0);
254 if (ret < 0)
255 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300256 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300257 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
258 if (ret < 0)
259 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300260 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300261 ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
262 if (ret < 0)
263 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300264 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300265 ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
266 if (ret < 0)
267 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300268 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300269 ret = az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300270
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300271error:
272 if (ret < 0)
273 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300274
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300275 return ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300276}
277
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300278static int az6007_led_on_off(struct usb_interface *intf, int onoff)
279{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300280 struct usb_device *udev = interface_to_usbdev(intf);
281 int ret;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300282 /* TS through */
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300283 ret = az6007_write(udev, AZ6007_POWER, onoff, 0, NULL, 0);
284 if (ret < 0)
285 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300286 return ret;
287}
288
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300289static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
290{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300291 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300292
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300293 BUG_ON(!st);
294
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300295 az6007_frontend_poweron(adap);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300296
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300297 info("attaching demod drxk");
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300298 adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
299 &adap->dev->i2c_adap);
300 if (!adap->fe_adap[0].fe)
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300301 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300302
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300303 adap->fe_adap[0].fe->sec_priv = adap;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300304 /* FIXME: do we need a pll semaphore? */
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300305 sema_init(&st->pll_mutex, 1);
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300306 st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
307 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300308
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300309 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300310}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300311
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300312static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
313{
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300314 struct az6007_device_state *st = adap->dev->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300315
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300316 if (st->tuner_attached)
317 return 0;
318
319 st->tuner_attached = true;
320
321 info("attaching tuner mt2063");
322 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300323 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
324 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
325 if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
326 &az6007_mt2063_config,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300327 &adap->dev->i2c_adap))
328 return -EINVAL;
329
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300330 if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
331 adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300332
333 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300334}
335
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300336int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
337{
338 if (!onoff)
339 return 0;
340
341
342 info("Sending poweron sequence");
343
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300344 az6007_write(d->udev, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300345
346#if 0
347 // Seems to be a poweroff sequence
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300348 az6007_write(d->udev, 0xbc, 1, 3, NULL, 0);
349 az6007_write(d->udev, 0xbc, 1, 4, NULL, 0);
350 az6007_write(d->udev, 0xc0, 0, 3, NULL, 0);
351 az6007_write(d->udev, 0xc0, 1, 3, NULL, 0);
352 az6007_write(d->udev, 0xbc, 0, 1, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300353#endif
354 return 0;
355}
356
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300357static struct dvb_usb_device_properties az6007_properties;
358
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300359static void az6007_usb_disconnect(struct usb_interface *intf)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300360{
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300361 dvb_usb_device_exit(intf);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300362}
363
364/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300365static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
366 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300367{
368 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300369 int i, j, len;
370 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300371 u16 index;
372 u16 value;
373 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300374 u8 req, addr;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300375 u8 data[512];
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300376
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300377 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
378 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300379
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300380 for (i = 0; i < num; i++) {
381 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300382 if (((i + 1) < num)
383 && (msgs[i].len == 1)
384 && (!msgs[i].flags & I2C_M_RD)
385 && (msgs[i + 1].flags & I2C_M_RD)
386 && (msgs[i].addr == msgs[i + 1].addr)) {
387 /*
388 * A write + read xfer for the same address, where
389 * the first xfer has just 1 byte length.
390 * Need to join both into one operation
391 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300392 if (dvb_usb_az6007_debug & 2)
393 printk(KERN_DEBUG
394 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
395 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300396 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300397 index = msgs[i].buf[0];
398 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300399 length = 6 + msgs[i + 1].len;
400 len = msgs[i + 1].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300401 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300402 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300403 if (ret >= len) {
404 for (j = 0; j < len; j++) {
405 msgs[i + 1].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300406 if (dvb_usb_az6007_debug & 2)
407 printk(KERN_CONT
408 "0x%02x ",
409 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300410 }
411 } else
412 ret = -EIO;
413 i++;
414 } else if (!(msgs[i].flags & I2C_M_RD)) {
415 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300416 if (dvb_usb_az6007_debug & 2)
417 printk(KERN_DEBUG
418 "az6007 I2C xfer write addr=0x%x len=%d: ",
419 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300420 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300421 index = msgs[i].buf[0];
422 value = addr | (1 << 8);
423 length = msgs[i].len - 1;
424 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300425 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300426 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
427 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300428 data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300429 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300430 printk(KERN_CONT "0x%02x ", data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300431 }
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300432 ret = az6007_write(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300433 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300434 } else {
435 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300436 if (dvb_usb_az6007_debug & 2)
437 printk(KERN_DEBUG
438 "az6007 I2C xfer read addr=0x%x len=%d: ",
439 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300440 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300441 index = msgs[i].buf[0];
442 value = addr;
443 length = msgs[i].len + 6;
444 len = msgs[i].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300445 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300446 length);
447 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300448 msgs[i].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300449 if (dvb_usb_az6007_debug & 2)
450 printk(KERN_CONT
451 "0x%02x ", data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300452 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300453 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300454 if (dvb_usb_az6007_debug & 2)
455 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300456 if (ret < 0)
457 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300458 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300459err:
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300460 mutex_unlock(&d->i2c_mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300461
462 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300463 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300464 return ret;
465 }
466 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300467}
468
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300469static u32 az6007_i2c_func(struct i2c_adapter *adapter)
470{
471 return I2C_FUNC_I2C;
472}
473
474static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300475 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300476 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300477};
478
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300479int az6007_identify_state(struct usb_device *udev,
480 struct dvb_usb_device_properties *props,
481 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300482{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300483 int ret;
484 u8 mac[6];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300485
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300486 /* Try to read the mac address */
487 ret = az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
488 if (ret == 6)
489 *cold = 0;
490 else
491 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300492
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300493 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300494 return 0;
495}
496
497static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300498 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300499{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300500 az6007_led_on_off(intf, 0);
501
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300502 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300503 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300504}
505
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300506static struct usb_device_id az6007_usb_table[] = {
507 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
508 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
509 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300510};
511
512MODULE_DEVICE_TABLE(usb, az6007_usb_table);
513
514static struct dvb_usb_device_properties az6007_properties = {
515 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
516 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300517 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300518 .no_reconnect = 1,
Mauro Carvalho Chehab978c26c2012-01-16 20:37:13 -0300519 .size_of_priv = sizeof(struct az6007_device_state),
520 .identify_state = az6007_identify_state,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300521 .num_adapters = 1,
522 .adapter = {
523 {
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300524 .num_frontends = 1,
525 .fe = {{
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300526 .streaming_ctrl = az6007_streaming_ctrl,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300527 .tuner_attach = az6007_tuner_attach,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300528 .frontend_attach = az6007_frontend_attach,
529
530 /* parameter for the MPEG2-data transfer */
531 .stream = {
532 .type = USB_BULK,
533 .count = 10,
534 .endpoint = 0x02,
535 .u = {
536 .bulk = {
537 .buffersize = 4096,
538 }
539 }
540 },
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300541 }}
542 } },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300543 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300544 .read_mac_address = az6007_read_mac_addr,
545
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300546 .rc.legacy = {
547 .rc_map_table = rc_map_az6007_table,
548 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
549 .rc_interval = 400,
550 .rc_query = az6007_rc_query,
551 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300552 .i2c_algo = &az6007_i2c_algo,
553
554 .num_device_descs = 2,
555 .devices = {
556 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
557 .cold_ids = { &az6007_usb_table[0], NULL },
558 .warm_ids = { NULL },
559 },
560 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
561 .cold_ids = { &az6007_usb_table[1], NULL },
562 .warm_ids = { NULL },
563 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300564 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300565 }
566};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300567
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300568/* usb specific object needed to register this driver with the usb subsystem */
569static struct usb_driver az6007_usb_driver = {
570 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300571 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300572 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300573 /* .disconnect = az6007_usb_disconnect, */
574 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300575};
576
577/* module stuff */
578static int __init az6007_usb_module_init(void)
579{
580 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300581 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300582
583 result = usb_register(&az6007_usb_driver);
584 if (result) {
585 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300586 return result;
587 }
588
589 return 0;
590}
591
592static void __exit az6007_usb_module_exit(void)
593{
594 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300595 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300596 usb_deregister(&az6007_usb_driver);
597}
598
599module_init(az6007_usb_module_init);
600module_exit(az6007_usb_module_exit);
601
602MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
603MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300604MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300605MODULE_LICENSE("GPL");