blob: b667854bdbf03baeb5c4ce249cab8c795e7c8640 [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 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{
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 Chehab1c9a2842011-07-31 10:11:32 -0300293 /* FIXME: dvb-usb will call this function twice! */
294 if (adap->fe[0])
295 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300296
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300297 BUG_ON(!st);
298
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300299 az6007_frontend_poweron(adap);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300300
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300301 info("attaching demod drxk");
302 adap->fe[0] = dvb_attach(drxk_attach, &terratec_h7_drxk,
303 &adap->dev->i2c_adap, &adap->fe[1]);
304 if (!adap->fe[0])
305 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300306
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300307 adap->fe[0]->sec_priv = adap;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300308 /* FIXME: do we need a pll semaphore? */
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300309 sema_init(&st->pll_mutex, 1);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300310 st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
311 adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
312 adap->dont_attach_fe[1] = true;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300313
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300314 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300315}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300316
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300317static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
318{
319 struct az6007_device_state *st = adap->priv;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300320
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300321 if (st->tuner_attached)
322 return 0;
323
324 st->tuner_attached = true;
325
326 info("attaching tuner mt2063");
327 /* Attach mt2063 to DVB-C frontend */
328 if (adap->fe[0]->ops.i2c_gate_ctrl)
329 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 1);
330 if (!dvb_attach(mt2063_attach, adap->fe[0], &az6007_mt2063_config,
331 &adap->dev->i2c_adap))
332 return -EINVAL;
333
334 if (adap->fe[0]->ops.i2c_gate_ctrl)
335 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 0);
336
337 /* Hack - needed due to drxk */
338 adap->fe[1]->tuner_priv = adap->fe[0]->tuner_priv;
339 memcpy(&adap->fe[1]->ops.tuner_ops,
340 &adap->fe[0]->ops.tuner_ops, sizeof(adap->fe[0]->ops.tuner_ops));
341
342 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300343}
344
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300345int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
346{
347 if (!onoff)
348 return 0;
349
350
351 info("Sending poweron sequence");
352
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300353 az6007_write(d->udev, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300354
355#if 0
356 // Seems to be a poweroff sequence
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300357 az6007_write(d->udev, 0xbc, 1, 3, NULL, 0);
358 az6007_write(d->udev, 0xbc, 1, 4, NULL, 0);
359 az6007_write(d->udev, 0xc0, 0, 3, NULL, 0);
360 az6007_write(d->udev, 0xc0, 1, 3, NULL, 0);
361 az6007_write(d->udev, 0xbc, 0, 1, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300362#endif
363 return 0;
364}
365
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300366static struct dvb_usb_device_properties az6007_properties;
367
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300368static void az6007_usb_disconnect(struct usb_interface *intf)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300369{
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300370 dvb_usb_device_exit(intf);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300371}
372
373/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300374static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
375 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300376{
377 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300378 int i, j, len;
379 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300380 u16 index;
381 u16 value;
382 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300383 u8 req, addr;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300384 u8 data[512];
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300385
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300386 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
387 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300388
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300389 for (i = 0; i < num; i++) {
390 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300391 if (((i + 1) < num)
392 && (msgs[i].len == 1)
393 && (!msgs[i].flags & I2C_M_RD)
394 && (msgs[i + 1].flags & I2C_M_RD)
395 && (msgs[i].addr == msgs[i + 1].addr)) {
396 /*
397 * A write + read xfer for the same address, where
398 * the first xfer has just 1 byte length.
399 * Need to join both into one operation
400 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300401 if (dvb_usb_az6007_debug & 2)
402 printk(KERN_DEBUG
403 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
404 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300405 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300406 index = msgs[i].buf[0];
407 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300408 length = 6 + msgs[i + 1].len;
409 len = msgs[i + 1].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300410 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300411 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300412 if (ret >= len) {
413 for (j = 0; j < len; j++) {
414 msgs[i + 1].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300415 if (dvb_usb_az6007_debug & 2)
416 printk(KERN_CONT
417 "0x%02x ",
418 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300419 }
420 } else
421 ret = -EIO;
422 i++;
423 } else if (!(msgs[i].flags & I2C_M_RD)) {
424 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300425 if (dvb_usb_az6007_debug & 2)
426 printk(KERN_DEBUG
427 "az6007 I2C xfer write addr=0x%x len=%d: ",
428 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300429 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300430 index = msgs[i].buf[0];
431 value = addr | (1 << 8);
432 length = msgs[i].len - 1;
433 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300434 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300435 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
436 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300437 data[j] = msgs[i].buf[j + 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 ", data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300440 }
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300441 ret = az6007_write(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300442 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300443 } else {
444 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300445 if (dvb_usb_az6007_debug & 2)
446 printk(KERN_DEBUG
447 "az6007 I2C xfer read addr=0x%x len=%d: ",
448 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300449 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300450 index = msgs[i].buf[0];
451 value = addr;
452 length = msgs[i].len + 6;
453 len = msgs[i].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300454 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300455 length);
456 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300457 msgs[i].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300458 if (dvb_usb_az6007_debug & 2)
459 printk(KERN_CONT
460 "0x%02x ", data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300461 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300462 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300463 if (dvb_usb_az6007_debug & 2)
464 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300465 if (ret < 0)
466 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300467 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300468err:
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300469 mutex_unlock(&d->i2c_mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300470
471 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300472 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300473 return ret;
474 }
475 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300476}
477
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300478static u32 az6007_i2c_func(struct i2c_adapter *adapter)
479{
480 return I2C_FUNC_I2C;
481}
482
483static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300484 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300485 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300486};
487
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300488int az6007_identify_state(struct usb_device *udev,
489 struct dvb_usb_device_properties *props,
490 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300491{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300492 int ret;
493 u8 mac[6];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300494
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300495 /* Try to read the mac address */
496 ret = az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
497 if (ret == 6)
498 *cold = 0;
499 else
500 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300501
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300502 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300503 return 0;
504}
505
506static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300507 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300508{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300509 az6007_led_on_off(intf, 0);
510
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300511 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300512 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300513}
514
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300515static struct usb_device_id az6007_usb_table[] = {
516 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
517 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
518 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300519};
520
521MODULE_DEVICE_TABLE(usb, az6007_usb_table);
522
523static struct dvb_usb_device_properties az6007_properties = {
524 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
525 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300526 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300527 .no_reconnect = 1,
528
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300529 .identify_state = az6007_identify_state,
530 .num_adapters = 1,
531 .adapter = {
532 {
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300533 .num_frontends = 2,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300534 .streaming_ctrl = az6007_streaming_ctrl,
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300535 .tuner_attach = az6007_tuner_attach,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300536 .frontend_attach = az6007_frontend_attach,
537
538 /* parameter for the MPEG2-data transfer */
539 .stream = {
540 .type = USB_BULK,
541 .count = 10,
542 .endpoint = 0x02,
543 .u = {
544 .bulk = {
545 .buffersize = 4096,
546 }
547 }
548 },
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300549 .size_of_priv = sizeof(struct az6007_device_state),
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300550 }
551 },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300552 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300553 .read_mac_address = az6007_read_mac_addr,
554
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300555 .rc.legacy = {
556 .rc_map_table = rc_map_az6007_table,
557 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
558 .rc_interval = 400,
559 .rc_query = az6007_rc_query,
560 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300561 .i2c_algo = &az6007_i2c_algo,
562
563 .num_device_descs = 2,
564 .devices = {
565 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
566 .cold_ids = { &az6007_usb_table[0], NULL },
567 .warm_ids = { NULL },
568 },
569 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
570 .cold_ids = { &az6007_usb_table[1], NULL },
571 .warm_ids = { NULL },
572 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300573 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300574 }
575};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300576
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300577/* usb specific object needed to register this driver with the usb subsystem */
578static struct usb_driver az6007_usb_driver = {
579 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300580 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300581 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300582 /* .disconnect = az6007_usb_disconnect, */
583 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300584};
585
586/* module stuff */
587static int __init az6007_usb_module_init(void)
588{
589 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300590 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300591
592 result = usb_register(&az6007_usb_driver);
593 if (result) {
594 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300595 return result;
596 }
597
598 return 0;
599}
600
601static void __exit az6007_usb_module_exit(void)
602{
603 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300604 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300605 usb_deregister(&az6007_usb_driver);
606}
607
608module_init(az6007_usb_module_init);
609module_exit(az6007_usb_module_exit);
610
611MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
612MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300613MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300614MODULE_LICENSE("GPL");