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