blob: 8add81ade08797b3bbc0fa4bd0dbfad7840eec13 [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
52#define AZ6007_READ_IR 0xc5
53
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{
175 return 0;
176#if 0
177 u8 key[10];
178 int i;
179
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300180 /* remove the following return to enabled remote querying */
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300181
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300182 az6007_read(d->udev, AZ6007_READ_IR, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300183
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300184 deb_rc("remote query key: %x %d\n", key[1], key[1]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300185
186 if (key[1] == 0x44) {
187 *state = REMOTE_NO_KEY_PRESSED;
188 return 0;
189 }
190
191 for (i = 0; i < ARRAY_SIZE(az6007_rc_keys); i++)
192 if (az6007_rc_keys[i].custom == key[1]) {
193 *state = REMOTE_KEY_PRESSED;
194 *event = az6007_rc_keys[i].event;
195 break;
196 }
197 return 0;
198#endif
199}
200
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300201#if 0
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300202int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
203{
204 u8 v = onoff;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300205 return az6007_write(d->udev, AZ6007_POWER, v , 3, NULL, 1);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300206}
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300207#endif
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300208
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300209static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300210{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300211 int ret;
212 ret = az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300213
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300214 if (ret > 0)
215 deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
216 __func__, mac[0], mac[1], mac[2],
217 mac[3], mac[4], mac[5]);
218
219 return ret;
220}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300221
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300222static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
223{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300224 int ret;
225 struct usb_device *udev = adap->dev->udev;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300226
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300227 deb_info("%s: adap=%p adap->dev=%p\n", __func__, adap, adap->dev);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300228
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300229 ret = az6007_write(udev, AZ6007_POWER, 0, 2, NULL, 0);
230 if (ret < 0)
231 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300232 msleep(150);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300233 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
234 if (ret < 0)
235 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300236 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300237 ret = az6007_write(udev, AZ6007_POWER, 1, 3, NULL, 0);
238 if (ret < 0)
239 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300240 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300241 ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
242 if (ret < 0)
243 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300244 msleep(100);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300245 ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
246 if (ret < 0)
247 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300248 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300249 ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
250 if (ret < 0)
251 goto error;
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300252 msleep (10);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300253 ret = az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300254
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300255error:
256 if (ret < 0)
257 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300258
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300259 return ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300260}
261
262static int az6007_frontend_reset(struct dvb_usb_adapter *adap)
263{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300264 struct usb_device *udev = adap->dev->udev;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300265 int ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300266
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300267 deb_info("az6007_frontend_reset adap=%p adap->dev=%p\n", adap, adap->dev);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300268
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300269 /* reset demodulator */
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300270 ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
271 if (ret < 0)
272 goto error;
273 msleep(200);
274 ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
275 if (ret < 0)
276 goto error;
277 msleep(200);
278 ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
279 if (ret < 0)
280 goto error;
281 msleep(200);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300282
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300283error:
284 if (ret < 0)
285 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300286
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300287 return ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300288}
289
290static int az6007_led_on_off(struct usb_interface *intf, int onoff)
291{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300292 struct usb_device *udev = interface_to_usbdev(intf);
293 int ret;
294
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300295 /* TS through */
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300296 ret = az6007_write(udev, AZ6007_POWER, onoff, 0, NULL, 0);
297 if (ret < 0)
298 err("%s failed with error %d", __func__, ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300299
300 return ret;
301}
302
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300303static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
304{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300305 struct az6007_device_state *st = adap->priv;
306
307 int result;
308
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300309 BUG_ON(!st);
310
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300311 az6007_frontend_poweron(adap);
312 az6007_frontend_reset(adap);
313
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300314 info("az6007: attaching demod drxk");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300315 adap->fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
316 &adap->dev->i2c_adap, &adap->fe2);
317 if (!adap->fe) {
318 result = -EINVAL;
319 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300320 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300321
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300322 deb_info("Setting hacks\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300323
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300324 /* FIXME: do we need a pll semaphore? */
325 adap->fe->sec_priv = adap;
326 sema_init(&st->pll_mutex, 1);
327 st->gate_ctrl = adap->fe->ops.i2c_gate_ctrl;
328 adap->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
329 adap->fe2->id = 1;
330
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300331 info("az6007: attaching tuner mt2063");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300332 /* Attach mt2063 to DVB-C frontend */
333 if (adap->fe->ops.i2c_gate_ctrl)
334 adap->fe->ops.i2c_gate_ctrl(adap->fe, 1);
335 if (!dvb_attach(mt2063_attach, adap->fe, &az6007_mt2063_config,
336 &adap->dev->i2c_adap)) {
337 result = -EINVAL;
338
339 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300340 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300341 if (adap->fe->ops.i2c_gate_ctrl)
342 adap->fe->ops.i2c_gate_ctrl(adap->fe, 0);
343
344 /* Hack - needed due to drxk */
345 adap->fe2->tuner_priv = adap->fe->tuner_priv;
346 memcpy(&adap->fe2->ops.tuner_ops,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300347 &adap->fe->ops.tuner_ops, sizeof(adap->fe->ops.tuner_ops));
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300348
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300349 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300350
351out_free:
352 if (adap->fe)
353 dvb_frontend_detach(adap->fe);
354 adap->fe = NULL;
355 adap->fe2 = NULL;
356
357 return result;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300358}
359
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300360int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
361{
362 if (!onoff)
363 return 0;
364
365
366 info("Sending poweron sequence");
367
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300368 az6007_write(d->udev, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300369
370#if 0
371 // Seems to be a poweroff sequence
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300372 az6007_write(d->udev, 0xbc, 1, 3, NULL, 0);
373 az6007_write(d->udev, 0xbc, 1, 4, NULL, 0);
374 az6007_write(d->udev, 0xc0, 0, 3, NULL, 0);
375 az6007_write(d->udev, 0xc0, 1, 3, NULL, 0);
376 az6007_write(d->udev, 0xbc, 0, 1, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300377#endif
378 return 0;
379}
380
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300381static struct dvb_usb_device_properties az6007_properties;
382
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300383static void az6007_usb_disconnect(struct usb_interface *intf)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300384{
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300385 dvb_usb_device_exit(intf);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300386}
387
388/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300389static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
390 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300391{
392 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300393 int i, j, len;
394 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300395 u16 index;
396 u16 value;
397 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300398 u8 req, addr;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300399 u8 data[512];
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300400
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300401 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
402 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300403
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300404 for (i = 0; i < num; i++) {
405 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300406 if (((i + 1) < num)
407 && (msgs[i].len == 1)
408 && (!msgs[i].flags & I2C_M_RD)
409 && (msgs[i + 1].flags & I2C_M_RD)
410 && (msgs[i].addr == msgs[i + 1].addr)) {
411 /*
412 * A write + read xfer for the same address, where
413 * the first xfer has just 1 byte length.
414 * Need to join both into one operation
415 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300416 if (dvb_usb_az6007_debug & 2)
417 printk(KERN_DEBUG
418 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
419 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300420 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300421 index = msgs[i].buf[0];
422 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300423 length = 6 + msgs[i + 1].len;
424 len = msgs[i + 1].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300425 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300426 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300427 if (ret >= len) {
428 for (j = 0; j < len; j++) {
429 msgs[i + 1].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300430 if (dvb_usb_az6007_debug & 2)
431 printk(KERN_CONT
432 "0x%02x ",
433 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300434 }
435 } else
436 ret = -EIO;
437 i++;
438 } else if (!(msgs[i].flags & I2C_M_RD)) {
439 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300440 if (dvb_usb_az6007_debug & 2)
441 printk(KERN_DEBUG
442 "az6007 I2C xfer write addr=0x%x len=%d: ",
443 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300444 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300445 index = msgs[i].buf[0];
446 value = addr | (1 << 8);
447 length = msgs[i].len - 1;
448 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300449 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300450 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
451 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300452 data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300453 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300454 printk(KERN_CONT "0x%02x ", data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300455 }
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300456 ret = az6007_write(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300457 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300458 } else {
459 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300460 if (dvb_usb_az6007_debug & 2)
461 printk(KERN_DEBUG
462 "az6007 I2C xfer read addr=0x%x len=%d: ",
463 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300464 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300465 index = msgs[i].buf[0];
466 value = addr;
467 length = msgs[i].len + 6;
468 len = msgs[i].len;
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300469 ret = az6007_read(d->udev, req, value, index, data,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300470 length);
471 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300472 msgs[i].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300473 if (dvb_usb_az6007_debug & 2)
474 printk(KERN_CONT
475 "0x%02x ", data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300476 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300477 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300478 if (dvb_usb_az6007_debug & 2)
479 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300480 if (ret < 0)
481 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300482 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300483err:
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300484 mutex_unlock(&d->i2c_mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300485
486 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300487 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300488 return ret;
489 }
490 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300491}
492
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300493static u32 az6007_i2c_func(struct i2c_adapter *adapter)
494{
495 return I2C_FUNC_I2C;
496}
497
498static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300499 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300500 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300501};
502
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300503int az6007_identify_state(struct usb_device *udev,
504 struct dvb_usb_device_properties *props,
505 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300506{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300507 int ret;
508 u8 mac[6];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300509
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300510 /* Try to read the mac address */
511 ret = az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
512 if (ret == 6)
513 *cold = 0;
514 else
515 *cold = 1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300516
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300517 deb_info("Device is on %s state\n", *cold? "warm" : "cold");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300518 return 0;
519}
520
521static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300522 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300523{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300524 az6007_led_on_off(intf, 0);
525
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300526 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300527 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300528}
529
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300530static struct usb_device_id az6007_usb_table[] = {
531 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
532 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
533 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300534};
535
536MODULE_DEVICE_TABLE(usb, az6007_usb_table);
537
538static struct dvb_usb_device_properties az6007_properties = {
539 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
540 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300541 .firmware = "dvb-usb-terratec-h7-az6007.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300542 .no_reconnect = 1,
543
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300544 .identify_state = az6007_identify_state,
545 .num_adapters = 1,
546 .adapter = {
547 {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300548 .streaming_ctrl = az6007_streaming_ctrl,
549 .frontend_attach = az6007_frontend_attach,
550
551 /* parameter for the MPEG2-data transfer */
552 .stream = {
553 .type = USB_BULK,
554 .count = 10,
555 .endpoint = 0x02,
556 .u = {
557 .bulk = {
558 .buffersize = 4096,
559 }
560 }
561 },
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300562 .size_of_priv = sizeof(struct az6007_device_state),
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300563 }
564 },
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300565 .power_ctrl = az6007_power_ctrl,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300566 .read_mac_address = az6007_read_mac_addr,
567
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300568 .rc.legacy = {
569 .rc_map_table = rc_map_az6007_table,
570 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
571 .rc_interval = 400,
572 .rc_query = az6007_rc_query,
573 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300574 .i2c_algo = &az6007_i2c_algo,
575
576 .num_device_descs = 2,
577 .devices = {
578 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
579 .cold_ids = { &az6007_usb_table[0], NULL },
580 .warm_ids = { NULL },
581 },
582 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
583 .cold_ids = { &az6007_usb_table[1], NULL },
584 .warm_ids = { NULL },
585 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300586 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300587 }
588};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300589
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300590/* usb specific object needed to register this driver with the usb subsystem */
591static struct usb_driver az6007_usb_driver = {
592 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300593 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300594 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300595 /* .disconnect = az6007_usb_disconnect, */
596 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300597};
598
599/* module stuff */
600static int __init az6007_usb_module_init(void)
601{
602 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300603 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300604
605 result = usb_register(&az6007_usb_driver);
606 if (result) {
607 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300608 return result;
609 }
610
611 return 0;
612}
613
614static void __exit az6007_usb_module_exit(void)
615{
616 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300617 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300618 usb_deregister(&az6007_usb_driver);
619}
620
621module_init(az6007_usb_module_init);
622module_exit(az6007_usb_module_exit);
623
624MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
625MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300626MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300627MODULE_LICENSE("GPL");