blob: 1791cb0886290fb5c3a99bee4c3c88b50d78e010 [file] [log] [blame]
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -03001/* DVB USB compliant Linux driver for the AzureWave 6017 USB2.0 DVB-S
2 * receiver.
3 * see Documentation/dvb/README.dvb-usb for more information
4 */
5
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -03006#include "drxk.h"
7#include "mt2063.h"
8#include "dvb_ca_en50221.h"
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -03009#include "dvb-usb.h"
10
11#define DVB_USB_LOG_PREFIX "az6007"
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030012
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030013/* HACK: Should be moved to the right place */
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030014#define USB_PID_AZUREWAVE_6007 0x0ccd
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030015#define USB_PID_TERRATEC_H7 0x10b4
16
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030017/* debug */
18int dvb_usb_az6007_debug;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030019module_param_named(debug, dvb_usb_az6007_debug, int, 0644);
20MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))."
21 DVB_USB_DEBUG_STATUS);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030022
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030023#define deb_info(args...) dprintk(dvb_usb_az6007_debug, 0x01, args)
24#define deb_xfer(args...) dprintk(dvb_usb_az6007_debug, 0x02, args)
25#define deb_rc(args...) dprintk(dvb_usb_az6007_debug, 0x04, args)
26#define deb_fe(args...) dprintk(dvb_usb_az6007_debug, 0x08, args)
27
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030028DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
29
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030030struct az6007_device_state {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030031 struct dvb_ca_en50221 ca;
32 struct mutex ca_mutex;
33 u8 power_state;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030034
35 /* Due to DRX-K - probably need changes */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030036 int (*gate_ctrl) (struct dvb_frontend *, int);
37 struct semaphore pll_mutex;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030038 bool dont_attach_fe1;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030039};
40
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030041static struct drxk_config terratec_h7_drxk = {
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030042 .adr = 0x29,
43 .single_master = 1,
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -030044 .no_i2c_bridge = 0,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030045 .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030046};
47
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030048static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
49{
50 struct dvb_usb_adapter *adap = fe->sec_priv;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030051 struct az6007_device_state *st;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030052 int status;
53
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030054 info("%s: %s", __func__, enable ? "enable" : "disable");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030055
56 if (!adap)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030057 return -EINVAL;
58
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030059 st = adap->priv;
60
61 if (!st)
62 return -EINVAL;
63
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030064 if (enable) {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030065#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030066 down(&st->pll_mutex);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030067#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030068 status = st->gate_ctrl(fe, 1);
69 } else {
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030070#if 0
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030071 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab22125012011-07-23 09:58:38 -030072#endif
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030073 up(&st->pll_mutex);
74 }
75 return status;
76}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030077
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030078static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030079 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030080 .refclock = 36125000,
81};
82
83/* check for mutex FIXME */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030084static int az6007_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value,
85 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030086{
87 int ret = -1;
88
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030089 ret = usb_control_msg(d->udev,
90 usb_rcvctrlpipe(d->udev, 0),
91 req,
92 USB_TYPE_VENDOR | USB_DIR_IN,
93 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030094
95 if (ret < 0) {
96 warn("usb in operation failed. (%d)", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030097 return -EIO;
98 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030099
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300100 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
101 index);
102 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300103
104 return ret;
105}
106
107static int az6007_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
108 u16 index, u8 *b, int blen)
109{
110 int ret;
111
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300112 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
113 index);
114 debug_dump(b, blen, deb_xfer);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300115
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300116 if (blen > 64) {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300117 printk(KERN_ERR
118 "az6007: doesn't suport I2C transactions longer than 64 bytes\n");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300119 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300120 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300121
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300122 ret = usb_control_msg(d->udev,
123 usb_sndctrlpipe(d->udev, 0),
124 req,
125 USB_TYPE_VENDOR | USB_DIR_OUT,
126 value, index, b, blen, 5000);
127 if (ret != blen) {
128 warn("usb out operation failed. (%d)", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300129 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300130 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300131
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300132 return 0;
133}
134
135static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
136{
137 return 0;
138}
139
140/* keys for the enclosed remote control */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300141static struct rc_map_table rc_map_az6007_table[] = {
142 {0x0001, KEY_1},
143 {0x0002, KEY_2},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300144};
145
146/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300147static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300148{
149 return 0;
150#if 0
151 u8 key[10];
152 int i;
153
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300154 /* remove the following return to enabled remote querying */
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300155
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300156 az6007_usb_in_op(d, READ_REMOTE_REQ, 0, 0, key, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300157
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300158 deb_rc("remote query key: %x %d\n", key[1], key[1]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300159
160 if (key[1] == 0x44) {
161 *state = REMOTE_NO_KEY_PRESSED;
162 return 0;
163 }
164
165 for (i = 0; i < ARRAY_SIZE(az6007_rc_keys); i++)
166 if (az6007_rc_keys[i].custom == key[1]) {
167 *state = REMOTE_KEY_PRESSED;
168 *event = az6007_rc_keys[i].event;
169 break;
170 }
171 return 0;
172#endif
173}
174
175/*
176int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
177{
178 u8 v = onoff;
179 return az6007_usb_out_op(d,0xBC,v,3,NULL,1);
180}
181*/
182
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300183static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300184{
185 az6007_usb_in_op(d, 0xb7, 6, 0, &mac[0], 6);
186 return 0;
187}
188
189static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
190{
191 int ret;
192 u8 req;
193 u16 value;
194 u16 index;
195 int blen;
196
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300197 info("az6007_frontend_poweron adap=%p adap->dev=%p", adap, adap->dev);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300198
199 req = 0xBC;
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300200 value = 1; /* power on */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300201 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300202 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300203
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300204 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
205 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300206 err("az6007_frontend_poweron failed!!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300207 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300208 }
209
210 msleep_interruptible(200);
211
212 req = 0xBC;
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300213 value = 0; /* power off */
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 = 1; /* power on */
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 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300235 info("az6007_frontend_poweron: OK");
236
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300237 return 0;
238}
239
240static int az6007_frontend_reset(struct dvb_usb_adapter *adap)
241{
242 int ret;
243 u8 req;
244 u16 value;
245 u16 index;
246 int blen;
247
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300248 info("az6007_frontend_reset adap=%p adap->dev=%p", adap, adap->dev);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300249
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300250 /* reset demodulator */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300251 req = 0xC0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300252 value = 1; /* high */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300253 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300254 blen = 0;
255 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
256 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300257 err("az6007_frontend_reset failed 1 !!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300258 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300259 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300260
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300261 req = 0xC0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300262 value = 0; /* low */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300263 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300264 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300265 msleep_interruptible(200);
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300266 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
267 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300268 err("az6007_frontend_reset failed 2 !!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300269 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300270 }
271 msleep_interruptible(200);
272 req = 0xC0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300273 value = 1; /* high */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300274 index = 3;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300275 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300276
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300277 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
278 if (ret != 0) {
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300279 err("az6007_frontend_reset failed 3 !!!");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300280 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300281 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300282
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300283 msleep_interruptible(200);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300284
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300285 info("reset az6007 frontend");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300286
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300287 return 0;
288}
289
290static int az6007_led_on_off(struct usb_interface *intf, int onoff)
291{
292 int ret = -1;
293 u8 req;
294 u16 value;
295 u16 index;
296 int blen;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300297 /* TS through */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300298 req = 0xBC;
299 value = onoff;
300 index = 0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300301 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300302
303 ret = usb_control_msg(interface_to_usbdev(intf),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300304 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
305 req,
306 USB_TYPE_VENDOR | USB_DIR_OUT,
307 value, index, NULL, blen, 2000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300308
309 if (ret < 0) {
310 warn("usb in operation failed. (%d)", ret);
311 ret = -EIO;
312 } else
313 ret = 0;
314
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300315 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
316 index);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300317
318 return ret;
319}
320
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300321static int az6007_frontend_tsbypass(struct dvb_usb_adapter *adap, int onoff)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300322{
323 int ret;
324 u8 req;
325 u16 value;
326 u16 index;
327 int blen;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300328 /* TS through */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300329 req = 0xC7;
330 value = onoff;
331 index = 0;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300332 blen = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300333
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300334 ret = az6007_usb_out_op(adap->dev, req, value, index, NULL, blen);
335 if (ret != 0)
336 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300337 return 0;
338}
339
340static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
341{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300342 struct az6007_device_state *st = adap->priv;
343
344 int result;
345
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300346 BUG_ON(!st);
347
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300348 az6007_frontend_poweron(adap);
349 az6007_frontend_reset(adap);
350
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300351 info("az6007_frontend_attach: drxk");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300352
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300353 adap->fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
354 &adap->dev->i2c_adap, &adap->fe2);
355 if (!adap->fe) {
356 result = -EINVAL;
357 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300358 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300359
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300360 info("Setting hacks");
361
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300362 /* FIXME: do we need a pll semaphore? */
363 adap->fe->sec_priv = adap;
364 sema_init(&st->pll_mutex, 1);
365 st->gate_ctrl = adap->fe->ops.i2c_gate_ctrl;
366 adap->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
367 adap->fe2->id = 1;
368
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300369 info("az6007_frontend_attach: mt2063");
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300370 /* Attach mt2063 to DVB-C frontend */
371 if (adap->fe->ops.i2c_gate_ctrl)
372 adap->fe->ops.i2c_gate_ctrl(adap->fe, 1);
373 if (!dvb_attach(mt2063_attach, adap->fe, &az6007_mt2063_config,
374 &adap->dev->i2c_adap)) {
375 result = -EINVAL;
376
377 goto out_free;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300378 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300379 if (adap->fe->ops.i2c_gate_ctrl)
380 adap->fe->ops.i2c_gate_ctrl(adap->fe, 0);
381
382 /* Hack - needed due to drxk */
383 adap->fe2->tuner_priv = adap->fe->tuner_priv;
384 memcpy(&adap->fe2->ops.tuner_ops,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300385 &adap->fe->ops.tuner_ops, sizeof(adap->fe->ops.tuner_ops));
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300386 return 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300387
388out_free:
389 if (adap->fe)
390 dvb_frontend_detach(adap->fe);
391 adap->fe = NULL;
392 adap->fe2 = NULL;
393
394 return result;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300395}
396
397static struct dvb_usb_device_properties az6007_properties;
398
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300399static void az6007_usb_disconnect(struct usb_interface *intf)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300400{
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300401 dvb_usb_device_exit(intf);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300402}
403
404/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300405static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
406 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300407{
408 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300409 int i, j, len;
410 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300411 u16 index;
412 u16 value;
413 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300414 u8 req, addr;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300415 u8 data[512];
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300416
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300417 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
418 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300419
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300420 for (i = 0; i < num; i++) {
421 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300422 if (((i + 1) < num)
423 && (msgs[i].len == 1)
424 && (!msgs[i].flags & I2C_M_RD)
425 && (msgs[i + 1].flags & I2C_M_RD)
426 && (msgs[i].addr == msgs[i + 1].addr)) {
427 /*
428 * A write + read xfer for the same address, where
429 * the first xfer has just 1 byte length.
430 * Need to join both into one operation
431 */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300432 if (dvb_usb_az6007_debug & 2)
433 printk(KERN_DEBUG
434 "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
435 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300436 req = 0xb9;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300437 index = msgs[i].buf[0];
438 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300439 length = 6 + msgs[i + 1].len;
440 len = msgs[i + 1].len;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300441 ret = az6007_usb_in_op(d, req, value, index, data,
442 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300443 if (ret >= len) {
444 for (j = 0; j < len; j++) {
445 msgs[i + 1].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300446 if (dvb_usb_az6007_debug & 2)
447 printk(KERN_CONT
448 "0x%02x ",
449 msgs[i + 1].buf[j]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300450 }
451 } else
452 ret = -EIO;
453 i++;
454 } else if (!(msgs[i].flags & I2C_M_RD)) {
455 /* write bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300456 if (dvb_usb_az6007_debug & 2)
457 printk(KERN_DEBUG
458 "az6007 I2C xfer write addr=0x%x len=%d: ",
459 addr, msgs[i].len);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300460 req = 0xbd;
461 index = msgs[i].buf[0];
462 value = addr | (1 << 8);
463 length = msgs[i].len - 1;
464 len = msgs[i].len - 1;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300465 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300466 printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
467 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300468 data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300469 if (dvb_usb_az6007_debug & 2)
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300470 printk(KERN_CONT "0x%02x ", data[j]);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300471 }
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300472 ret = az6007_usb_out_op(d, req, value, index, data,
473 length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300474 } else {
475 /* read bytes */
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300476 if (dvb_usb_az6007_debug & 2)
477 printk(KERN_DEBUG
478 "az6007 I2C xfer read addr=0x%x len=%d: ",
479 addr, msgs[i].len);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300480 req = 0xb9;
481 index = msgs[i].buf[0];
482 value = addr;
483 length = msgs[i].len + 6;
484 len = msgs[i].len;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300485 ret = az6007_usb_in_op(d, req, value, index, data,
486 length);
487 for (j = 0; j < len; j++) {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300488 msgs[i].buf[j] = data[j + 5];
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300489 if (dvb_usb_az6007_debug & 2)
490 printk(KERN_CONT
491 "0x%02x ", data[j + 5]);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300492 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300493 }
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300494 if (dvb_usb_az6007_debug & 2)
495 printk(KERN_CONT "\n");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300496 if (ret < 0)
497 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300498 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300499err:
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300500 mutex_unlock(&d->i2c_mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300501
502 if (ret < 0) {
503 info("%s ERROR: %i\n", __func__, ret);
504 return ret;
505 }
506 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300507}
508
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300509static u32 az6007_i2c_func(struct i2c_adapter *adapter)
510{
511 return I2C_FUNC_I2C;
512}
513
514static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300515 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300516 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300517};
518
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300519int az6007_identify_state(struct usb_device *udev,
520 struct dvb_usb_device_properties *props,
521 struct dvb_usb_device_description **desc, int *cold)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300522{
523 u8 b[16];
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300524 s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
525 0xb7, USB_TYPE_VENDOR | USB_DIR_IN, 6, 0, b,
526 6, USB_CTRL_GET_TIMEOUT);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300527
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300528 info("FW GET_VERSION length: %d", ret);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300529
530 *cold = ret <= 0;
531
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300532 info("cold: %d", *cold);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300533 return 0;
534}
535
536static int az6007_usb_probe(struct usb_interface *intf,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300537 const struct usb_device_id *id)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300538{
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300539 az6007_led_on_off(intf, 0);
540
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300541 return dvb_usb_device_init(intf, &az6007_properties,
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300542 THIS_MODULE, NULL, adapter_nr);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300543}
544
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300545static struct usb_device_id az6007_usb_table[] = {
546 {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
547 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
548 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300549};
550
551MODULE_DEVICE_TABLE(usb, az6007_usb_table);
552
553static struct dvb_usb_device_properties az6007_properties = {
554 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
555 .usb_ctrl = CYPRESS_FX2,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300556 .firmware = "dvb-usb-az6007-03.fw",
557 .no_reconnect = 1,
558
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300559 .identify_state = az6007_identify_state,
560 .num_adapters = 1,
561 .adapter = {
562 {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300563 /* .caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS, */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300564 .streaming_ctrl = az6007_streaming_ctrl,
565 .frontend_attach = az6007_frontend_attach,
566
567 /* parameter for the MPEG2-data transfer */
568 .stream = {
569 .type = USB_BULK,
570 .count = 10,
571 .endpoint = 0x02,
572 .u = {
573 .bulk = {
574 .buffersize = 4096,
575 }
576 }
577 },
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300578 .size_of_priv = sizeof(struct az6007_device_state),
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300579 }
580 },
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300581 /* .power_ctrl = az6007_power_ctrl, */
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300582 .read_mac_address = az6007_read_mac_addr,
583
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300584 .rc.legacy = {
585 .rc_map_table = rc_map_az6007_table,
586 .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
587 .rc_interval = 400,
588 .rc_query = az6007_rc_query,
589 },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300590 .i2c_algo = &az6007_i2c_algo,
591
592 .num_device_descs = 2,
593 .devices = {
594 { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
595 .cold_ids = { &az6007_usb_table[0], NULL },
596 .warm_ids = { NULL },
597 },
598 { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
599 .cold_ids = { &az6007_usb_table[1], NULL },
600 .warm_ids = { NULL },
601 },
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300602 { NULL },
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300603 }
604};
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300605
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300606/* usb specific object needed to register this driver with the usb subsystem */
607static struct usb_driver az6007_usb_driver = {
608 .name = "dvb_usb_az6007",
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300609 .probe = az6007_usb_probe,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300610 .disconnect = dvb_usb_device_exit,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300611 /* .disconnect = az6007_usb_disconnect, */
612 .id_table = az6007_usb_table,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300613};
614
615/* module stuff */
616static int __init az6007_usb_module_init(void)
617{
618 int result;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300619 info("az6007 usb module init");
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300620
621 result = usb_register(&az6007_usb_driver);
622 if (result) {
623 err("usb_register failed. (%d)", result);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300624 return result;
625 }
626
627 return 0;
628}
629
630static void __exit az6007_usb_module_exit(void)
631{
632 /* deregister this driver from the USB subsystem */
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300633 info("az6007 usb module exit");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300634 usb_deregister(&az6007_usb_driver);
635}
636
637module_init(az6007_usb_module_init);
638module_exit(az6007_usb_module_exit);
639
640MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
641MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300642MODULE_VERSION("1.1");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300643MODULE_LICENSE("GPL");