blob: 92ded30b2cc61f6eacd103aa45b9c290607e4313 [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,
67 .single_master = 1,
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -030068 .no_i2c_bridge = 0,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -030069 .chunk_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{
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300113 int ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300114
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 if (ret < 0) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300121 warn("usb read operation failed. (%d)", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300122 return -EIO;
123 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300124
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300125 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
126 index);
127 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300128
129 return ret;
130}
131
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300132static int az6007_write(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300133 u16 index, u8 *b, int blen)
134{
135 int ret;
136
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300137 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
138 index);
139 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300140
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300141 if (blen > 64) {
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300142 err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
143 blen);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300144 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300145 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300146
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300147 ret = usb_control_msg(udev,
148 usb_sndctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300149 req,
150 USB_TYPE_VENDOR | USB_DIR_OUT,
151 value, index, b, blen, 5000);
152 if (ret != blen) {
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -0300153 err("usb write operation failed. (%d)", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300154 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300155 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300156
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300157 return 0;
158}
159
160static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
161{
162 return 0;
163}
164
165/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300166static struct rc_map_table rc_map_az6007_table[] = {
167 {0x0001, KEY_1},
168 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300169};
170
171/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300172static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300173{
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300174 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300175 u8 key[10];
176 int i;
177
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300178 /*
179 * FIXME: remove the following return to enabled remote querying
180 * The driver likely needs proper locking to avoid troubles between
181 * this call and other concurrent calls.
182 */
183 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300184
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300185 az6007_read(d->udev, AZ6007_READ_IR, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300186
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300187 if (key[1] == 0x44) {
188 *state = REMOTE_NO_KEY_PRESSED;
189 return 0;
190 }
191
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300192 /*
193 * FIXME: need to make something useful with the keycodes and to
194 * convert it to the non-legacy mode. Yet, it is producing some
195 * debug info already, like:
196 * 88 04 eb 02 fd ff 00 82 63 82 (terratec IR)
197 * 88 04 eb 03 fc 00 00 82 63 82 (terratec IR)
198 * 88 80 7e 0d f2 ff 00 82 63 82 (another NEC-extended based IR)
199 * I suspect that the IR data is at bytes 1 to 4, and byte 5 is parity
200 */
201 deb_rc("remote query key: %x %d\n", key[1], key[1]);
202 print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, key, 10);
203
204 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
205 if (rc5_custom(&keymap[i]) == key[1]) {
206 *event = keymap[i].keycode;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300207 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300208
209 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300210 }
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300211 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300212 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300213}
214
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300215#if 0
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300216int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
217{
218 u8 v = onoff;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300219 return az6007_write(d->udev, AZ6007_POWER, v , 3, NULL, 1);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300220}
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300221#endif
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300222
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300223static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300224{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300225 int ret;
226 ret = az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300227
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300228 if (ret > 0)
229 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
230 __func__, mac[0], mac[1], mac[2],
231 mac[3], mac[4], mac[5]);
232
233 return ret;
234}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300235
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300236static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
237{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300238 int ret;
239 struct usb_device *udev = adap->dev->udev;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300240
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300241 deb_info("%s: adap=%p adap->dev=%p\n", __func__, adap, adap->dev);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300242
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300243 ret = az6007_write(udev, AZ6007_POWER, 0, 2, NULL, 0);
244 if (ret < 0)
245 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300246 msleep(150);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300247 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
248 if (ret < 0)
249 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300250 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300251 ret = az6007_write(udev, AZ6007_POWER, 1, 3, NULL, 0);
252 if (ret < 0)
253 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300254 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300255 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
256 if (ret < 0)
257 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300258 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300259 ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
260 if (ret < 0)
261 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300262 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300263 ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
264 if (ret < 0)
265 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300266 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300267 ret = az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300268
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300269error:
270 if (ret < 0)
271 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300272
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300273 return ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300274}
275
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300276static int az6007_led_on_off(struct usb_interface *intf, int onoff)
277{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300278 struct usb_device *udev = interface_to_usbdev(intf);
279 int ret;
280
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300281 /* TS through */
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300282 ret = az6007_write(udev, AZ6007_POWER, onoff, 0, NULL, 0);
283 if (ret < 0)
284 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300285
286 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 Chehab6da34702011-07-21 18:31:14 -0300291 struct az6007_device_state *st = adap->priv;
292
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{
314 struct az6007_device_state *st = adap->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,
519
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300520 .identify_state = az6007_identify_state,
521 .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 Chehabcaa1a702011-07-22 10:31:25 -0300541 .size_of_priv = sizeof(struct az6007_device_state),
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300542 }}
543 } },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300544 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300545 .read_mac_address = az6007_read_mac_addr,
546
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300547 .rc.legacy = {
548 .rc_map_table = rc_map_az6007_table,
549 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
550 .rc_interval = 400,
551 .rc_query = az6007_rc_query,
552 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300553 .i2c_algo = &az6007_i2c_algo,
554
555 .num_device_descs = 2,
556 .devices = {
557 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
558 .cold_ids = { &az6007_usb_table[0], NULL },
559 .warm_ids = { NULL },
560 },
561 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
562 .cold_ids = { &az6007_usb_table[1], NULL },
563 .warm_ids = { NULL },
564 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300565 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300566 }
567};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300568
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300569/* usb specific object needed to register this driver with the usb subsystem */
570static struct usb_driver az6007_usb_driver = {
571 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300572 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300573 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300574 /* .disconnect = az6007_usb_disconnect, */
575 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300576};
577
578/* module stuff */
579static int __init az6007_usb_module_init(void)
580{
581 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300582 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300583
584 result = usb_register(&az6007_usb_driver);
585 if (result) {
586 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300587 return result;
588 }
589
590 return 0;
591}
592
593static void __exit az6007_usb_module_exit(void)
594{
595 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300596 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300597 usb_deregister(&az6007_usb_driver);
598}
599
600module_init(az6007_usb_module_init);
601module_exit(az6007_usb_module_exit);
602
603MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
604MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300605MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300606MODULE_LICENSE("GPL");