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