blob: f946b1b65b1dbcb7143ce63072673bc1b283f8bc [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
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030030/* HACK: Should be moved to the right place */
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030031#define USB_PID_AZUREWAVE_6007 0x0ccd
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030032#define USB_PID_TERRATEC_H7 0x10b4
33
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030034/* debug */
35int dvb_usb_az6007_debug;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030036module_param_named(debug, dvb_usb_az6007_debug, int, 0644);
37MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))."
38 DVB_USB_DEBUG_STATUS);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030039
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030040#define deb_info(args...) dprintk(dvb_usb_az6007_debug, 0x01, args)
41#define deb_xfer(args...) dprintk(dvb_usb_az6007_debug, 0x02, args)
42#define deb_rc(args...) dprintk(dvb_usb_az6007_debug, 0x04, args)
43#define deb_fe(args...) dprintk(dvb_usb_az6007_debug, 0x08, args)
44
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030045DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
46
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030047struct az6007_device_state {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030048 struct dvb_ca_en50221 ca;
49 struct mutex ca_mutex;
50 u8 power_state;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030051
52 /* Due to DRX-K - probably need changes */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030053 int (*gate_ctrl) (struct dvb_frontend *, int);
54 struct semaphore pll_mutex;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030055 bool dont_attach_fe1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030056};
57
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030058static struct drxk_config terratec_h7_drxk = {
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030059 .adr = 0x29,
60 .single_master = 1,
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -030061 .no_i2c_bridge = 0,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030062 .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030063};
64
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030065static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
66{
67 struct dvb_usb_adapter *adap = fe->sec_priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030068 struct az6007_device_state *st;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030069 int status;
70
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030071 info("%s: %s", __func__, enable ? "enable" : "disable");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030072
73 if (!adap)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030074 return -EINVAL;
75
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030076 st = adap->priv;
77
78 if (!st)
79 return -EINVAL;
80
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030081 if (enable) {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030082#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030083 down(&st->pll_mutex);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030084#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030085 status = st->gate_ctrl(fe, 1);
86 } else {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030087#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030088 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030089#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030090 up(&st->pll_mutex);
91 }
92 return status;
93}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030094
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030095static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030096 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030097 .refclock = 36125000,
98};
99
100/* check for mutex FIXME */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300101static int az6007_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value,
102 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300103{
104 int ret = -1;
105
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300106 ret = usb_control_msg(d->udev,
107 usb_rcvctrlpipe(d->udev, 0),
108 req,
109 USB_TYPE_VENDOR | USB_DIR_IN,
110 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300111
112 if (ret < 0) {
113 warn("usb in operation failed. (%d)", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300114 return -EIO;
115 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300116
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300117 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
118 index);
119 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300120
121 return ret;
122}
123
124static int az6007_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
125 u16 index, u8 *b, int blen)
126{
127 int ret;
128
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300129 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
130 index);
131 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300132
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300133 if (blen > 64) {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300134 printk(KERN_ERR
135 "az6007: doesn't suport I2C transactions longer than 64 bytes\n");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300136 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300137 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300138
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300139 ret = usb_control_msg(d->udev,
140 usb_sndctrlpipe(d->udev, 0),
141 req,
142 USB_TYPE_VENDOR | USB_DIR_OUT,
143 value, index, b, blen, 5000);
144 if (ret != blen) {
145 warn("usb out operation failed. (%d)", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300146 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300147 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300148
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300149 return 0;
150}
151
152static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
153{
154 return 0;
155}
156
157/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300158static struct rc_map_table rc_map_az6007_table[] = {
159 {0x0001, KEY_1},
160 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300161};
162
163/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300164static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300165{
166 return 0;
167#if 0
168 u8 key[10];
169 int i;
170
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300171 /* remove the following return to enabled remote querying */
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300172
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300173 az6007_usb_in_op(d, READ_REMOTE_REQ, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300174
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300175 deb_rc("remote query key: %x %d\n", key[1], key[1]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300176
177 if (key[1] == 0x44) {
178 *state = REMOTE_NO_KEY_PRESSED;
179 return 0;
180 }
181
182 for (i = 0; i < ARRAY_SIZE(az6007_rc_keys); i++)
183 if (az6007_rc_keys[i].custom == key[1]) {
184 *state = REMOTE_KEY_PRESSED;
185 *event = az6007_rc_keys[i].event;
186 break;
187 }
188 return 0;
189#endif
190}
191
192/*
193int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
194{
195 u8 v = onoff;
196 return az6007_usb_out_op(d,0xBC,v,3,NULL,1);
197}
198*/
199
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300200static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300201{
202 az6007_usb_in_op(d, 0xb7, 6, 0, &mac[0], 6);
203 return 0;
204}
205
206static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
207{
208 int ret;
209 u8 req;
210 u16 value;
211 u16 index;
212 int blen;
213
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300214 info("az6007_frontend_poweron adap=%p adap->dev=%p", adap, adap->dev);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300215
216 req = 0xBC;
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300217 value = 1; /* power on */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300218 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300219 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300220
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300221 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
222 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300223 err("az6007_frontend_poweron failed!!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300224 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300225 }
226
227 msleep_interruptible(200);
228
229 req = 0xBC;
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300230 value = 0; /* power off */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300231 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300232 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300233
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300234 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
235 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300236 err("az6007_frontend_poweron failed!!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300237 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300238 }
239
240 msleep_interruptible(200);
241
242 req = 0xBC;
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300243 value = 1; /* power on */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300244 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300245 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300246
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300247 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
248 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300249 err("az6007_frontend_poweron failed!!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300250 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300251 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300252 info("az6007_frontend_poweron: OK");
253
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300254 return 0;
255}
256
257static int az6007_frontend_reset(struct dvb_usb_adapter *adap)
258{
259 int ret;
260 u8 req;
261 u16 value;
262 u16 index;
263 int blen;
264
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300265 info("az6007_frontend_reset adap=%p adap->dev=%p", adap, adap->dev);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300266
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300267 /* reset demodulator */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300268 req = 0xC0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300269 value = 1; /* high */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300270 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300271 blen = 0;
272 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
273 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300274 err("az6007_frontend_reset failed 1 !!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300275 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300276 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300277
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300278 req = 0xC0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300279 value = 0; /* low */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300280 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300281 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300282 msleep_interruptible(200);
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300283 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
284 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300285 err("az6007_frontend_reset failed 2 !!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300286 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300287 }
288 msleep_interruptible(200);
289 req = 0xC0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300290 value = 1; /* high */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300291 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300292 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300293
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300294 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
295 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300296 err("az6007_frontend_reset failed 3 !!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300297 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300298 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300299
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300300 msleep_interruptible(200);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300301
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300302 info("reset az6007 frontend");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300303
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300304 return 0;
305}
306
307static int az6007_led_on_off(struct usb_interface *intf, int onoff)
308{
309 int ret = -1;
310 u8 req;
311 u16 value;
312 u16 index;
313 int blen;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300314 /* TS through */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300315 req = 0xBC;
316 value = onoff;
317 index = 0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300318 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300319
320 ret = usb_control_msg(interface_to_usbdev(intf),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300321 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
322 req,
323 USB_TYPE_VENDOR | USB_DIR_OUT,
324 value, index, NULL, blen, 2000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300325
326 if (ret < 0) {
327 warn("usb in operation failed. (%d)", ret);
328 ret = -EIO;
329 } else
330 ret = 0;
331
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300332 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
333 index);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300334
335 return ret;
336}
337
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300338static int az6007_frontend_tsbypass(struct dvb_usb_adapter *adap, int onoff)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300339{
340 int ret;
341 u8 req;
342 u16 value;
343 u16 index;
344 int blen;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300345 /* TS through */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300346 req = 0xC7;
347 value = onoff;
348 index = 0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300349 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300350
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300351 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
352 if (ret != 0)
353 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300354 return 0;
355}
356
357static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
358{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300359 struct az6007_device_state *st = adap->priv;
360
361 int result;
362
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300363 BUG_ON(!st);
364
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300365 az6007_frontend_poweron(adap);
366 az6007_frontend_reset(adap);
367
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300368 info("az6007_frontend_attach: drxk");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300369
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300370 adap->fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
371 &adap->dev->i2c_adap, &adap->fe2);
372 if (!adap->fe) {
373 result = -EINVAL;
374 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300375 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300376
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300377 info("Setting hacks");
378
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300379 /* FIXME: do we need a pll semaphore? */
380 adap->fe->sec_priv = adap;
381 sema_init(&st->pll_mutex, 1);
382 st->gate_ctrl = adap->fe->ops.i2c_gate_ctrl;
383 adap->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
384 adap->fe2->id = 1;
385
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300386 info("az6007_frontend_attach: mt2063");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300387 /* Attach mt2063 to DVB-C frontend */
388 if (adap->fe->ops.i2c_gate_ctrl)
389 adap->fe->ops.i2c_gate_ctrl(adap->fe, 1);
390 if (!dvb_attach(mt2063_attach, adap->fe, &az6007_mt2063_config,
391 &adap->dev->i2c_adap)) {
392 result = -EINVAL;
393
394 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300395 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300396 if (adap->fe->ops.i2c_gate_ctrl)
397 adap->fe->ops.i2c_gate_ctrl(adap->fe, 0);
398
399 /* Hack - needed due to drxk */
400 adap->fe2->tuner_priv = adap->fe->tuner_priv;
401 memcpy(&adap->fe2->ops.tuner_ops,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300402 &adap->fe->ops.tuner_ops, sizeof(adap->fe->ops.tuner_ops));
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300403 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300404
405out_free:
406 if (adap->fe)
407 dvb_frontend_detach(adap->fe);
408 adap->fe = NULL;
409 adap->fe2 = NULL;
410
411 return result;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300412}
413
414static struct dvb_usb_device_properties az6007_properties;
415
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300416static void az6007_usb_disconnect(struct usb_interface *intf)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300417{
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300418 dvb_usb_device_exit(intf);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300419}
420
421/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300422static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
423 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300424{
425 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300426 int i, j, len;
427 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300428 u16 index;
429 u16 value;
430 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300431 u8 req, addr;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300432 u8 data[512];
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300433
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300434 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
435 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300436
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300437 for (i = 0; i < num; i++) {
438 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300439 if (((i + 1) < num)
440 && (msgs[i].len == 1)
441 && (!msgs[i].flags & I2C_M_RD)
442 && (msgs[i + 1].flags & I2C_M_RD)
443 && (msgs[i].addr == msgs[i + 1].addr)) {
444 /*
445 * A write + read xfer for the same address, where
446 * the first xfer has just 1 byte length.
447 * Need to join both into one operation
448 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300449 if (dvb_usb_az6007_debug & 2)
450 printk(KERN_DEBUG
451 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
452 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300453 req = 0xb9;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300454 index = msgs[i].buf[0];
455 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300456 length = 6 + msgs[i + 1].len;
457 len = msgs[i + 1].len;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300458 ret = az6007_usb_in_op(d, req, value, index, data,
459 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300460 if (ret >= len) {
461 for (j = 0; j < len; j++) {
462 msgs[i + 1].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300463 if (dvb_usb_az6007_debug & 2)
464 printk(KERN_CONT
465 "0x%02x ",
466 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300467 }
468 } else
469 ret = -EIO;
470 i++;
471 } else if (!(msgs[i].flags & I2C_M_RD)) {
472 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300473 if (dvb_usb_az6007_debug & 2)
474 printk(KERN_DEBUG
475 "az6007 I2C xfer write addr=0x%x len=%d: ",
476 addr, msgs[i].len);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300477 req = 0xbd;
478 index = msgs[i].buf[0];
479 value = addr | (1 << 8);
480 length = msgs[i].len - 1;
481 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300482 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300483 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
484 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300485 data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300486 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300487 printk(KERN_CONT "0x%02x ", data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300488 }
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300489 ret = az6007_usb_out_op(d, req, value, index, data,
490 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300491 } else {
492 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300493 if (dvb_usb_az6007_debug & 2)
494 printk(KERN_DEBUG
495 "az6007 I2C xfer read addr=0x%x len=%d: ",
496 addr, msgs[i].len);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300497 req = 0xb9;
498 index = msgs[i].buf[0];
499 value = addr;
500 length = msgs[i].len + 6;
501 len = msgs[i].len;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300502 ret = az6007_usb_in_op(d, req, value, index, data,
503 length);
504 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300505 msgs[i].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300506 if (dvb_usb_az6007_debug & 2)
507 printk(KERN_CONT
508 "0x%02x ", data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300509 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300510 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300511 if (dvb_usb_az6007_debug & 2)
512 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300513 if (ret < 0)
514 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300515 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300516err:
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300517 mutex_unlock(&d->i2c_mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300518
519 if (ret < 0) {
520 info("%s ERROR: %i\n", __func__, ret);
521 return ret;
522 }
523 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300524}
525
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300526static u32 az6007_i2c_func(struct i2c_adapter *adapter)
527{
528 return I2C_FUNC_I2C;
529}
530
531static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300532 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300533 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300534};
535
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300536int az6007_identify_state(struct usb_device *udev,
537 struct dvb_usb_device_properties *props,
538 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300539{
540 u8 b[16];
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300541 s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
542 0xb7, USB_TYPE_VENDOR | USB_DIR_IN, 6, 0, b,
543 6, USB_CTRL_GET_TIMEOUT);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300544
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300545 info("FW GET_VERSION length: %d", ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300546
547 *cold = ret <= 0;
548
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300549 info("cold: %d", *cold);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300550 return 0;
551}
552
553static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300554 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300555{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300556 az6007_led_on_off(intf, 0);
557
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300558 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300559 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300560}
561
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300562static struct usb_device_id az6007_usb_table[] = {
563 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
564 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
565 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300566};
567
568MODULE_DEVICE_TABLE(usb, az6007_usb_table);
569
570static struct dvb_usb_device_properties az6007_properties = {
571 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
572 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300573 .firmware = "dvb-usb-az6007-03.fw",
574 .no_reconnect = 1,
575
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300576 .identify_state = az6007_identify_state,
577 .num_adapters = 1,
578 .adapter = {
579 {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300580 /* .caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS, */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300581 .streaming_ctrl = az6007_streaming_ctrl,
582 .frontend_attach = az6007_frontend_attach,
583
584 /* parameter for the MPEG2-data transfer */
585 .stream = {
586 .type = USB_BULK,
587 .count = 10,
588 .endpoint = 0x02,
589 .u = {
590 .bulk = {
591 .buffersize = 4096,
592 }
593 }
594 },
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300595 .size_of_priv = sizeof(struct az6007_device_state),
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300596 }
597 },
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300598 /* .power_ctrl = az6007_power_ctrl, */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300599 .read_mac_address = az6007_read_mac_addr,
600
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300601 .rc.legacy = {
602 .rc_map_table = rc_map_az6007_table,
603 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
604 .rc_interval = 400,
605 .rc_query = az6007_rc_query,
606 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300607 .i2c_algo = &az6007_i2c_algo,
608
609 .num_device_descs = 2,
610 .devices = {
611 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
612 .cold_ids = { &az6007_usb_table[0], NULL },
613 .warm_ids = { NULL },
614 },
615 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
616 .cold_ids = { &az6007_usb_table[1], NULL },
617 .warm_ids = { NULL },
618 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300619 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300620 }
621};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300622
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300623/* usb specific object needed to register this driver with the usb subsystem */
624static struct usb_driver az6007_usb_driver = {
625 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300626 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300627 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300628 /* .disconnect = az6007_usb_disconnect, */
629 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300630};
631
632/* module stuff */
633static int __init az6007_usb_module_init(void)
634{
635 int result;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300636 info("az6007 usb module init");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300637
638 result = usb_register(&az6007_usb_driver);
639 if (result) {
640 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300641 return result;
642 }
643
644 return 0;
645}
646
647static void __exit az6007_usb_module_exit(void)
648{
649 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300650 info("az6007 usb module exit");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300651 usb_deregister(&az6007_usb_driver);
652}
653
654module_init(az6007_usb_module_init);
655module_exit(az6007_usb_module_exit);
656
657MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
658MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300659MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300660MODULE_LICENSE("GPL");