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