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