blob: bb597c67cc9fbcd0e7e48c8a33535b88ab3580d1 [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#include "dvb-usb.h"
27
28#define DVB_USB_LOG_PREFIX "az6007"
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 Chehab71d67632011-07-21 17:46:41 -030043struct az6007_device_state {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030044 struct dvb_ca_en50221 ca;
45 struct mutex ca_mutex;
46 u8 power_state;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030047
48 /* Due to DRX-K - probably need changes */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030049 int (*gate_ctrl) (struct dvb_frontend *, int);
50 struct semaphore pll_mutex;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030051 bool dont_attach_fe1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030052};
53
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030054static struct drxk_config terratec_h7_drxk = {
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030055 .adr = 0x29,
56 .single_master = 1,
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -030057 .no_i2c_bridge = 0,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030058 .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030059};
60
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030061static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
62{
63 struct dvb_usb_adapter *adap = fe->sec_priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030064 struct az6007_device_state *st;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030065 int status = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030066
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030067 deb_info("%s: %s\n", __func__, enable ? "enable" : "disable");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030068
69 if (!adap)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030070 return -EINVAL;
71
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030072 st = adap->priv;
73
74 if (!st)
75 return -EINVAL;
76
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030077 if (enable) {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030078#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030079 down(&st->pll_mutex);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030080#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030081 status = st->gate_ctrl(fe, 1);
82 } else {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030083#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030084 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030085#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030086 up(&st->pll_mutex);
87 }
88 return status;
89}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030090
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030091static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030092 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030093 .refclock = 36125000,
94};
95
96/* check for mutex FIXME */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030097static int az6007_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value,
98 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030099{
100 int ret = -1;
101
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300102 ret = usb_control_msg(d->udev,
103 usb_rcvctrlpipe(d->udev, 0),
104 req,
105 USB_TYPE_VENDOR | USB_DIR_IN,
106 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300107
108 if (ret < 0) {
109 warn("usb in operation failed. (%d)", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300110 return -EIO;
111 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300112
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300113 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
114 index);
115 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300116
117 return ret;
118}
119
120static int az6007_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
121 u16 index, u8 *b, int blen)
122{
123 int ret;
124
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300125 deb_xfer("out: 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
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300129 if (blen > 64) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300130 err("az6007: doesn't suport I2C transactions longer than 64 bytes\n");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300131 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300132 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300133
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300134 ret = usb_control_msg(d->udev,
135 usb_sndctrlpipe(d->udev, 0),
136 req,
137 USB_TYPE_VENDOR | USB_DIR_OUT,
138 value, index, b, blen, 5000);
139 if (ret != blen) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300140 err("usb out operation failed. (%d)", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300141 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300142 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300143
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300144 return 0;
145}
146
147static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
148{
149 return 0;
150}
151
152/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300153static struct rc_map_table rc_map_az6007_table[] = {
154 {0x0001, KEY_1},
155 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300156};
157
158/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300159static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300160{
161 return 0;
162#if 0
163 u8 key[10];
164 int i;
165
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300166 /* remove the following return to enabled remote querying */
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300167
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300168 az6007_usb_in_op(d, READ_REMOTE_REQ, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300169
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300170 deb_rc("remote query key: %x %d\n", key[1], key[1]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300171
172 if (key[1] == 0x44) {
173 *state = REMOTE_NO_KEY_PRESSED;
174 return 0;
175 }
176
177 for (i = 0; i < ARRAY_SIZE(az6007_rc_keys); i++)
178 if (az6007_rc_keys[i].custom == key[1]) {
179 *state = REMOTE_KEY_PRESSED;
180 *event = az6007_rc_keys[i].event;
181 break;
182 }
183 return 0;
184#endif
185}
186
187/*
188int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
189{
190 u8 v = onoff;
191 return az6007_usb_out_op(d,0xBC,v,3,NULL,1);
192}
193*/
194
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300195static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300196{
197 az6007_usb_in_op(d, 0xb7, 6, 0, &mac[0], 6);
198 return 0;
199}
200
201static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
202{
203 int ret;
204 u8 req;
205 u16 value;
206 u16 index;
207 int blen;
208
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300209 deb_info("az6007_frontend_poweron adap=%p adap->dev=%p\n",
210 adap, adap->dev);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300211
212 req = 0xBC;
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300213 value = 1; /* power on */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300214 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300215 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300216
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300217 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
218 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300219 err("az6007_frontend_poweron failed!!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300220 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300221 }
222
223 msleep_interruptible(200);
224
225 req = 0xBC;
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300226 value = 0; /* power off */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300227 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300228 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300229
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300230 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
231 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300232 err("az6007_frontend_poweron failed!!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300233 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300234 }
235
236 msleep_interruptible(200);
237
238 req = 0xBC;
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300239 value = 1; /* power on */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300240 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300241 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300242
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300243 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
244 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300245 err("az6007_frontend_poweron failed!!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300246 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300247 }
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300248 deb_info("az6007_frontend_poweron: OK\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300249
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300250 return 0;
251}
252
253static int az6007_frontend_reset(struct dvb_usb_adapter *adap)
254{
255 int ret;
256 u8 req;
257 u16 value;
258 u16 index;
259 int blen;
260
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300261 deb_info("az6007_frontend_reset adap=%p adap->dev=%p\n", adap, adap->dev);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300262
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300263 /* reset demodulator */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300264 req = 0xC0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300265 value = 1; /* high */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300266 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300267 blen = 0;
268 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
269 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300270 err("az6007_frontend_reset failed 1 !!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300271 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300272 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300273
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300274 req = 0xC0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300275 value = 0; /* low */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300276 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300277 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300278 msleep_interruptible(200);
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300279 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
280 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300281 err("az6007_frontend_reset failed 2 !!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300282 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300283 }
284 msleep_interruptible(200);
285 req = 0xC0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300286 value = 1; /* high */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300287 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300288 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300289
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300290 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
291 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300292 err("az6007_frontend_reset failed 3 !!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300293 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300294 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300295
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300296 msleep_interruptible(200);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300297
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300298 deb_info("reset az6007 frontend\n");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300299
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300300 return 0;
301}
302
303static int az6007_led_on_off(struct usb_interface *intf, int onoff)
304{
305 int ret = -1;
306 u8 req;
307 u16 value;
308 u16 index;
309 int blen;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300310 /* TS through */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300311 req = 0xBC;
312 value = onoff;
313 index = 0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300314 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300315
316 ret = usb_control_msg(interface_to_usbdev(intf),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300317 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
318 req,
319 USB_TYPE_VENDOR | USB_DIR_OUT,
320 value, index, NULL, blen, 2000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300321
322 if (ret < 0) {
323 warn("usb in operation failed. (%d)", ret);
324 ret = -EIO;
325 } else
326 ret = 0;
327
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300328 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
329 index);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300330
331 return ret;
332}
333
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300334static int az6007_frontend_tsbypass(struct dvb_usb_adapter *adap, int onoff)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300335{
336 int ret;
337 u8 req;
338 u16 value;
339 u16 index;
340 int blen;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300341 /* TS through */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300342 req = 0xC7;
343 value = onoff;
344 index = 0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300345 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300346
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300347 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
348 if (ret != 0)
349 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300350 return 0;
351}
352
353static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
354{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300355 struct az6007_device_state *st = adap->priv;
356
357 int result;
358
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300359 BUG_ON(!st);
360
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300361 az6007_frontend_poweron(adap);
362 az6007_frontend_reset(adap);
363
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300364 info("az6007: attaching demod drxk");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300365 adap->fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
366 &adap->dev->i2c_adap, &adap->fe2);
367 if (!adap->fe) {
368 result = -EINVAL;
369 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300370 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300371
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300372 deb_info("Setting hacks\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300373
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300374 /* FIXME: do we need a pll semaphore? */
375 adap->fe->sec_priv = adap;
376 sema_init(&st->pll_mutex, 1);
377 st->gate_ctrl = adap->fe->ops.i2c_gate_ctrl;
378 adap->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
379 adap->fe2->id = 1;
380
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300381 info("az6007: attaching tuner mt2063");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300382 /* Attach mt2063 to DVB-C frontend */
383 if (adap->fe->ops.i2c_gate_ctrl)
384 adap->fe->ops.i2c_gate_ctrl(adap->fe, 1);
385 if (!dvb_attach(mt2063_attach, adap->fe, &az6007_mt2063_config,
386 &adap->dev->i2c_adap)) {
387 result = -EINVAL;
388
389 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300390 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300391 if (adap->fe->ops.i2c_gate_ctrl)
392 adap->fe->ops.i2c_gate_ctrl(adap->fe, 0);
393
394 /* Hack - needed due to drxk */
395 adap->fe2->tuner_priv = adap->fe->tuner_priv;
396 memcpy(&adap->fe2->ops.tuner_ops,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300397 &adap->fe->ops.tuner_ops, sizeof(adap->fe->ops.tuner_ops));
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300398 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300399
400out_free:
401 if (adap->fe)
402 dvb_frontend_detach(adap->fe);
403 adap->fe = NULL;
404 adap->fe2 = NULL;
405
406 return result;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300407}
408
409static struct dvb_usb_device_properties az6007_properties;
410
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300411static void az6007_usb_disconnect(struct usb_interface *intf)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300412{
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300413 dvb_usb_device_exit(intf);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300414}
415
416/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300417static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
418 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300419{
420 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300421 int i, j, len;
422 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300423 u16 index;
424 u16 value;
425 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300426 u8 req, addr;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300427 u8 data[512];
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300428
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300429 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
430 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300431
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300432 for (i = 0; i < num; i++) {
433 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300434 if (((i + 1) < num)
435 && (msgs[i].len == 1)
436 && (!msgs[i].flags & I2C_M_RD)
437 && (msgs[i + 1].flags & I2C_M_RD)
438 && (msgs[i].addr == msgs[i + 1].addr)) {
439 /*
440 * A write + read xfer for the same address, where
441 * the first xfer has just 1 byte length.
442 * Need to join both into one operation
443 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300444 if (dvb_usb_az6007_debug & 2)
445 printk(KERN_DEBUG
446 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
447 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300448 req = 0xb9;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300449 index = msgs[i].buf[0];
450 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300451 length = 6 + msgs[i + 1].len;
452 len = msgs[i + 1].len;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300453 ret = az6007_usb_in_op(d, req, value, index, data,
454 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300455 if (ret >= len) {
456 for (j = 0; j < len; j++) {
457 msgs[i + 1].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 ",
461 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300462 }
463 } else
464 ret = -EIO;
465 i++;
466 } else if (!(msgs[i].flags & I2C_M_RD)) {
467 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300468 if (dvb_usb_az6007_debug & 2)
469 printk(KERN_DEBUG
470 "az6007 I2C xfer write addr=0x%x len=%d: ",
471 addr, msgs[i].len);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300472 req = 0xbd;
473 index = msgs[i].buf[0];
474 value = addr | (1 << 8);
475 length = msgs[i].len - 1;
476 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300477 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300478 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
479 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300480 data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300481 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300482 printk(KERN_CONT "0x%02x ", data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300483 }
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300484 ret = az6007_usb_out_op(d, req, value, index, data,
485 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300486 } else {
487 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300488 if (dvb_usb_az6007_debug & 2)
489 printk(KERN_DEBUG
490 "az6007 I2C xfer read addr=0x%x len=%d: ",
491 addr, msgs[i].len);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300492 req = 0xb9;
493 index = msgs[i].buf[0];
494 value = addr;
495 length = msgs[i].len + 6;
496 len = msgs[i].len;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300497 ret = az6007_usb_in_op(d, req, value, index, data,
498 length);
499 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300500 msgs[i].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300501 if (dvb_usb_az6007_debug & 2)
502 printk(KERN_CONT
503 "0x%02x ", data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300504 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300505 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300506 if (dvb_usb_az6007_debug & 2)
507 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300508 if (ret < 0)
509 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300510 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300511err:
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300512 mutex_unlock(&d->i2c_mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300513
514 if (ret < 0) {
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300515 info("%s ERROR: %i", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300516 return ret;
517 }
518 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300519}
520
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300521static u32 az6007_i2c_func(struct i2c_adapter *adapter)
522{
523 return I2C_FUNC_I2C;
524}
525
526static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300527 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300528 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300529};
530
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300531int az6007_identify_state(struct usb_device *udev,
532 struct dvb_usb_device_properties *props,
533 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300534{
535 u8 b[16];
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300536 s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
537 0xb7, USB_TYPE_VENDOR | USB_DIR_IN, 6, 0, b,
538 6, USB_CTRL_GET_TIMEOUT);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300539
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300540 deb_info("FW GET_VERSION length: %d\n", ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300541
542 *cold = ret <= 0;
543
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300544 deb_info("cold: %d\n", *cold);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300545 return 0;
546}
547
548static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300549 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300550{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300551 az6007_led_on_off(intf, 0);
552
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300553 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300554 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300555}
556
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300557static struct usb_device_id az6007_usb_table[] = {
558 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
559 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
560 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300561};
562
563MODULE_DEVICE_TABLE(usb, az6007_usb_table);
564
565static struct dvb_usb_device_properties az6007_properties = {
566 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
567 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300568 .firmware = "dvb-usb-az6007-03.fw",
569 .no_reconnect = 1,
570
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300571 .identify_state = az6007_identify_state,
572 .num_adapters = 1,
573 .adapter = {
574 {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300575 /* .caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS, */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300576 .streaming_ctrl = az6007_streaming_ctrl,
577 .frontend_attach = az6007_frontend_attach,
578
579 /* parameter for the MPEG2-data transfer */
580 .stream = {
581 .type = USB_BULK,
582 .count = 10,
583 .endpoint = 0x02,
584 .u = {
585 .bulk = {
586 .buffersize = 4096,
587 }
588 }
589 },
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300590 .size_of_priv = sizeof(struct az6007_device_state),
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300591 }
592 },
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300593 /* .power_ctrl = az6007_power_ctrl, */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300594 .read_mac_address = az6007_read_mac_addr,
595
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300596 .rc.legacy = {
597 .rc_map_table = rc_map_az6007_table,
598 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
599 .rc_interval = 400,
600 .rc_query = az6007_rc_query,
601 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300602 .i2c_algo = &az6007_i2c_algo,
603
604 .num_device_descs = 2,
605 .devices = {
606 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
607 .cold_ids = { &az6007_usb_table[0], NULL },
608 .warm_ids = { NULL },
609 },
610 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
611 .cold_ids = { &az6007_usb_table[1], NULL },
612 .warm_ids = { NULL },
613 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300614 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300615 }
616};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300617
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300618/* usb specific object needed to register this driver with the usb subsystem */
619static struct usb_driver az6007_usb_driver = {
620 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300621 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300622 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300623 /* .disconnect = az6007_usb_disconnect, */
624 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300625};
626
627/* module stuff */
628static int __init az6007_usb_module_init(void)
629{
630 int result;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300631 deb_info("az6007 usb module init\n");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300632
633 result = usb_register(&az6007_usb_driver);
634 if (result) {
635 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300636 return result;
637 }
638
639 return 0;
640}
641
642static void __exit az6007_usb_module_exit(void)
643{
644 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -0300645 deb_info("az6007 usb module exit\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300646 usb_deregister(&az6007_usb_driver);
647}
648
649module_init(az6007_usb_module_init);
650module_exit(az6007_usb_module_exit);
651
652MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
653MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300654MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300655MODULE_LICENSE("GPL");