Commit | Line | Data |
---|---|---|
25c16466 MCC |
1 | /* |
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 | * | |
04e3ece7 MCC |
10 | * Copyright (c) 2010-2011 Mauro Carvalho Chehab <mchehab@redhat.com> |
11 | * Driver modified by in order to work with upstream drxk driver, and | |
12 | * tons of bugs got fixed. | |
25c16466 MCC |
13 | * |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation under version 2 of the License. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
71d67634 MCC |
22 | */ |
23 | ||
71d67634 MCC |
24 | #include "drxk.h" |
25 | #include "mt2063.h" | |
26 | #include "dvb_ca_en50221.h" | |
255d52f2 MCC |
27 | #include "dvb_usb.h" |
28 | #include "cypress_firmware.h" | |
70fa444d | 29 | |
255d52f2 | 30 | #define AZ6007_FIRMWARE "dvb-usb-terratec-h7-az6007.fw" |
71d67634 | 31 | |
255d52f2 MCC |
32 | static int az6007_xfer_debug; |
33 | module_param_named(xfer_debug, az6007_xfer_debug, int, 0644); | |
34 | MODULE_PARM_DESC(xfer_debug, "Enable xfer debug"); | |
70fa444d | 35 | |
71d67634 MCC |
36 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
37 | ||
3aecf2c5 MCC |
38 | /* Known requests (Cypress FX2 firmware + az6007 "private" ones*/ |
39 | ||
40 | #define FX2_OED 0xb5 | |
41 | #define AZ6007_READ_DATA 0xb7 | |
42 | #define AZ6007_I2C_RD 0xb9 | |
43 | #define AZ6007_POWER 0xbc | |
44 | #define AZ6007_I2C_WR 0xbd | |
45 | #define FX2_SCON1 0xc0 | |
46 | #define AZ6007_TS_THROUGH 0xc7 | |
2d5161b7 | 47 | #define AZ6007_READ_IR 0xb4 |
3aecf2c5 | 48 | |
71d67634 | 49 | struct az6007_device_state { |
a2c35d34 | 50 | struct mutex mutex; |
962f8f67 | 51 | struct mutex ca_mutex; |
a2c35d34 MCC |
52 | struct dvb_ca_en50221 ca; |
53 | unsigned warm:1; | |
93b32126 | 54 | int (*gate_ctrl) (struct dvb_frontend *, int); |
b19280cf | 55 | unsigned char data[4096]; |
71d67634 MCC |
56 | }; |
57 | ||
93b32126 | 58 | static struct drxk_config terratec_h7_drxk = { |
6da34706 | 59 | .adr = 0x29, |
6e5caf84 MCC |
60 | .parallel_ts = true, |
61 | .dynamic_clk = true, | |
62 | .single_master = true, | |
d5856813 | 63 | .enable_merr_cfg = true, |
6e5caf84 | 64 | .no_i2c_bridge = false, |
781dacc8 | 65 | .chunk_size = 64, |
6fb65a66 | 66 | .mpeg_out_clk_strength = 0x02, |
caeb5ac7 | 67 | .qam_demod_parameter_count = 2, |
b19280cf | 68 | .microcode_name = "dvb-usb-terratec-h7-drxk.fw", |
71d67634 MCC |
69 | }; |
70 | ||
6da34706 MCC |
71 | static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable) |
72 | { | |
255d52f2 | 73 | struct az6007_device_state *st = fe_to_priv(fe); |
6da34706 | 74 | struct dvb_usb_adapter *adap = fe->sec_priv; |
f2ba9e5d | 75 | int status = 0; |
6da34706 | 76 | |
255d52f2 | 77 | pr_debug("%s: %s\n", __func__, enable ? "enable" : "disable"); |
caa1a700 | 78 | |
255d52f2 | 79 | if (!adap || !st) |
6da34706 MCC |
80 | return -EINVAL; |
81 | ||
04e3ece7 | 82 | if (enable) |
6da34706 | 83 | status = st->gate_ctrl(fe, 1); |
04e3ece7 | 84 | else |
6da34706 | 85 | status = st->gate_ctrl(fe, 0); |
04e3ece7 | 86 | |
6da34706 MCC |
87 | return status; |
88 | } | |
89 | ||
93b32126 | 90 | static struct mt2063_config az6007_mt2063_config = { |
caa1a700 | 91 | .tuner_address = 0x60, |
71d67634 MCC |
92 | .refclock = 36125000, |
93 | }; | |
94 | ||
a2c35d34 | 95 | static int __az6007_read(struct usb_device *udev, u8 req, u16 value, |
93b32126 | 96 | u16 index, u8 *b, int blen) |
71d67634 | 97 | { |
9b01f3d4 | 98 | int ret; |
71d67634 | 99 | |
3af2f4f1 MCC |
100 | ret = usb_control_msg(udev, |
101 | usb_rcvctrlpipe(udev, 0), | |
93b32126 MCC |
102 | req, |
103 | USB_TYPE_VENDOR | USB_DIR_IN, | |
104 | value, index, b, blen, 5000); | |
71d67634 | 105 | if (ret < 0) { |
255d52f2 | 106 | pr_warn("usb read operation failed. (%d)\n", ret); |
caa1a700 MCC |
107 | return -EIO; |
108 | } | |
71d67634 | 109 | |
255d52f2 MCC |
110 | if (az6007_xfer_debug) { |
111 | printk(KERN_DEBUG "az6007: IN req: %02x, value: %04x, index: %04x\n", | |
112 | req, value, index); | |
113 | print_hex_dump_bytes("az6007: payload: ", | |
114 | DUMP_PREFIX_NONE, b, blen); | |
115 | } | |
71d67634 MCC |
116 | |
117 | return ret; | |
118 | } | |
119 | ||
a2c35d34 MCC |
120 | static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value, |
121 | u16 index, u8 *b, int blen) | |
122 | { | |
123 | struct az6007_device_state *st = d->priv; | |
124 | int ret; | |
125 | ||
126 | if (mutex_lock_interruptible(&st->mutex) < 0) | |
127 | return -EAGAIN; | |
128 | ||
129 | ret = __az6007_read(d->udev, req, value, index, b, blen); | |
130 | ||
131 | mutex_unlock(&st->mutex); | |
132 | ||
133 | return ret; | |
134 | } | |
135 | ||
136 | static int __az6007_write(struct usb_device *udev, u8 req, u16 value, | |
71d67634 MCC |
137 | u16 index, u8 *b, int blen) |
138 | { | |
139 | int ret; | |
140 | ||
255d52f2 MCC |
141 | if (az6007_xfer_debug) { |
142 | printk(KERN_DEBUG "az6007: OUT req: %02x, value: %04x, index: %04x\n", | |
143 | req, value, index); | |
144 | print_hex_dump_bytes("az6007: payload: ", | |
145 | DUMP_PREFIX_NONE, b, blen); | |
146 | } | |
71d67634 | 147 | |
35753580 | 148 | if (blen > 64) { |
255d52f2 MCC |
149 | pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n", |
150 | blen); | |
35753580 | 151 | return -EOPNOTSUPP; |
71d67634 | 152 | } |
35753580 | 153 | |
3af2f4f1 MCC |
154 | ret = usb_control_msg(udev, |
155 | usb_sndctrlpipe(udev, 0), | |
93b32126 MCC |
156 | req, |
157 | USB_TYPE_VENDOR | USB_DIR_OUT, | |
158 | value, index, b, blen, 5000); | |
159 | if (ret != blen) { | |
255d52f2 | 160 | pr_err("usb write operation failed. (%d)\n", ret); |
35753580 | 161 | return -EIO; |
71d67634 | 162 | } |
6da34706 | 163 | |
71d67634 MCC |
164 | return 0; |
165 | } | |
166 | ||
a2c35d34 MCC |
167 | static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value, |
168 | u16 index, u8 *b, int blen) | |
169 | { | |
170 | struct az6007_device_state *st = d->priv; | |
171 | int ret; | |
172 | ||
173 | if (mutex_lock_interruptible(&st->mutex) < 0) | |
174 | return -EAGAIN; | |
175 | ||
176 | ret = __az6007_write(d->udev, req, value, index, b, blen); | |
177 | ||
178 | mutex_unlock(&st->mutex); | |
179 | ||
180 | return ret; | |
181 | } | |
182 | ||
255d52f2 | 183 | static int az6007_streaming_ctrl(struct dvb_frontend *fe, int onoff) |
71d67634 | 184 | { |
255d52f2 | 185 | struct dvb_usb_device *d = fe_to_d(fe); |
a2c35d34 | 186 | |
255d52f2 | 187 | pr_debug("%s: %s\n", __func__, onoff ? "enable" : "disable"); |
b19280cf | 188 | |
a2c35d34 | 189 | return az6007_write(d, 0xbc, onoff, 0, NULL, 0); |
71d67634 MCC |
190 | } |
191 | ||
71d67634 | 192 | /* remote control stuff (does not work with my box) */ |
d3d076aa | 193 | static int az6007_rc_query(struct dvb_usb_device *d) |
71d67634 | 194 | { |
255d52f2 | 195 | struct az6007_device_state *st = d_to_priv(d); |
91651440 | 196 | unsigned code = 0; |
71d67634 | 197 | |
711e1398 | 198 | az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10); |
71d67634 | 199 | |
d3d076aa | 200 | if (st->data[1] == 0x44) |
71d67634 | 201 | return 0; |
71d67634 | 202 | |
91651440 MCC |
203 | if ((st->data[1] ^ st->data[2]) == 0xff) |
204 | code = st->data[1]; | |
205 | else | |
206 | code = st->data[1] << 8 | st->data[2]; | |
207 | ||
208 | if ((st->data[3] ^ st->data[4]) == 0xff) | |
209 | code = code << 8 | st->data[3]; | |
210 | else | |
9e5e3097 | 211 | code = code << 16 | st->data[3] << 8 | st->data[4]; |
2d5161b7 | 212 | |
d3d076aa | 213 | rc_keydown(d->rc_dev, code, st->data[5]); |
2d5161b7 | 214 | |
71d67634 | 215 | return 0; |
71d67634 MCC |
216 | } |
217 | ||
962f8f67 JAR |
218 | static int az6007_ci_read_attribute_mem(struct dvb_ca_en50221 *ca, |
219 | int slot, | |
220 | int address) | |
221 | { | |
222 | struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; | |
255d52f2 | 223 | struct az6007_device_state *state = d_to_priv(d); |
962f8f67 JAR |
224 | |
225 | int ret; | |
226 | u8 req; | |
227 | u16 value; | |
228 | u16 index; | |
229 | int blen; | |
230 | u8 *b; | |
231 | ||
232 | if (slot != 0) | |
233 | return -EINVAL; | |
234 | ||
235 | b = kmalloc(12, GFP_KERNEL); | |
236 | if (!b) | |
237 | return -ENOMEM; | |
238 | ||
239 | mutex_lock(&state->ca_mutex); | |
240 | ||
241 | req = 0xC1; | |
242 | value = address; | |
243 | index = 0; | |
244 | blen = 1; | |
245 | ||
246 | ret = az6007_read(d, req, value, index, b, blen); | |
247 | if (ret < 0) { | |
255d52f2 | 248 | pr_warn("usb in operation failed. (%d)\n", ret); |
962f8f67 JAR |
249 | ret = -EINVAL; |
250 | } else { | |
251 | ret = b[0]; | |
252 | } | |
253 | ||
254 | mutex_unlock(&state->ca_mutex); | |
255 | kfree(b); | |
256 | return ret; | |
257 | } | |
258 | ||
259 | static int az6007_ci_write_attribute_mem(struct dvb_ca_en50221 *ca, | |
260 | int slot, | |
261 | int address, | |
262 | u8 value) | |
263 | { | |
264 | struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; | |
255d52f2 | 265 | struct az6007_device_state *state = d_to_priv(d); |
962f8f67 JAR |
266 | |
267 | int ret; | |
268 | u8 req; | |
269 | u16 value1; | |
270 | u16 index; | |
271 | int blen; | |
272 | ||
255d52f2 | 273 | pr_debug("%s(), slot %d\n", __func__, slot); |
962f8f67 JAR |
274 | if (slot != 0) |
275 | return -EINVAL; | |
276 | ||
277 | mutex_lock(&state->ca_mutex); | |
278 | req = 0xC2; | |
279 | value1 = address; | |
280 | index = value; | |
281 | blen = 0; | |
282 | ||
283 | ret = az6007_write(d, req, value1, index, NULL, blen); | |
284 | if (ret != 0) | |
255d52f2 | 285 | pr_warn("usb out operation failed. (%d)\n", ret); |
962f8f67 JAR |
286 | |
287 | mutex_unlock(&state->ca_mutex); | |
288 | return ret; | |
289 | } | |
290 | ||
291 | static int az6007_ci_read_cam_control(struct dvb_ca_en50221 *ca, | |
292 | int slot, | |
293 | u8 address) | |
294 | { | |
295 | struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; | |
255d52f2 | 296 | struct az6007_device_state *state = d_to_priv(d); |
962f8f67 JAR |
297 | |
298 | int ret; | |
299 | u8 req; | |
300 | u16 value; | |
301 | u16 index; | |
302 | int blen; | |
303 | u8 *b; | |
304 | ||
305 | if (slot != 0) | |
306 | return -EINVAL; | |
307 | ||
308 | b = kmalloc(12, GFP_KERNEL); | |
309 | if (!b) | |
310 | return -ENOMEM; | |
311 | ||
312 | mutex_lock(&state->ca_mutex); | |
313 | ||
314 | req = 0xC3; | |
315 | value = address; | |
316 | index = 0; | |
317 | blen = 2; | |
318 | ||
319 | ret = az6007_read(d, req, value, index, b, blen); | |
320 | if (ret < 0) { | |
255d52f2 | 321 | pr_warn("usb in operation failed. (%d)\n", ret); |
962f8f67 JAR |
322 | ret = -EINVAL; |
323 | } else { | |
324 | if (b[0] == 0) | |
255d52f2 | 325 | pr_warn("Read CI IO error\n"); |
962f8f67 JAR |
326 | |
327 | ret = b[1]; | |
255d52f2 | 328 | pr_debug("read cam data = %x from 0x%x\n", b[1], value); |
962f8f67 JAR |
329 | } |
330 | ||
331 | mutex_unlock(&state->ca_mutex); | |
332 | kfree(b); | |
333 | return ret; | |
334 | } | |
335 | ||
336 | static int az6007_ci_write_cam_control(struct dvb_ca_en50221 *ca, | |
337 | int slot, | |
338 | u8 address, | |
339 | u8 value) | |
340 | { | |
341 | struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; | |
255d52f2 | 342 | struct az6007_device_state *state = d_to_priv(d); |
962f8f67 JAR |
343 | |
344 | int ret; | |
345 | u8 req; | |
346 | u16 value1; | |
347 | u16 index; | |
348 | int blen; | |
349 | ||
350 | if (slot != 0) | |
351 | return -EINVAL; | |
352 | ||
353 | mutex_lock(&state->ca_mutex); | |
354 | req = 0xC4; | |
355 | value1 = address; | |
356 | index = value; | |
357 | blen = 0; | |
358 | ||
359 | ret = az6007_write(d, req, value1, index, NULL, blen); | |
360 | if (ret != 0) { | |
255d52f2 | 361 | pr_warn("usb out operation failed. (%d)\n", ret); |
962f8f67 JAR |
362 | goto failed; |
363 | } | |
364 | ||
365 | failed: | |
366 | mutex_unlock(&state->ca_mutex); | |
367 | return ret; | |
368 | } | |
369 | ||
370 | static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot) | |
371 | { | |
372 | struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; | |
373 | ||
374 | int ret; | |
375 | u8 req; | |
376 | u16 value; | |
377 | u16 index; | |
378 | int blen; | |
379 | u8 *b; | |
380 | ||
381 | b = kmalloc(12, GFP_KERNEL); | |
382 | if (!b) | |
383 | return -ENOMEM; | |
384 | ||
385 | req = 0xC8; | |
386 | value = 0; | |
387 | index = 0; | |
388 | blen = 1; | |
389 | ||
390 | ret = az6007_read(d, req, value, index, b, blen); | |
391 | if (ret < 0) { | |
255d52f2 | 392 | pr_warn("usb in operation failed. (%d)\n", ret); |
962f8f67 JAR |
393 | ret = -EIO; |
394 | } else{ | |
395 | ret = b[0]; | |
396 | } | |
397 | kfree(b); | |
398 | return ret; | |
399 | } | |
400 | ||
401 | static int az6007_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot) | |
402 | { | |
403 | struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; | |
255d52f2 | 404 | struct az6007_device_state *state = d_to_priv(d); |
962f8f67 JAR |
405 | |
406 | int ret, i; | |
407 | u8 req; | |
408 | u16 value; | |
409 | u16 index; | |
410 | int blen; | |
411 | ||
412 | mutex_lock(&state->ca_mutex); | |
413 | ||
414 | req = 0xC6; | |
415 | value = 1; | |
416 | index = 0; | |
417 | blen = 0; | |
418 | ||
419 | ret = az6007_write(d, req, value, index, NULL, blen); | |
420 | if (ret != 0) { | |
255d52f2 | 421 | pr_warn("usb out operation failed. (%d)\n", ret); |
962f8f67 JAR |
422 | goto failed; |
423 | } | |
424 | ||
425 | msleep(500); | |
426 | req = 0xC6; | |
427 | value = 0; | |
428 | index = 0; | |
429 | blen = 0; | |
430 | ||
431 | ret = az6007_write(d, req, value, index, NULL, blen); | |
432 | if (ret != 0) { | |
255d52f2 | 433 | pr_warn("usb out operation failed. (%d)\n", ret); |
962f8f67 JAR |
434 | goto failed; |
435 | } | |
436 | ||
437 | for (i = 0; i < 15; i++) { | |
438 | msleep(100); | |
439 | ||
440 | if (CI_CamReady(ca, slot)) { | |
255d52f2 | 441 | pr_debug("CAM Ready\n"); |
962f8f67 JAR |
442 | break; |
443 | } | |
444 | } | |
445 | msleep(5000); | |
446 | ||
447 | failed: | |
448 | mutex_unlock(&state->ca_mutex); | |
449 | return ret; | |
450 | } | |
451 | ||
452 | static int az6007_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot) | |
453 | { | |
454 | return 0; | |
455 | } | |
456 | ||
457 | static int az6007_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot) | |
458 | { | |
459 | struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; | |
255d52f2 | 460 | struct az6007_device_state *state = d_to_priv(d); |
962f8f67 JAR |
461 | |
462 | int ret; | |
463 | u8 req; | |
464 | u16 value; | |
465 | u16 index; | |
466 | int blen; | |
467 | ||
255d52f2 | 468 | pr_debug("%s()\n", __func__); |
962f8f67 JAR |
469 | mutex_lock(&state->ca_mutex); |
470 | req = 0xC7; | |
471 | value = 1; | |
472 | index = 0; | |
473 | blen = 0; | |
474 | ||
475 | ret = az6007_write(d, req, value, index, NULL, blen); | |
476 | if (ret != 0) { | |
255d52f2 | 477 | pr_warn("usb out operation failed. (%d)\n", ret); |
962f8f67 JAR |
478 | goto failed; |
479 | } | |
480 | ||
481 | failed: | |
482 | mutex_unlock(&state->ca_mutex); | |
483 | return ret; | |
484 | } | |
485 | ||
486 | static int az6007_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open) | |
487 | { | |
488 | struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; | |
255d52f2 | 489 | struct az6007_device_state *state = d_to_priv(d); |
962f8f67 JAR |
490 | int ret; |
491 | u8 req; | |
492 | u16 value; | |
493 | u16 index; | |
494 | int blen; | |
495 | u8 *b; | |
496 | ||
497 | b = kmalloc(12, GFP_KERNEL); | |
498 | if (!b) | |
499 | return -ENOMEM; | |
500 | mutex_lock(&state->ca_mutex); | |
501 | ||
502 | req = 0xC5; | |
503 | value = 0; | |
504 | index = 0; | |
505 | blen = 1; | |
506 | ||
507 | ret = az6007_read(d, req, value, index, b, blen); | |
508 | if (ret < 0) { | |
255d52f2 | 509 | pr_warn("usb in operation failed. (%d)\n", ret); |
962f8f67 JAR |
510 | ret = -EIO; |
511 | } else | |
512 | ret = 0; | |
513 | ||
514 | if (!ret && b[0] == 1) { | |
515 | ret = DVB_CA_EN50221_POLL_CAM_PRESENT | | |
516 | DVB_CA_EN50221_POLL_CAM_READY; | |
517 | } | |
518 | ||
519 | mutex_unlock(&state->ca_mutex); | |
520 | kfree(b); | |
521 | return ret; | |
522 | } | |
523 | ||
524 | ||
525 | static void az6007_ci_uninit(struct dvb_usb_device *d) | |
526 | { | |
527 | struct az6007_device_state *state; | |
528 | ||
255d52f2 | 529 | pr_debug("%s()\n", __func__); |
962f8f67 JAR |
530 | |
531 | if (NULL == d) | |
532 | return; | |
533 | ||
255d52f2 | 534 | state = d_to_priv(d); |
962f8f67 JAR |
535 | if (NULL == state) |
536 | return; | |
537 | ||
538 | if (NULL == state->ca.data) | |
539 | return; | |
540 | ||
541 | dvb_ca_en50221_release(&state->ca); | |
542 | ||
543 | memset(&state->ca, 0, sizeof(state->ca)); | |
544 | } | |
545 | ||
546 | ||
255d52f2 | 547 | static int az6007_ci_init(struct dvb_usb_adapter *adap) |
962f8f67 | 548 | { |
255d52f2 MCC |
549 | struct dvb_usb_device *d = adap_to_d(adap); |
550 | struct az6007_device_state *state = adap_to_priv(adap); | |
962f8f67 JAR |
551 | int ret; |
552 | ||
255d52f2 | 553 | pr_debug("%s()\n", __func__); |
962f8f67 JAR |
554 | |
555 | mutex_init(&state->ca_mutex); | |
962f8f67 JAR |
556 | state->ca.owner = THIS_MODULE; |
557 | state->ca.read_attribute_mem = az6007_ci_read_attribute_mem; | |
558 | state->ca.write_attribute_mem = az6007_ci_write_attribute_mem; | |
559 | state->ca.read_cam_control = az6007_ci_read_cam_control; | |
560 | state->ca.write_cam_control = az6007_ci_write_cam_control; | |
561 | state->ca.slot_reset = az6007_ci_slot_reset; | |
562 | state->ca.slot_shutdown = az6007_ci_slot_shutdown; | |
563 | state->ca.slot_ts_enable = az6007_ci_slot_ts_enable; | |
564 | state->ca.poll_slot_status = az6007_ci_poll_slot_status; | |
565 | state->ca.data = d; | |
566 | ||
255d52f2 | 567 | ret = dvb_ca_en50221_init(&adap->dvb_adap, |
962f8f67 JAR |
568 | &state->ca, |
569 | 0, /* flags */ | |
570 | 1);/* n_slots */ | |
571 | if (ret != 0) { | |
255d52f2 | 572 | pr_err("Cannot initialize CI: Error %d.\n", ret); |
962f8f67 JAR |
573 | memset(&state->ca, 0, sizeof(state->ca)); |
574 | return ret; | |
575 | } | |
576 | ||
255d52f2 | 577 | pr_debug("CI initialized.\n"); |
962f8f67 JAR |
578 | |
579 | return 0; | |
580 | } | |
581 | ||
255d52f2 | 582 | static int az6007_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6]) |
71d67634 | 583 | { |
255d52f2 MCC |
584 | struct dvb_usb_device *d = adap_to_d(adap); |
585 | struct az6007_device_state *st = adap_to_priv(adap); | |
3aecf2c5 | 586 | int ret; |
711e1398 MCC |
587 | |
588 | ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6); | |
5a7a570b | 589 | memcpy(mac, st->data, 6); |
71d67634 | 590 | |
3aecf2c5 | 591 | if (ret > 0) |
255d52f2 | 592 | pr_debug("%s: mac is %pM\n", __func__, mac); |
3aecf2c5 MCC |
593 | |
594 | return ret; | |
595 | } | |
81091144 | 596 | |
71d67634 MCC |
597 | static int az6007_frontend_attach(struct dvb_usb_adapter *adap) |
598 | { | |
255d52f2 MCC |
599 | struct az6007_device_state *st = adap_to_priv(adap); |
600 | struct dvb_usb_device *d = adap_to_d(adap); | |
6da34706 | 601 | |
255d52f2 | 602 | pr_debug("attaching demod drxk\n"); |
71d67634 | 603 | |
255d52f2 MCC |
604 | adap->fe[0] = dvb_attach(drxk_attach, &terratec_h7_drxk, |
605 | &d->i2c_adap); | |
606 | if (!adap->fe[0]) | |
1c9a284f | 607 | return -EINVAL; |
caa1a700 | 608 | |
255d52f2 MCC |
609 | adap->fe[0]->sec_priv = adap; |
610 | st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl; | |
611 | adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl; | |
6da34706 | 612 | |
962f8f67 JAR |
613 | az6007_ci_init(adap); |
614 | ||
1c9a284f MCC |
615 | return 0; |
616 | } | |
617 | ||
618 | static int az6007_tuner_attach(struct dvb_usb_adapter *adap) | |
619 | { | |
255d52f2 MCC |
620 | struct dvb_usb_device *d = adap_to_d(adap); |
621 | ||
622 | pr_debug("attaching tuner mt2063\n"); | |
04e3ece7 | 623 | |
6da34706 | 624 | /* Attach mt2063 to DVB-C frontend */ |
255d52f2 MCC |
625 | if (adap->fe[0]->ops.i2c_gate_ctrl) |
626 | adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 1); | |
627 | if (!dvb_attach(mt2063_attach, adap->fe[0], | |
781dacc8 | 628 | &az6007_mt2063_config, |
255d52f2 | 629 | &d->i2c_adap)) |
1c9a284f | 630 | return -EINVAL; |
6da34706 | 631 | |
255d52f2 MCC |
632 | if (adap->fe[0]->ops.i2c_gate_ctrl) |
633 | adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 0); | |
da989e0b | 634 | |
71d67634 MCC |
635 | return 0; |
636 | } | |
637 | ||
81091144 MCC |
638 | int az6007_power_ctrl(struct dvb_usb_device *d, int onoff) |
639 | { | |
255d52f2 | 640 | struct az6007_device_state *st = d_to_priv(d); |
b19280cf MCC |
641 | int ret; |
642 | ||
255d52f2 | 643 | pr_debug("%s()\n", __func__); |
b19280cf MCC |
644 | |
645 | if (!st->warm) { | |
a2c35d34 | 646 | mutex_init(&st->mutex); |
b19280cf | 647 | |
a2c35d34 | 648 | ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0); |
b19280cf MCC |
649 | if (ret < 0) |
650 | return ret; | |
651 | msleep(60); | |
a2c35d34 | 652 | ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0); |
b19280cf MCC |
653 | if (ret < 0) |
654 | return ret; | |
655 | msleep(100); | |
a2c35d34 | 656 | ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0); |
b19280cf MCC |
657 | if (ret < 0) |
658 | return ret; | |
659 | msleep(20); | |
a2c35d34 | 660 | ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0); |
b19280cf MCC |
661 | if (ret < 0) |
662 | return ret; | |
663 | ||
664 | msleep(400); | |
a2c35d34 | 665 | ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0); |
b19280cf MCC |
666 | if (ret < 0) |
667 | return ret; | |
9e5e3097 | 668 | msleep(150); |
a2c35d34 | 669 | ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0); |
b19280cf MCC |
670 | if (ret < 0) |
671 | return ret; | |
9e5e3097 | 672 | msleep(430); |
a2c35d34 | 673 | ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0); |
b19280cf MCC |
674 | if (ret < 0) |
675 | return ret; | |
81091144 | 676 | |
b19280cf | 677 | st->warm = true; |
81091144 | 678 | |
b19280cf MCC |
679 | return 0; |
680 | } | |
81091144 | 681 | |
b19280cf MCC |
682 | if (!onoff) |
683 | return 0; | |
684 | ||
a2c35d34 MCC |
685 | az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0); |
686 | az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0); | |
81091144 | 687 | |
81091144 MCC |
688 | return 0; |
689 | } | |
690 | ||
71d67634 | 691 | /* I2C */ |
93b32126 MCC |
692 | static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], |
693 | int num) | |
71d67634 MCC |
694 | { |
695 | struct dvb_usb_device *d = i2c_get_adapdata(adap); | |
255d52f2 | 696 | struct az6007_device_state *st = d_to_priv(d); |
caa1a700 MCC |
697 | int i, j, len; |
698 | int ret = 0; | |
71d67634 MCC |
699 | u16 index; |
700 | u16 value; | |
701 | int length; | |
caa1a700 | 702 | u8 req, addr; |
6da34706 | 703 | |
a2c35d34 | 704 | if (mutex_lock_interruptible(&st->mutex) < 0) |
71d67634 | 705 | return -EAGAIN; |
71d67634 | 706 | |
caa1a700 MCC |
707 | for (i = 0; i < num; i++) { |
708 | addr = msgs[i].addr << 1; | |
caa1a700 MCC |
709 | if (((i + 1) < num) |
710 | && (msgs[i].len == 1) | |
916c8142 | 711 | && ((msgs[i].flags & I2C_M_RD) != I2C_M_RD) |
caa1a700 MCC |
712 | && (msgs[i + 1].flags & I2C_M_RD) |
713 | && (msgs[i].addr == msgs[i + 1].addr)) { | |
714 | /* | |
715 | * A write + read xfer for the same address, where | |
716 | * the first xfer has just 1 byte length. | |
717 | * Need to join both into one operation | |
718 | */ | |
255d52f2 MCC |
719 | if (az6007_xfer_debug) |
720 | printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n", | |
d20a7f72 | 721 | addr, msgs[i].len, msgs[i + 1].len); |
3aecf2c5 | 722 | req = AZ6007_I2C_RD; |
d20a7f72 MCC |
723 | index = msgs[i].buf[0]; |
724 | value = addr | (1 << 8); | |
caa1a700 MCC |
725 | length = 6 + msgs[i + 1].len; |
726 | len = msgs[i + 1].len; | |
9e5e3097 MCC |
727 | ret = __az6007_read(d->udev, req, value, index, |
728 | st->data, length); | |
caa1a700 | 729 | if (ret >= len) { |
255d52f2 | 730 | for (j = 0; j < len; j++) |
b19280cf | 731 | msgs[i + 1].buf[j] = st->data[j + 5]; |
caa1a700 MCC |
732 | } else |
733 | ret = -EIO; | |
734 | i++; | |
735 | } else if (!(msgs[i].flags & I2C_M_RD)) { | |
736 | /* write bytes */ | |
255d52f2 MCC |
737 | if (az6007_xfer_debug) |
738 | printk(KERN_DEBUG "az6007: I2C W addr=0x%x len=%d\n", | |
d20a7f72 | 739 | addr, msgs[i].len); |
3aecf2c5 | 740 | req = AZ6007_I2C_WR; |
caa1a700 MCC |
741 | index = msgs[i].buf[0]; |
742 | value = addr | (1 << 8); | |
743 | length = msgs[i].len - 1; | |
744 | len = msgs[i].len - 1; | |
255d52f2 | 745 | for (j = 0; j < len; j++) |
b19280cf | 746 | st->data[j] = msgs[i].buf[j + 1]; |
9e5e3097 MCC |
747 | ret = __az6007_write(d->udev, req, value, index, |
748 | st->data, length); | |
caa1a700 MCC |
749 | } else { |
750 | /* read bytes */ | |
255d52f2 MCC |
751 | if (az6007_xfer_debug) |
752 | printk(KERN_DEBUG "az6007: I2C R addr=0x%x len=%d\n", | |
d20a7f72 | 753 | addr, msgs[i].len); |
3aecf2c5 | 754 | req = AZ6007_I2C_RD; |
caa1a700 MCC |
755 | index = msgs[i].buf[0]; |
756 | value = addr; | |
757 | length = msgs[i].len + 6; | |
758 | len = msgs[i].len; | |
9e5e3097 MCC |
759 | ret = __az6007_read(d->udev, req, value, index, |
760 | st->data, length); | |
255d52f2 | 761 | for (j = 0; j < len; j++) |
b19280cf | 762 | msgs[i].buf[j] = st->data[j + 5]; |
71d67634 | 763 | } |
caa1a700 MCC |
764 | if (ret < 0) |
765 | goto err; | |
71d67634 | 766 | } |
caa1a700 | 767 | err: |
a2c35d34 | 768 | mutex_unlock(&st->mutex); |
caa1a700 MCC |
769 | |
770 | if (ret < 0) { | |
255d52f2 | 771 | pr_info("%s ERROR: %i\n", __func__, ret); |
caa1a700 MCC |
772 | return ret; |
773 | } | |
774 | return num; | |
71d67634 MCC |
775 | } |
776 | ||
71d67634 MCC |
777 | static u32 az6007_i2c_func(struct i2c_adapter *adapter) |
778 | { | |
779 | return I2C_FUNC_I2C; | |
780 | } | |
781 | ||
782 | static struct i2c_algorithm az6007_i2c_algo = { | |
93b32126 | 783 | .master_xfer = az6007_i2c_xfer, |
71d67634 | 784 | .functionality = az6007_i2c_func, |
71d67634 MCC |
785 | }; |
786 | ||
255d52f2 | 787 | int az6007_identify_state(struct dvb_usb_device *d, const char **name) |
71d67634 | 788 | { |
3aecf2c5 | 789 | int ret; |
711e1398 MCC |
790 | u8 *mac; |
791 | ||
255d52f2 MCC |
792 | pr_debug("Identifying az6007 state\n"); |
793 | ||
711e1398 MCC |
794 | mac = kmalloc(6, GFP_ATOMIC); |
795 | if (!mac) | |
796 | return -ENOMEM; | |
71d67634 | 797 | |
3aecf2c5 | 798 | /* Try to read the mac address */ |
255d52f2 | 799 | ret = __az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6); |
3aecf2c5 | 800 | if (ret == 6) |
255d52f2 | 801 | ret = WARM; |
3aecf2c5 | 802 | else |
255d52f2 | 803 | ret = COLD; |
71d67634 | 804 | |
711e1398 MCC |
805 | kfree(mac); |
806 | ||
255d52f2 MCC |
807 | if (ret == COLD) { |
808 | __az6007_write(d->udev, 0x09, 1, 0, NULL, 0); | |
809 | __az6007_write(d->udev, 0x00, 0, 0, NULL, 0); | |
810 | __az6007_write(d->udev, 0x00, 0, 0, NULL, 0); | |
b19280cf MCC |
811 | } |
812 | ||
255d52f2 MCC |
813 | pr_debug("Device is on %s state\n", |
814 | ret == WARM ? "warm" : "cold"); | |
815 | return ret; | |
71d67634 MCC |
816 | } |
817 | ||
962f8f67 JAR |
818 | static void az6007_usb_disconnect(struct usb_interface *intf) |
819 | { | |
820 | struct dvb_usb_device *d = usb_get_intfdata(intf); | |
821 | az6007_ci_uninit(d); | |
255d52f2 | 822 | dvb_usbv2_disconnect(intf); |
962f8f67 JAR |
823 | } |
824 | ||
255d52f2 | 825 | static int az6007_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc) |
71d67634 | 826 | { |
255d52f2 MCC |
827 | pr_debug("Getting az6007 Remote Control properties\n"); |
828 | ||
829 | rc->allowed_protos = RC_TYPE_NEC; | |
830 | rc->query = az6007_rc_query; | |
831 | rc->interval = 400; | |
832 | ||
833 | return 0; | |
71d67634 MCC |
834 | } |
835 | ||
255d52f2 MCC |
836 | static int az6007_download_firmware(struct dvb_usb_device *d, |
837 | const struct firmware *fw) | |
838 | { | |
839 | pr_debug("Loading az6007 firmware\n"); | |
71d67634 | 840 | |
255d52f2 MCC |
841 | return usbv2_cypress_load_firmware(d->udev, fw, CYPRESS_FX2); |
842 | } | |
71d67634 | 843 | |
255d52f2 MCC |
844 | /* DVB USB Driver stuff */ |
845 | static struct dvb_usb_device_properties az6007_props = { | |
846 | .driver_name = KBUILD_MODNAME, | |
847 | .owner = THIS_MODULE, | |
848 | .firmware = AZ6007_FIRMWARE, | |
849 | ||
850 | .adapter_nr = adapter_nr, | |
978c26c3 | 851 | .size_of_priv = sizeof(struct az6007_device_state), |
255d52f2 MCC |
852 | .i2c_algo = &az6007_i2c_algo, |
853 | .tuner_attach = az6007_tuner_attach, | |
854 | .frontend_attach = az6007_frontend_attach, | |
855 | .streaming_ctrl = az6007_streaming_ctrl, | |
856 | .get_rc_config = az6007_get_rc_config, | |
857 | .read_mac_address = az6007_read_mac_addr, | |
858 | .download_firmware = az6007_download_firmware, | |
978c26c3 | 859 | .identify_state = az6007_identify_state, |
255d52f2 MCC |
860 | .power_ctrl = az6007_power_ctrl, |
861 | .num_adapters = 1, | |
862 | .adapter = { | |
863 | { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), } | |
71d67634 MCC |
864 | } |
865 | }; | |
6da34706 | 866 | |
255d52f2 MCC |
867 | static struct usb_device_id az6007_usb_table[] = { |
868 | {DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007, | |
869 | &az6007_props, "Azurewave 6007", RC_MAP_EMPTY)}, | |
870 | {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7, | |
871 | &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)}, | |
872 | {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2, | |
873 | &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)}, | |
874 | {0}, | |
875 | }; | |
876 | ||
877 | MODULE_DEVICE_TABLE(usb, az6007_usb_table); | |
878 | ||
71d67634 MCC |
879 | /* usb specific object needed to register this driver with the usb subsystem */ |
880 | static struct usb_driver az6007_usb_driver = { | |
255d52f2 | 881 | .name = KBUILD_MODNAME, |
93b32126 | 882 | .id_table = az6007_usb_table, |
255d52f2 MCC |
883 | .probe = dvb_usbv2_probe, |
884 | .disconnect = az6007_usb_disconnect, | |
885 | .suspend = dvb_usbv2_suspend, | |
886 | .resume = dvb_usbv2_resume, | |
887 | .no_dynamic_id = 1, | |
888 | .soft_unbind = 1, | |
71d67634 MCC |
889 | }; |
890 | ||
255d52f2 | 891 | module_usb_driver(az6007_usb_driver); |
71d67634 MCC |
892 | |
893 | MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>"); | |
04e3ece7 | 894 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
71d67634 | 895 | MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones"); |
255d52f2 | 896 | MODULE_VERSION("2.0"); |
71d67634 | 897 | MODULE_LICENSE("GPL"); |
255d52f2 | 898 | MODULE_FIRMWARE(AZ6007_FIRMWARE); |