blob: c9743ee2ba75ab9ec7ff76e2f81bfbaef128dc76 [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 Chehab6da34702011-07-21 18:31:14 -030062 bool dont_attach_fe1;
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,
67 .single_master = 1,
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -030068 .no_i2c_bridge = 0,
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -030069 .max_size = 64,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -030070 .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
71 .parallel_ts = 1,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030072};
73
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030074static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
75{
76 struct dvb_usb_adapter *adap = fe->sec_priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030077 struct az6007_device_state *st;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030078 int status = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030079
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030080 deb_info("%s: %s\n", __func__, enable ? "enable" : "disable");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030081
82 if (!adap)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030083 return -EINVAL;
84
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030085 st = adap->priv;
86
87 if (!st)
88 return -EINVAL;
89
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030090 if (enable) {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030091#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030092 down(&st->pll_mutex);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030093#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030094 status = st->gate_ctrl(fe, 1);
95 } else {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030096#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030097 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030098#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030099 up(&st->pll_mutex);
100 }
101 return status;
102}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300103
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300104static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300105 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300106 .refclock = 36125000,
107};
108
109/* check for mutex FIXME */
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300110static int az6007_read(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300111 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300112{
113 int ret = -1;
114
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300115 ret = usb_control_msg(udev,
116 usb_rcvctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300117 req,
118 USB_TYPE_VENDOR | USB_DIR_IN,
119 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300120
121 if (ret < 0) {
122 warn("usb in 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 Chehabf2ba9e52011-07-23 11:54:40 -0300154 err("usb out 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{
163 return 0;
164}
165
166/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300167static struct rc_map_table rc_map_az6007_table[] = {
168 {0x0001, KEY_1},
169 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300170};
171
172/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300173static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300174{
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300175 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300176 u8 key[10];
177 int i;
178
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300179 /*
180 * FIXME: remove the following return to enabled remote querying
181 * The driver likely needs proper locking to avoid troubles between
182 * this call and other concurrent calls.
183 */
184 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300185
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300186 az6007_read(d->udev, AZ6007_READ_IR, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300187
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300188 if (key[1] == 0x44) {
189 *state = REMOTE_NO_KEY_PRESSED;
190 return 0;
191 }
192
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300193 /*
194 * FIXME: need to make something useful with the keycodes and to
195 * convert it to the non-legacy mode. Yet, it is producing some
196 * debug info already, like:
197 * 88 04 eb 02 fd ff 00 82 63 82 (terratec IR)
198 * 88 04 eb 03 fc 00 00 82 63 82 (terratec IR)
199 * 88 80 7e 0d f2 ff 00 82 63 82 (another NEC-extended based IR)
200 * I suspect that the IR data is at bytes 1 to 4, and byte 5 is parity
201 */
202 deb_rc("remote query key: %x %d\n", key[1], key[1]);
203 print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, key, 10);
204
205 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
206 if (rc5_custom(&keymap[i]) == key[1]) {
207 *event = keymap[i].keycode;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300208 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300209
210 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300211 }
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300212 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300213 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300214}
215
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300216#if 0
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300217int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
218{
219 u8 v = onoff;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300220 return az6007_write(d->udev, AZ6007_POWER, v , 3, NULL, 1);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300221}
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300222#endif
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300223
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300224static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300225{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300226 int ret;
227 ret = az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300228
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300229 if (ret > 0)
230 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
231 __func__, mac[0], mac[1], mac[2],
232 mac[3], mac[4], mac[5]);
233
234 return ret;
235}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300236
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300237static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
238{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300239 int ret;
240 struct usb_device *udev = adap->dev->udev;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300241
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300242 deb_info("%s: adap=%p adap->dev=%p\n", __func__, adap, adap->dev);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300243
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300244 ret = az6007_write(udev, AZ6007_POWER, 0, 2, NULL, 0);
245 if (ret < 0)
246 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300247 msleep(150);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300248 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
249 if (ret < 0)
250 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300251 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300252 ret = az6007_write(udev, AZ6007_POWER, 1, 3, NULL, 0);
253 if (ret < 0)
254 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300255 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300256 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
257 if (ret < 0)
258 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300259 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300260 ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
261 if (ret < 0)
262 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300263 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300264 ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
265 if (ret < 0)
266 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300267 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300268 ret = az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300269
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300270error:
271 if (ret < 0)
272 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300273
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300274 return ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300275}
276
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300277static int az6007_led_on_off(struct usb_interface *intf, int onoff)
278{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300279 struct usb_device *udev = interface_to_usbdev(intf);
280 int ret;
281
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
287 return ret;
288}
289
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300290static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
291{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300292 struct az6007_device_state *st = adap->priv;
293
294 int result;
295
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300296 BUG_ON(!st);
297
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300298 az6007_frontend_poweron(adap);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300299
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300300 info("az6007: attaching demod drxk");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300301 adap->fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
302 &adap->dev->i2c_adap, &adap->fe2);
303 if (!adap->fe) {
304 result = -EINVAL;
305 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300306 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300307
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300308 deb_info("Setting hacks\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300309
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300310 /* FIXME: do we need a pll semaphore? */
311 adap->fe->sec_priv = adap;
312 sema_init(&st->pll_mutex, 1);
313 st->gate_ctrl = adap->fe->ops.i2c_gate_ctrl;
314 adap->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
315 adap->fe2->id = 1;
316
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300317 info("az6007: attaching tuner mt2063");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300318 /* Attach mt2063 to DVB-C frontend */
319 if (adap->fe->ops.i2c_gate_ctrl)
320 adap->fe->ops.i2c_gate_ctrl(adap->fe, 1);
321 if (!dvb_attach(mt2063_attach, adap->fe, &az6007_mt2063_config,
322 &adap->dev->i2c_adap)) {
323 result = -EINVAL;
324
325 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300326 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300327 if (adap->fe->ops.i2c_gate_ctrl)
328 adap->fe->ops.i2c_gate_ctrl(adap->fe, 0);
329
330 /* Hack - needed due to drxk */
331 adap->fe2->tuner_priv = adap->fe->tuner_priv;
332 memcpy(&adap->fe2->ops.tuner_ops,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300333 &adap->fe->ops.tuner_ops, sizeof(adap->fe->ops.tuner_ops));
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300334
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300335 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300336
337out_free:
338 if (adap->fe)
339 dvb_frontend_detach(adap->fe);
340 adap->fe = NULL;
341 adap->fe2 = NULL;
342
343 return result;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300344}
345
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300346int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
347{
348 if (!onoff)
349 return 0;
350
351
352 info("Sending poweron sequence");
353
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300354 az6007_write(d->udev, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300355
356#if 0
357 // Seems to be a poweroff sequence
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300358 az6007_write(d->udev, 0xbc, 1, 3, NULL, 0);
359 az6007_write(d->udev, 0xbc, 1, 4, NULL, 0);
360 az6007_write(d->udev, 0xc0, 0, 3, NULL, 0);
361 az6007_write(d->udev, 0xc0, 1, 3, NULL, 0);
362 az6007_write(d->udev, 0xbc, 0, 1, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300363#endif
364 return 0;
365}
366
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300367static struct dvb_usb_device_properties az6007_properties;
368
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300369static void az6007_usb_disconnect(struct usb_interface *intf)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300370{
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300371 dvb_usb_device_exit(intf);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300372}
373
374/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300375static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
376 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300377{
378 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300379 int i, j, len;
380 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300381 u16 index;
382 u16 value;
383 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300384 u8 req, addr;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300385 u8 data[512];
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300386
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300387 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
388 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300389
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300390 for (i = 0; i < num; i++) {
391 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300392 if (((i + 1) < num)
393 && (msgs[i].len == 1)
394 && (!msgs[i].flags & I2C_M_RD)
395 && (msgs[i + 1].flags & I2C_M_RD)
396 && (msgs[i].addr == msgs[i + 1].addr)) {
397 /*
398 * A write + read xfer for the same address, where
399 * the first xfer has just 1 byte length.
400 * Need to join both into one operation
401 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300402 if (dvb_usb_az6007_debug & 2)
403 printk(KERN_DEBUG
404 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
405 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300406 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300407 index = msgs[i].buf[0];
408 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300409 length = 6 + msgs[i + 1].len;
410 len = msgs[i + 1].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300411 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300412 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300413 if (ret >= len) {
414 for (j = 0; j < len; j++) {
415 msgs[i + 1].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300416 if (dvb_usb_az6007_debug & 2)
417 printk(KERN_CONT
418 "0x%02x ",
419 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300420 }
421 } else
422 ret = -EIO;
423 i++;
424 } else if (!(msgs[i].flags & I2C_M_RD)) {
425 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300426 if (dvb_usb_az6007_debug & 2)
427 printk(KERN_DEBUG
428 "az6007 I2C xfer write addr=0x%x len=%d: ",
429 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300430 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300431 index = msgs[i].buf[0];
432 value = addr | (1 << 8);
433 length = msgs[i].len - 1;
434 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300435 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300436 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
437 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300438 data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300439 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300440 printk(KERN_CONT "0x%02x ", data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300441 }
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300442 ret = az6007_write(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300443 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300444 } else {
445 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300446 if (dvb_usb_az6007_debug & 2)
447 printk(KERN_DEBUG
448 "az6007 I2C xfer read addr=0x%x len=%d: ",
449 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300450 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300451 index = msgs[i].buf[0];
452 value = addr;
453 length = msgs[i].len + 6;
454 len = msgs[i].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300455 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300456 length);
457 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300458 msgs[i].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300459 if (dvb_usb_az6007_debug & 2)
460 printk(KERN_CONT
461 "0x%02x ", data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300462 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300463 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300464 if (dvb_usb_az6007_debug & 2)
465 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300466 if (ret < 0)
467 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300468 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300469err:
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300470 mutex_unlock(&d->i2c_mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300471
472 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300473 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300474 return ret;
475 }
476 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300477}
478
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300479static u32 az6007_i2c_func(struct i2c_adapter *adapter)
480{
481 return I2C_FUNC_I2C;
482}
483
484static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300485 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300486 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300487};
488
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300489int az6007_identify_state(struct usb_device *udev,
490 struct dvb_usb_device_properties *props,
491 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300492{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300493 int ret;
494 u8 mac[6];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300495
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300496 /* Try to read the mac address */
497 ret = az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
498 if (ret == 6)
499 *cold = 0;
500 else
501 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300502
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300503 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300504 return 0;
505}
506
507static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300508 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300509{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300510 az6007_led_on_off(intf, 0);
511
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300512 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300513 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300514}
515
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300516static struct usb_device_id az6007_usb_table[] = {
517 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
518 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
519 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300520};
521
522MODULE_DEVICE_TABLE(usb, az6007_usb_table);
523
524static struct dvb_usb_device_properties az6007_properties = {
525 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
526 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300527 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300528 .no_reconnect = 1,
529
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300530 .identify_state = az6007_identify_state,
531 .num_adapters = 1,
532 .adapter = {
533 {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300534 .streaming_ctrl = az6007_streaming_ctrl,
535 .frontend_attach = az6007_frontend_attach,
536
537 /* parameter for the MPEG2-data transfer */
538 .stream = {
539 .type = USB_BULK,
540 .count = 10,
541 .endpoint = 0x02,
542 .u = {
543 .bulk = {
544 .buffersize = 4096,
545 }
546 }
547 },
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300548 .size_of_priv = sizeof(struct az6007_device_state),
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300549 }
550 },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300551 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300552 .read_mac_address = az6007_read_mac_addr,
553
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300554 .rc.legacy = {
555 .rc_map_table = rc_map_az6007_table,
556 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
557 .rc_interval = 400,
558 .rc_query = az6007_rc_query,
559 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300560 .i2c_algo = &az6007_i2c_algo,
561
562 .num_device_descs = 2,
563 .devices = {
564 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
565 .cold_ids = { &az6007_usb_table[0], NULL },
566 .warm_ids = { NULL },
567 },
568 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
569 .cold_ids = { &az6007_usb_table[1], NULL },
570 .warm_ids = { NULL },
571 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300572 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300573 }
574};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300575
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300576/* usb specific object needed to register this driver with the usb subsystem */
577static struct usb_driver az6007_usb_driver = {
578 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300579 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300580 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300581 /* .disconnect = az6007_usb_disconnect, */
582 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300583};
584
585/* module stuff */
586static int __init az6007_usb_module_init(void)
587{
588 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300589 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300590
591 result = usb_register(&az6007_usb_driver);
592 if (result) {
593 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300594 return result;
595 }
596
597 return 0;
598}
599
600static void __exit az6007_usb_module_exit(void)
601{
602 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300603 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300604 usb_deregister(&az6007_usb_driver);
605}
606
607module_init(az6007_usb_module_init);
608module_exit(az6007_usb_module_exit);
609
610MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
611MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300612MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300613MODULE_LICENSE("GPL");