1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
/*
* Driver for AzureWave 6007 DVB-C/T USB2.0 and clones
*
* Copyright (c) Henry Wang <Henry.wang@AzureWave.com>
*
* This driver was made publicly available by Terratec, at:
* http://linux.terratec.de/files/TERRATEC_H7/20110323_TERRATEC_H7_Linux.tar.gz
* The original driver's license is GPL, as declared with MODULE_LICENSE()
*
* Copyright (c) 2010-2011 Mauro Carvalho Chehab <mchehab@redhat.com>
* Driver modified by in order to work with upstream drxk driver, and
* tons of bugs got fixed.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation under version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "drxk.h"
#include "mt2063.h"
#include "dvb_ca_en50221.h"
#define DVB_USB_LOG_PREFIX "az6007"
#include "dvb-usb.h"
/* debug */
int dvb_usb_az6007_debug;
module_param_named(debug, dvb_usb_az6007_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))."
DVB_USB_DEBUG_STATUS);
#define deb_info(args...) dprintk(dvb_usb_az6007_debug, 0x01, args)
#define deb_xfer(args...) dprintk(dvb_usb_az6007_debug, 0x02, args)
#define deb_rc(args...) dprintk(dvb_usb_az6007_debug, 0x04, args)
#define deb_fe(args...) dprintk(dvb_usb_az6007_debug, 0x08, args)
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
/* Known requests (Cypress FX2 firmware + az6007 "private" ones*/
#define FX2_OED 0xb5
#define AZ6007_READ_DATA 0xb7
#define AZ6007_I2C_RD 0xb9
#define AZ6007_POWER 0xbc
#define AZ6007_I2C_WR 0xbd
#define FX2_SCON1 0xc0
#define AZ6007_TS_THROUGH 0xc7
#define AZ6007_READ_IR 0xb4
struct az6007_device_state {
struct mutex mutex;
struct dvb_ca_en50221 ca;
unsigned warm:1;
int (*gate_ctrl) (struct dvb_frontend *, int);
unsigned char data[4096];
};
static struct drxk_config terratec_h7_drxk = {
.adr = 0x29,
.parallel_ts = true,
.dynamic_clk = true,
.single_master = true,
.enable_merr_cfg = true,
.no_i2c_bridge = false,
.chunk_size = 64,
.mpeg_out_clk_strength = 0x02,
.microcode_name = "dvb-usb-terratec-h7-drxk.fw",
};
static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
{
struct dvb_usb_adapter *adap = fe->sec_priv;
struct az6007_device_state *st;
int status = 0;
deb_info("%s: %s\n", __func__, enable ? "enable" : "disable");
if (!adap)
return -EINVAL;
st = adap->dev->priv;
if (!st)
return -EINVAL;
if (enable)
status = st->gate_ctrl(fe, 1);
else
status = st->gate_ctrl(fe, 0);
return status;
}
static struct mt2063_config az6007_mt2063_config = {
.tuner_address = 0x60,
.refclock = 36125000,
};
static int __az6007_read(struct usb_device *udev, u8 req, u16 value,
u16 index, u8 *b, int blen)
{
int ret;
ret = usb_control_msg(udev,
usb_rcvctrlpipe(udev, 0),
req,
USB_TYPE_VENDOR | USB_DIR_IN,
value, index, b, blen, 5000);
if (ret < 0) {
warn("usb read operation failed. (%d)", ret);
return -EIO;
}
deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
index);
debug_dump(b, blen, deb_xfer);
return ret;
}
static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value,
u16 index, u8 *b, int blen)
{
struct az6007_device_state *st = d->priv;
int ret;
if (mutex_lock_interruptible(&st->mutex) < 0)
return -EAGAIN;
ret = __az6007_read(d->udev, req, value, index, b, blen);
mutex_unlock(&st->mutex);
return ret;
}
static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
u16 index, u8 *b, int blen)
{
int ret;
deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
index);
debug_dump(b, blen, deb_xfer);
if (blen > 64) {
err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
blen);
return -EOPNOTSUPP;
}
ret = usb_control_msg(udev,
usb_sndctrlpipe(udev, 0),
req,
USB_TYPE_VENDOR | USB_DIR_OUT,
value, index, b, blen, 5000);
if (ret != blen) {
err("usb write operation failed. (%d)", ret);
return -EIO;
}
return 0;
}
static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value,
u16 index, u8 *b, int blen)
{
struct az6007_device_state *st = d->priv;
int ret;
if (mutex_lock_interruptible(&st->mutex) < 0)
return -EAGAIN;
ret = __az6007_write(d->udev, req, value, index, b, blen);
mutex_unlock(&st->mutex);
return ret;
}
static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
struct dvb_usb_device *d = adap->dev;
deb_info("%s: %s", __func__, onoff ? "enable" : "disable");
return az6007_write(d, 0xbc, onoff, 0, NULL, 0);
}
/* remote control stuff (does not work with my box) */
static int az6007_rc_query(struct dvb_usb_device *d)
{
struct az6007_device_state *st = d->priv;
unsigned code = 0;
az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
if (st->data[1] == 0x44)
return 0;
if ((st->data[1] ^ st->data[2]) == 0xff)
code = st->data[1];
else
code = st->data[1] << 8 | st->data[2];
if ((st->data[3] ^ st->data[4]) == 0xff)
code = code << 8 | st->data[3];
else
code = code << 16 | st->data[3] << 8 | st->data[4];
rc_keydown(d->rc_dev, code, st->data[5]);
return 0;
}
static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
{
struct az6007_device_state *st = d->priv;
int ret;
ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
memcpy(mac, st->data, sizeof(mac));
if (ret > 0)
deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
__func__, mac[0], mac[1], mac[2],
mac[3], mac[4], mac[5]);
return ret;
}
static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
{
struct az6007_device_state *st = adap->dev->priv;
deb_info("attaching demod drxk");
adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
&adap->dev->i2c_adap);
if (!adap->fe_adap[0].fe)
return -EINVAL;
adap->fe_adap[0].fe->sec_priv = adap;
st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
return 0;
}
static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
{
deb_info("attaching tuner mt2063");
/* Attach mt2063 to DVB-C frontend */
if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
&az6007_mt2063_config,
&adap->dev->i2c_adap))
return -EINVAL;
if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
return 0;
}
int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
{
struct az6007_device_state *st = d->priv;
int ret;
deb_info("%s()\n", __func__);
if (!st->warm) {
mutex_init(&st->mutex);
ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
if (ret < 0)
return ret;
msleep(60);
ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
if (ret < 0)
return ret;
msleep(100);
ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
if (ret < 0)
return ret;
msleep(20);
ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
if (ret < 0)
return ret;
msleep(400);
ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
if (ret < 0)
return ret;
msleep(150);
ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
if (ret < 0)
return ret;
msleep(430);
ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
if (ret < 0)
return ret;
st->warm = true;
return 0;
}
if (!onoff)
return 0;
az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
return 0;
}
/* I2C */
static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
int num)
{
struct dvb_usb_device *d = i2c_get_adapdata(adap);
struct az6007_device_state *st = d->priv;
int i, j, len;
int ret = 0;
u16 index;
u16 value;
int length;
u8 req, addr;
if (mutex_lock_interruptible(&st->mutex) < 0)
return -EAGAIN;
for (i = 0; i < num; i++) {
addr = msgs[i].addr << 1;
if (((i + 1) < num)
&& (msgs[i].len == 1)
&& (!msgs[i].flags & I2C_M_RD)
&& (msgs[i + 1].flags & I2C_M_RD)
&& (msgs[i].addr == msgs[i + 1].addr)) {
/*
* A write + read xfer for the same address, where
* the first xfer has just 1 byte length.
* Need to join both into one operation
*/
if (dvb_usb_az6007_debug & 2)
printk(KERN_DEBUG
"az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
addr, msgs[i].len, msgs[i + 1].len);
req = AZ6007_I2C_RD;
index = msgs[i].buf[0];
value = addr | (1 << 8);
length = 6 + msgs[i + 1].len;
len = msgs[i + 1].len;
ret = __az6007_read(d->udev, req, value, index,
st->data, length);
if (ret >= len) {
for (j = 0; j < len; j++) {
msgs[i + 1].buf[j] = st->data[j + 5];
if (dvb_usb_az6007_debug & 2)
printk(KERN_CONT
"0x%02x ",
msgs[i + 1].buf[j]);
}
} else
ret = -EIO;
i++;
} else if (!(msgs[i].flags & I2C_M_RD)) {
/* write bytes */
if (dvb_usb_az6007_debug & 2)
printk(KERN_DEBUG
"az6007 I2C xfer write addr=0x%x len=%d: ",
addr, msgs[i].len);
req = AZ6007_I2C_WR;
index = msgs[i].buf[0];
value = addr | (1 << 8);
length = msgs[i].len - 1;
len = msgs[i].len - 1;
if (dvb_usb_az6007_debug & 2)
printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
for (j = 0; j < len; j++) {
st->data[j] = msgs[i].buf[j + 1];
if (dvb_usb_az6007_debug & 2)
printk(KERN_CONT "0x%02x ",
st->data[j]);
}
ret = __az6007_write(d->udev, req, value, index,
st->data, length);
} else {
/* read bytes */
if (dvb_usb_az6007_debug & 2)
printk(KERN_DEBUG
"az6007 I2C xfer read addr=0x%x len=%d: ",
addr, msgs[i].len);
req = AZ6007_I2C_RD;
index = msgs[i].buf[0];
value = addr;
length = msgs[i].len + 6;
len = msgs[i].len;
ret = __az6007_read(d->udev, req, value, index,
st->data, length);
for (j = 0; j < len; j++) {
msgs[i].buf[j] = st->data[j + 5];
if (dvb_usb_az6007_debug & 2)
printk(KERN_CONT
"0x%02x ", st->data[j + 5]);
}
}
if (dvb_usb_az6007_debug & 2)
printk(KERN_CONT "\n");
if (ret < 0)
goto err;
}
err:
mutex_unlock(&st->mutex);
if (ret < 0) {
info("%s ERROR: %i", __func__, ret);
return ret;
}
return num;
}
static u32 az6007_i2c_func(struct i2c_adapter *adapter)
{
return I2C_FUNC_I2C;
}
static struct i2c_algorithm az6007_i2c_algo = {
.master_xfer = az6007_i2c_xfer,
.functionality = az6007_i2c_func,
};
int az6007_identify_state(struct usb_device *udev,
struct dvb_usb_device_properties *props,
struct dvb_usb_device_description **desc, int *cold)
{
int ret;
u8 *mac;
mac = kmalloc(6, GFP_ATOMIC);
if (!mac)
return -ENOMEM;
/* Try to read the mac address */
ret = __az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
if (ret == 6)
*cold = 0;
else
*cold = 1;
kfree(mac);
if (*cold) {
__az6007_write(udev, 0x09, 1, 0, NULL, 0);
__az6007_write(udev, 0x00, 0, 0, NULL, 0);
__az6007_write(udev, 0x00, 0, 0, NULL, 0);
}
deb_info("Device is on %s state\n", *cold ? "warm" : "cold");
return 0;
}
static struct dvb_usb_device_properties az6007_properties;
static int az6007_usb_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
return dvb_usb_device_init(intf, &az6007_properties,
THIS_MODULE, NULL, adapter_nr);
}
static struct usb_device_id az6007_usb_table[] = {
{USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
{USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
{USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2)},
{0},
};
MODULE_DEVICE_TABLE(usb, az6007_usb_table);
static struct dvb_usb_device_properties az6007_properties = {
.caps = DVB_USB_IS_AN_I2C_ADAPTER,
.usb_ctrl = CYPRESS_FX2,
.firmware = "dvb-usb-terratec-h7-az6007.fw",
.no_reconnect = 1,
.size_of_priv = sizeof(struct az6007_device_state),
.identify_state = az6007_identify_state,
.num_adapters = 1,
.adapter = {
{
.num_frontends = 1,
.fe = {{
.streaming_ctrl = az6007_streaming_ctrl,
.tuner_attach = az6007_tuner_attach,
.frontend_attach = az6007_frontend_attach,
/* parameter for the MPEG2-data transfer */
.stream = {
.type = USB_BULK,
.count = 10,
.endpoint = 0x02,
.u = {
.bulk = {
.buffersize = 4096,
}
}
},
} }
} },
.power_ctrl = az6007_power_ctrl,
.read_mac_address = az6007_read_mac_addr,
.rc.core = {
.rc_interval = 400,
.rc_codes = RC_MAP_NEC_TERRATEC_CINERGY_XS,
.module_name = "az6007",
.rc_query = az6007_rc_query,
.allowed_protos = RC_TYPE_NEC,
},
.i2c_algo = &az6007_i2c_algo,
.num_device_descs = 2,
.devices = {
{ .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
.cold_ids = { &az6007_usb_table[0], NULL },
.warm_ids = { NULL },
},
{ .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
.cold_ids = { &az6007_usb_table[1], &az6007_usb_table[2], NULL },
.warm_ids = { NULL },
},
{ NULL },
}
};
/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver az6007_usb_driver = {
.name = "dvb_usb_az6007",
.probe = az6007_usb_probe,
.disconnect = dvb_usb_device_exit,
.id_table = az6007_usb_table,
};
/* module stuff */
static int __init az6007_usb_module_init(void)
{
int result;
deb_info("az6007 usb module init\n");
result = usb_register(&az6007_usb_driver);
if (result) {
err("usb_register failed. (%d)", result);
return result;
}
return 0;
}
static void __exit az6007_usb_module_exit(void)
{
/* deregister this driver from the USB subsystem */
deb_info("az6007 usb module exit\n");
usb_deregister(&az6007_usb_driver);
}
module_init(az6007_usb_module_init);
module_exit(az6007_usb_module_exit);
MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
MODULE_VERSION("1.1");
MODULE_LICENSE("GPL");
|