tuner-xc2028.c 35.3 KB
Newer Older
1 2
/* tuner-xc2028
 *
3
 * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org)
4
 *
5 6
 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
 *       - frontend interface
7
 *
8 9 10 11 12 13
 * This code is placed under the terms of the GNU General Public License v2
 */

#include <linux/i2c.h>
#include <asm/div64.h>
#include <linux/firmware.h>
14
#include <linux/videodev2.h>
15
#include <linux/delay.h>
16
#include <media/tuner.h>
17
#include <linux/mutex.h>
18
#include <linux/slab.h>
19
#include <asm/unaligned.h>
20
#include "tuner-i2c.h"
21
#include "tuner-xc2028.h"
22
#include "tuner-xc2028-types.h"
23

24 25 26
#include <linux/dvb/frontend.h>
#include "dvb_frontend.h"

27 28 29
/* Max transfer size done by I2C transfer functions */
#define MAX_XFER_SIZE  80

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/* Registers (Write-only) */
#define XREG_INIT         0x00
#define XREG_RF_FREQ      0x02
#define XREG_POWER_DOWN   0x08

/* Registers (Read-only) */
#define XREG_FREQ_ERROR   0x01
#define XREG_LOCK         0x02
#define XREG_VERSION      0x04
#define XREG_PRODUCT_ID   0x08
#define XREG_HSYNC_FREQ   0x10
#define XREG_FRAME_LINES  0x20
#define XREG_SNR          0x40

#define XREG_ADC_ENV      0x0100
45

46 47 48 49
static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable verbose debug messages");

50 51
static int no_poweroff;
module_param(no_poweroff, int, 0644);
52
MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
53 54 55
	"1 keep device energized and with tuner ready all the times.\n"
	"  Faster, but consumes more power and keeps the device hotter\n");

56 57 58 59 60 61 62 63 64 65 66 67 68 69
static char audio_std[8];
module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
MODULE_PARM_DESC(audio_std,
	"Audio standard. XC3028 audio decoder explicitly "
	"needs to know what audio\n"
	"standard is needed for some video standards with audio A2 or NICAM.\n"
	"The valid values are:\n"
	"A2\n"
	"A2/A\n"
	"A2/B\n"
	"NICAM\n"
	"NICAM/A\n"
	"NICAM/B\n");

70
static char firmware_name[30];
71 72 73 74
module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
				"default firmware name\n");

75
static LIST_HEAD(hybrid_tuner_instance_list);
76 77
static DEFINE_MUTEX(xc2028_list_mutex);

78 79 80 81
/* struct for storing firmware table */
struct firmware_description {
	unsigned int  type;
	v4l2_std_id   id;
82
	__u16         int_freq;
83 84 85
	unsigned char *ptr;
	unsigned int  size;
};
86

87 88 89 90
struct firmware_properties {
	unsigned int	type;
	v4l2_std_id	id;
	v4l2_std_id	std_req;
91
	__u16		int_freq;
92 93 94 95
	unsigned int	scode_table;
	int 		scode_nr;
};

96 97 98 99 100 101 102 103
enum xc2028_state {
	XC2028_NO_FIRMWARE = 0,
	XC2028_WAITING_FIRMWARE,
	XC2028_ACTIVE,
	XC2028_SLEEP,
	XC2028_NODEV,
};

104
struct xc2028_data {
105
	struct list_head        hybrid_tuner_instance_list;
106
	struct tuner_i2c_props  i2c_props;
107 108
	__u32			frequency;

109 110 111
	enum xc2028_state	state;
	const char		*fname;

112 113
	struct firmware_description *firm;
	int			firm_size;
114
	__u16			firm_version;
115

116 117 118
	__u16			hwmodel;
	__u16			hwvers;

119
	struct xc2028_ctrl	ctrl;
120

121
	struct firmware_properties cur_fw;
122 123

	struct mutex lock;
124 125
};

126 127 128 129 130 131
#define i2c_send(priv, buf, size) ({					\
	int _rc;							\
	_rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size);		\
	if (size != _rc)						\
		tuner_info("i2c output error: rc = %d (should be %d)\n",\
			   _rc, (int)size);				\
132 133
	if (priv->ctrl.msleep)						\
		msleep(priv->ctrl.msleep);				\
134 135 136
	_rc;								\
})

137 138 139 140 141 142 143
#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({		\
	int _rc;							\
	_rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize,	\
				       ibuf, isize);			\
	if (isize != _rc)						\
		tuner_err("i2c input error: rc = %d (should be %d)\n",	\
			   _rc, (int)isize); 				\
144 145
	if (priv->ctrl.msleep)						\
		msleep(priv->ctrl.msleep);				\
146 147 148
	_rc;								\
})

149
#define send_seq(priv, data...)	({					\
150
	static u8 _val[] = data;					\
151
	int _rc;							\
152
	if (sizeof(_val) !=						\
153
			(_rc = tuner_i2c_xfer_send(&priv->i2c_props,	\
154
						_val, sizeof(_val)))) {	\
155
		tuner_err("Error on line %d: %d\n", __LINE__, _rc);	\
156
	} else if (priv->ctrl.msleep)					\
157
		msleep(priv->ctrl.msleep);				\
158 159
	_rc;								\
})
160

161
static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
162
{
163
	unsigned char buf[2];
164
	unsigned char ibuf[2];
165

166
	tuner_dbg("%s %04x called\n", __func__, reg);
167

168
	buf[0] = reg >> 8;
169
	buf[1] = (unsigned char) reg;
170

171 172
	if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
		return -EIO;
173

174 175
	*val = (ibuf[1]) | (ibuf[0] << 8);
	return 0;
176 177
}

178
#define dump_firm_type(t) 	dump_firm_type_and_int_freq(t, 0)
179
static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
180
{
181
	if (type & BASE)
182
		printk(KERN_CONT "BASE ");
183
	if (type & INIT1)
184
		printk(KERN_CONT "INIT1 ");
185
	if (type & F8MHZ)
186
		printk(KERN_CONT "F8MHZ ");
187
	if (type & MTS)
188
		printk(KERN_CONT "MTS ");
189
	if (type & D2620)
190
		printk(KERN_CONT "D2620 ");
191
	if (type & D2633)
192
		printk(KERN_CONT "D2633 ");
193
	if (type & DTV6)
194
		printk(KERN_CONT "DTV6 ");
195
	if (type & QAM)
196
		printk(KERN_CONT "QAM ");
197
	if (type & DTV7)
198
		printk(KERN_CONT "DTV7 ");
199
	if (type & DTV78)
200
		printk(KERN_CONT "DTV78 ");
201
	if (type & DTV8)
202
		printk(KERN_CONT "DTV8 ");
203
	if (type & FM)
204
		printk(KERN_CONT "FM ");
205
	if (type & INPUT1)
206
		printk(KERN_CONT "INPUT1 ");
207
	if (type & LCD)
208
		printk(KERN_CONT "LCD ");
209
	if (type & NOGD)
210
		printk(KERN_CONT "NOGD ");
211
	if (type & MONO)
212
		printk(KERN_CONT "MONO ");
213
	if (type & ATSC)
214
		printk(KERN_CONT "ATSC ");
215
	if (type & IF)
216
		printk(KERN_CONT "IF ");
217
	if (type & LG60)
218
		printk(KERN_CONT "LG60 ");
219
	if (type & ATI638)
220
		printk(KERN_CONT "ATI638 ");
221
	if (type & OREN538)
222
		printk(KERN_CONT "OREN538 ");
223
	if (type & OREN36)
224
		printk(KERN_CONT "OREN36 ");
225
	if (type & TOYOTA388)
226
		printk(KERN_CONT "TOYOTA388 ");
227
	if (type & TOYOTA794)
228
		printk(KERN_CONT "TOYOTA794 ");
229
	if (type & DIBCOM52)
230
		printk(KERN_CONT "DIBCOM52 ");
231
	if (type & ZARLINK456)
232
		printk(KERN_CONT "ZARLINK456 ");
233
	if (type & CHINA)
234
		printk(KERN_CONT "CHINA ");
235
	if (type & F6MHZ)
236
		printk(KERN_CONT "F6MHZ ");
237
	if (type & INPUT2)
238
		printk(KERN_CONT "INPUT2 ");
239
	if (type & SCODE)
240
		printk(KERN_CONT "SCODE ");
241
	if (type & HAS_IF)
242
		printk(KERN_CONT "HAS_IF_%d ", int_freq);
243 244
}

245
static  v4l2_std_id parse_audio_std_option(void)
246
{
247
	if (strcasecmp(audio_std, "A2") == 0)
248
		return V4L2_STD_A2;
249
	if (strcasecmp(audio_std, "A2/A") == 0)
250
		return V4L2_STD_A2_A;
251
	if (strcasecmp(audio_std, "A2/B") == 0)
252
		return V4L2_STD_A2_B;
253
	if (strcasecmp(audio_std, "NICAM") == 0)
254
		return V4L2_STD_NICAM;
255
	if (strcasecmp(audio_std, "NICAM/A") == 0)
256
		return V4L2_STD_NICAM_A;
257
	if (strcasecmp(audio_std, "NICAM/B") == 0)
258 259 260 261 262
		return V4L2_STD_NICAM_B;

	return 0;
}

263 264 265 266 267 268 269
static int check_device_status(struct xc2028_data *priv)
{
	switch (priv->state) {
	case XC2028_NO_FIRMWARE:
	case XC2028_WAITING_FIRMWARE:
		return -EAGAIN;
	case XC2028_ACTIVE:
270
		return 1;
271 272 273 274 275 276 277 278
	case XC2028_SLEEP:
		return 0;
	case XC2028_NODEV:
		return -ENODEV;
	}
	return 0;
}

279
static void free_firmware(struct xc2028_data *priv)
280
{
281
	int i;
282
	tuner_dbg("%s called\n", __func__);
283 284 285 286

	if (!priv->firm)
		return;

287 288 289
	for (i = 0; i < priv->firm_size; i++)
		kfree(priv->firm[i].ptr);

290 291
	kfree(priv->firm);

292
	priv->firm = NULL;
293
	priv->firm_size = 0;
294
	priv->state = XC2028_NO_FIRMWARE;
295 296

	memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
297 298
}

299 300
static int load_all_firmwares(struct dvb_frontend *fe,
			      const struct firmware *fw)
301 302
{
	struct xc2028_data    *priv = fe->tuner_priv;
303
	const unsigned char   *p, *endp;
304 305
	int                   rc = 0;
	int		      n, n_array;
306
	char		      name[33];
307

308
	tuner_dbg("%s called\n", __func__);
309

310 311
	p = fw->data;
	endp = p + fw->size;
312

313 314
	if (fw->size < sizeof(name) - 1 + 2 + 2) {
		tuner_err("Error: firmware file %s has invalid size!\n",
315
			  priv->fname);
316
		goto corrupt;
317
	}
318

319 320 321
	memcpy(name, p, sizeof(name) - 1);
	name[sizeof(name) - 1] = 0;
	p += sizeof(name) - 1;
322

323
	priv->firm_version = get_unaligned_le16(p);
324 325
	p += 2;

326
	n_array = get_unaligned_le16(p);
327 328
	p += 2;

329
	tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
330
		   n_array, priv->fname, name,
331
		   priv->firm_version >> 8, priv->firm_version & 0xff);
332

333
	priv->firm = kcalloc(n_array, sizeof(*priv->firm), GFP_KERNEL);
334 335
	if (priv->firm == NULL) {
		tuner_err("Not enough memory to load firmware file.\n");
336
		rc = -ENOMEM;
337
		goto err;
338
	}
339
	priv->firm_size = n_array;
340

341 342
	n = -1;
	while (p < endp) {
343 344
		__u32 type, size;
		v4l2_std_id id;
345
		__u16 int_freq = 0;
346 347 348

		n++;
		if (n >= n_array) {
349 350
			tuner_err("More firmware images in file than "
				  "were expected!\n");
351 352 353 354
			goto corrupt;
		}

		/* Checks if there's enough bytes to read */
355 356
		if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
			goto header;
357

358
		type = get_unaligned_le32(p);
359 360
		p += sizeof(type);

361
		id = get_unaligned_le64(p);
362 363
		p += sizeof(id);

364
		if (type & HAS_IF) {
365
			int_freq = get_unaligned_le16(p);
366
			p += sizeof(int_freq);
367 368
			if (endp - p < sizeof(size))
				goto header;
369 370
		}

371
		size = get_unaligned_le32(p);
372 373
		p += sizeof(size);

374
		if (!size || size > endp - p) {
375
			tuner_err("Firmware type ");
376
			dump_firm_type(type);
377 378
			printk(KERN_CONT
			       "(%x), id %llx is corrupted (size=%d, expected %d)\n",
379
			       type, (unsigned long long)id,
380
			       (unsigned)(endp - p), size);
381 382 383
			goto corrupt;
		}

384
		priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
385 386
		if (priv->firm[n].ptr == NULL) {
			tuner_err("Not enough memory to load firmware file.\n");
387
			rc = -ENOMEM;
388 389
			goto err;
		}
390 391
		tuner_dbg("Reading firmware type ");
		if (debug) {
392
			dump_firm_type_and_int_freq(type, int_freq);
393
			printk(KERN_CONT "(%x), id %llx, size=%d.\n",
394
			       type, (unsigned long long)id, size);
395
		}
396 397 398 399 400

		memcpy(priv->firm[n].ptr, p, size);
		priv->firm[n].type = type;
		priv->firm[n].id   = id;
		priv->firm[n].size = size;
401
		priv->firm[n].int_freq = int_freq;
402 403 404 405

		p += size;
	}

406
	if (n + 1 != priv->firm_size) {
407
		tuner_err("Firmware file is incomplete!\n");
408 409 410 411 412
		goto corrupt;
	}

	goto done;

413 414
header:
	tuner_err("Firmware header is incomplete!\n");
415
corrupt:
416
	rc = -EINVAL;
417
	tuner_err("Error: firmware file is corrupted!\n");
418 419

err:
420
	tuner_info("Releasing partially loaded firmware file.\n");
421 422 423
	free_firmware(priv);

done:
424 425
	if (rc == 0)
		tuner_dbg("Firmware files loaded.\n");
426 427
	else
		priv->state = XC2028_NODEV;
428 429 430 431

	return rc;
}

432 433
static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
			 v4l2_std_id *id)
434 435
{
	struct xc2028_data *priv = fe->tuner_priv;
436
	int                 i, best_i = -1, best_nr_matches = 0;
437
	unsigned int        type_mask = 0;
438

439
	tuner_dbg("%s called, want type=", __func__);
440 441
	if (debug) {
		dump_firm_type(type);
442 443
		printk(KERN_CONT "(%x), id %016llx.\n",
		       type, (unsigned long long)*id);
444
	}
445 446

	if (!priv->firm) {
447
		tuner_err("Error! firmware not loaded\n");
448 449 450
		return -EINVAL;
	}

451
	if (((type & ~SCODE) == 0) && (*id == 0))
452
		*id = V4L2_STD_PAL;
453

454
	if (type & BASE)
455
		type_mask = BASE_TYPES;
456
	else if (type & SCODE) {
457
		type &= SCODE_TYPES;
458
		type_mask = SCODE_TYPES & ~HAS_IF;
459
	} else if (type & DTV_TYPES)
460
		type_mask = DTV_TYPES;
461
	else if (type & STD_SPECIFIC_TYPES)
462 463 464 465
		type_mask = STD_SPECIFIC_TYPES;

	type &= type_mask;

466
	if (!(type & SCODE))
467
		type_mask = ~0;
468

469
	/* Seek for exact match */
470
	for (i = 0; i < priv->firm_size; i++) {
471
		if ((type == (priv->firm[i].type & type_mask)) &&
472
		    (*id == priv->firm[i].id))
473 474 475 476
			goto found;
	}

	/* Seek for generic video standard match */
477
	for (i = 0; i < priv->firm_size; i++) {
478 479 480
		v4l2_std_id match_mask;
		int nr_matches;

481
		if (type != (priv->firm[i].type & type_mask))
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
			continue;

		match_mask = *id & priv->firm[i].id;
		if (!match_mask)
			continue;

		if ((*id & match_mask) == *id)
			goto found; /* Supports all the requested standards */

		nr_matches = hweight64(match_mask);
		if (nr_matches > best_nr_matches) {
			best_nr_matches = nr_matches;
			best_i = i;
		}
	}

	if (best_nr_matches > 0) {
		tuner_dbg("Selecting best matching firmware (%d bits) for "
			  "type=", best_nr_matches);
		dump_firm_type(type);
502 503
		printk(KERN_CONT
		       "(%x), id %016llx:\n", type, (unsigned long long)*id);
504 505
		i = best_i;
		goto found;
506 507 508 509
	}

	/*FIXME: Would make sense to seek for type "hint" match ? */

510
	i = -ENOENT;
511
	goto ret;
512 513 514 515

found:
	*id = priv->firm[i].id;

516
ret:
517
	tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
518 519
	if (debug) {
		dump_firm_type(type);
520 521
		printk(KERN_CONT "(%x), id %016llx.\n",
		       type, (unsigned long long)*id);
522
	}
523 524 525
	return i;
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
{
	struct xc2028_data *priv = fe->tuner_priv;

	/* analog side (tuner-core) uses i2c_adap->algo_data.
	 * digital side is not guaranteed to have algo_data defined.
	 *
	 * digital side will always have fe->dvb defined.
	 * analog side (tuner-core) doesn't (yet) define fe->dvb.
	 */

	return (!fe->callback) ? -EINVAL :
		fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
				fe->dvb->priv : priv->i2c_props.adap->algo_data,
			     DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
}

543 544 545 546 547
static int load_firmware(struct dvb_frontend *fe, unsigned int type,
			 v4l2_std_id *id)
{
	struct xc2028_data *priv = fe->tuner_priv;
	int                pos, rc;
548 549 550 551
	unsigned char      *p, *endp, buf[MAX_XFER_SIZE];

	if (priv->ctrl.max_len > sizeof(buf))
		priv->ctrl.max_len = sizeof(buf);
552

553
	tuner_dbg("%s called\n", __func__);
554 555 556 557 558

	pos = seek_firmware(fe, type, id);
	if (pos < 0)
		return pos;

559
	tuner_info("Loading firmware for type=");
560
	dump_firm_type(priv->firm[pos].type);
561 562
	printk(KERN_CONT "(%x), id %016llx.\n",
	       priv->firm[pos].type, (unsigned long long)*id);
563

564 565
	p = priv->firm[pos].ptr;
	endp = p + priv->firm[pos].size;
566

567
	while (p < endp) {
568 569 570
		__u16 size;

		/* Checks if there's enough bytes to read */
571
		if (p + sizeof(size) > endp) {
572
			tuner_err("Firmware chunk size is wrong\n");
573 574 575
			return -EINVAL;
		}

576
		size = le16_to_cpu(*(__le16 *) p);
577 578 579 580 581 582
		p += sizeof(size);

		if (size == 0xffff)
			return 0;

		if (!size) {
583
			/* Special callback command received */
584
			rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
585
			if (rc < 0) {
586
				tuner_err("Error at RESET code %d\n",
587
					   (*p) & 0x7f);
588
				return -EINVAL;
589 590 591
			}
			continue;
		}
592 593 594
		if (size >= 0xff00) {
			switch (size) {
			case 0xff00:
595
				rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
596 597 598 599 600
				if (rc < 0) {
					tuner_err("Error at RESET code %d\n",
						  (*p) & 0x7f);
					return -EINVAL;
				}
601
				break;
602 603 604 605 606 607
			default:
				tuner_info("Invalid RESET code %d\n",
					   size & 0x7f);
				return -EINVAL;

			}
608
			continue;
609
		}
610 611 612

		/* Checks for a sleep command */
		if (size & 0x8000) {
613
			msleep(size & 0x7fff);
614
			continue;
615 616
		}

617
		if ((size + p > endp)) {
618
			tuner_err("missing bytes: need %d, have %d\n",
619
				   size, (int)(endp - p));
620 621
			return -EINVAL;
		}
622

623
		buf[0] = *p;
624
		p++;
625
		size--;
626

627
		/* Sends message chunks */
628
		while (size > 0) {
629 630
			int len = (size < priv->ctrl.max_len - 1) ?
				   size : priv->ctrl.max_len - 1;
631

632
			memcpy(buf + 1, p, len);
633

634
			rc = i2c_send(priv, buf, len + 1);
635
			if (rc < 0) {
636
				tuner_err("%d returned from send\n", rc);
637 638 639 640 641 642
				return -EINVAL;
			}

			p += len;
			size -= len;
		}
643 644 645 646 647 648 649

		/* silently fail if the frontend doesn't support I2C flush */
		rc = do_tuner_callback(fe, XC2028_I2C_FLUSH, 0);
		if ((rc < 0) && (rc != -EINVAL)) {
			tuner_err("error executing flush: %d\n", rc);
			return rc;
		}
650
	}
651
	return 0;
652 653
}

654
static int load_scode(struct dvb_frontend *fe, unsigned int type,
655
			 v4l2_std_id *id, __u16 int_freq, int scode)
656 657 658 659 660
{
	struct xc2028_data *priv = fe->tuner_priv;
	int                pos, rc;
	unsigned char	   *p;

661
	tuner_dbg("%s called\n", __func__);
662

663 664 665 666 667 668 669
	if (!int_freq) {
		pos = seek_firmware(fe, type, id);
		if (pos < 0)
			return pos;
	} else {
		for (pos = 0; pos < priv->firm_size; pos++) {
			if ((priv->firm[pos].int_freq == int_freq) &&
670
			    (priv->firm[pos].type & HAS_IF))
671 672 673 674 675
				break;
		}
		if (pos == priv->firm_size)
			return -ENOENT;
	}
676 677 678

	p = priv->firm[pos].ptr;

679
	if (priv->firm[pos].type & HAS_IF) {
680 681 682 683 684 685 686
		if (priv->firm[pos].size != 12 * 16 || scode >= 16)
			return -EINVAL;
		p += 12 * scode;
	} else {
		/* 16 SCODE entries per file; each SCODE entry is 12 bytes and
		 * has a 2-byte size header in the firmware format. */
		if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
687
		    le16_to_cpu(*(__le16 *)(p + 14 * scode)) != 12)
688 689 690
			return -EINVAL;
		p += 14 * scode + 2;
	}
691

692
	tuner_info("Loading SCODE for type=");
693 694
	dump_firm_type_and_int_freq(priv->firm[pos].type,
				    priv->firm[pos].int_freq);
695
	printk(KERN_CONT "(%x), id %016llx.\n", priv->firm[pos].type,
696 697
	       (unsigned long long)*id);

698
	if (priv->firm_version < 0x0202)
699 700 701 702 703
		rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
	else
		rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
	if (rc < 0)
		return -EIO;
704

705
	rc = i2c_send(priv, p, 12);
706 707
	if (rc < 0)
		return -EIO;
708

709 710 711
	rc = send_seq(priv, {0x00, 0x8c});
	if (rc < 0)
		return -EIO;
712 713 714 715

	return 0;
}

716 717
static int xc2028_sleep(struct dvb_frontend *fe);

718
static int check_firmware(struct dvb_frontend *fe, unsigned int type,
719
			  v4l2_std_id std, __u16 int_freq)
720
{
721
	struct xc2028_data         *priv = fe->tuner_priv;
722
	struct firmware_properties new_fw;
723
	int			   rc, retry_count = 0;
724 725
	u16			   version, hwmodel;
	v4l2_std_id		   std0;
726

727
	tuner_dbg("%s called\n", __func__);
728

729 730 731
	rc = check_device_status(priv);
	if (rc < 0)
		return rc;
732

733
	if (priv->ctrl.mts && !(type & FM))
734
		type |= MTS;
735

736
retry:
737 738 739 740 741
	new_fw.type = type;
	new_fw.id = std;
	new_fw.std_req = std;
	new_fw.scode_table = SCODE | priv->ctrl.scode_table;
	new_fw.scode_nr = 0;
742
	new_fw.int_freq = int_freq;
743 744 745 746

	tuner_dbg("checking firmware, user requested type=");
	if (debug) {
		dump_firm_type(new_fw.type);
747
		printk(KERN_CONT "(%x), id %016llx, ", new_fw.type,
748
		       (unsigned long long)new_fw.std_req);
749
		if (!int_freq) {
750
			printk(KERN_CONT "scode_tbl ");
751
			dump_firm_type(priv->ctrl.scode_table);
752
			printk(KERN_CONT "(%x), ", priv->ctrl.scode_table);
753
		} else
754 755
			printk(KERN_CONT "int_freq %d, ", new_fw.int_freq);
		printk(KERN_CONT "scode_nr %d\n", new_fw.scode_nr);
756 757
	}

758 759 760 761
	/*
	 * No need to reload base firmware if it matches and if the tuner
	 * is not at sleep mode
	 */
762
	if ((priv->state == XC2028_ACTIVE) &&
763 764
	    (((BASE | new_fw.type) & BASE_TYPES) ==
	    (priv->cur_fw.type & BASE_TYPES))) {
765 766 767 768 769 770 771 772
		tuner_dbg("BASE firmware not changed.\n");
		goto skip_base;
	}

	/* Updating BASE - forget about all currently loaded firmware */
	memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));

	/* Reset is needed before loading firmware */
773
	rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
774 775 776
	if (rc < 0)
		goto fail;

777 778 779
	/* BASE firmwares are all std0 */
	std0 = 0;
	rc = load_firmware(fe, BASE | new_fw.type, &std0);
780 781 782 783 784
	if (rc < 0) {
		tuner_err("Error %d while loading base firmware\n",
			  rc);
		goto fail;
	}
785

786
	/* Load INIT1, if needed */
787
	tuner_dbg("Load init1 firmware, if exists\n");
788

789
	rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
790 791 792
	if (rc == -ENOENT)
		rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
				   &std0);
793 794 795 796 797
	if (rc < 0 && rc != -ENOENT) {
		tuner_err("Error %d while loading init1 firmware\n",
			  rc);
		goto fail;
	}
798

799 800 801 802
skip_base:
	/*
	 * No need to reload standard specific firmware if base firmware
	 * was not reloaded and requested video standards have not changed.
803
	 */
804 805
	if (priv->cur_fw.type == (BASE | new_fw.type) &&
	    priv->cur_fw.std_req == std) {
806
		tuner_dbg("Std-specific firmware already loaded.\n");
807
		goto skip_std_specific;
808
	}
809

810 811 812 813
	/* Reloading std-specific firmware forces a SCODE update */
	priv->cur_fw.scode_table = 0;

	rc = load_firmware(fe, new_fw.type, &new_fw.id);
814 815 816
	if (rc == -ENOENT)
		rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);

817
	if (rc < 0)
818 819 820 821 822 823 824 825
		goto fail;

skip_std_specific:
	if (priv->cur_fw.scode_table == new_fw.scode_table &&
	    priv->cur_fw.scode_nr == new_fw.scode_nr) {
		tuner_dbg("SCODE firmware already loaded.\n");
		goto check_device;
	}
826

827 828 829
	if (new_fw.type & FM)
		goto check_device;

830
	/* Load SCODE firmware, if exists */
831
	tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
832

833 834
	rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
			new_fw.int_freq, new_fw.scode_nr);
835

836
check_device:
837 838 839 840 841
	if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
	    xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
		tuner_err("Unable to read tuner registers.\n");
		goto fail;
	}
842

843 844 845 846
	tuner_dbg("Device is Xceive %d version %d.%d, "
		  "firmware version %d.%d\n",
		  hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
		  (version & 0xf0) >> 4, version & 0xf);
847

848 849 850 851

	if (priv->ctrl.read_not_reliable)
		goto read_not_reliable;

852 853
	/* Check firmware version against what we downloaded. */
	if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
854 855 856 857 858 859 860 861
		if (!priv->ctrl.read_not_reliable) {
			tuner_err("Incorrect readback of firmware version.\n");
			goto fail;
		} else {
			tuner_err("Returned an incorrect version. However, "
				  "read is not reliable enough. Ignoring it.\n");
			hwmodel = 3028;
		}
862 863 864 865 866 867 868 869 870 871 872 873 874
	}

	/* Check that the tuner hardware model remains consistent over time. */
	if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
		priv->hwmodel = hwmodel;
		priv->hwvers  = version & 0xff00;
	} else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
		   priv->hwvers != (version & 0xff00)) {
		tuner_err("Read invalid device hardware information - tuner "
			  "hung?\n");
		goto fail;
	}

875
read_not_reliable:
876
	priv->cur_fw = new_fw;
877 878 879 880 881 882 883 884

	/*
	 * By setting BASE in cur_fw.type only after successfully loading all
	 * firmwares, we can:
	 * 1. Identify that BASE firmware with type=0 has been loaded;
	 * 2. Tell whether BASE firmware was just changed the next time through.
	 */
	priv->cur_fw.type |= BASE;
885
	priv->state = XC2028_ACTIVE;
886 887

	return 0;
888 889

fail:
890
	priv->state = XC2028_NO_FIRMWARE;
891

892
	memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
893
	if (retry_count < 8) {
894
		msleep(50);
895
		retry_count++;
896 897 898 899
		tuner_dbg("Retrying firmware load\n");
		goto retry;
	}

900 901 902
	/* Firmware didn't load. Put the device to sleep */
	xc2028_sleep(fe);

903 904 905
	if (rc == -ENOENT)
		rc = -EINVAL;
	return rc;
906 907
}

908
static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
909
{
910
	struct xc2028_data *priv = fe->tuner_priv;
911
	u16                 frq_lock, signal = 0;
912
	int                 rc, i;
913

914
	tuner_dbg("%s called\n", __func__);
915

916 917 918 919
	rc = check_device_status(priv);
	if (rc < 0)
		return rc;

920 921 922 923 924 925
	/* If the device is sleeping, no channel is tuned */
	if (!rc) {
		*strength = 0;
		return 0;
	}

926
	mutex_lock(&priv->lock);
927

928
	/* Sync Lock Indicator */
929 930 931 932 933 934 935 936 937
	for (i = 0; i < 3; i++) {
		rc = xc2028_get_reg(priv, XREG_LOCK, &frq_lock);
		if (rc < 0)
			goto ret;

		if (frq_lock)
			break;
		msleep(6);
	}
938

939
	/* Frequency didn't lock */
940 941
	if (frq_lock == 2)
		goto ret;
942

943
	/* Get SNR of the video signal */
944
	rc = xc2028_get_reg(priv, XREG_SNR, &signal);
945
	if (rc < 0)
946 947
		goto ret;

948 949 950
	/* Signal level is 3 bits only */

	signal = ((1 << 12) - 1) | ((signal & 0x07) << 12);
951 952

ret:
953 954 955
	mutex_unlock(&priv->lock);

	*strength = signal;
956

957 958
	tuner_dbg("signal strength is %d\n", signal);

959
	return rc;
960 961
}

962 963 964 965 966 967 968 969 970 971 972
static int xc2028_get_afc(struct dvb_frontend *fe, s32 *afc)
{
	struct xc2028_data *priv = fe->tuner_priv;
	int i, rc;
	u16 frq_lock = 0;
	s16 afc_reg = 0;

	rc = check_device_status(priv);
	if (rc < 0)
		return rc;

973 974 975 976 977 978
	/* If the device is sleeping, no channel is tuned */
	if (!rc) {
		*afc = 0;
		return 0;
	}

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	mutex_lock(&priv->lock);

	/* Sync Lock Indicator */
	for (i = 0; i < 3; i++) {
		rc = xc2028_get_reg(priv, XREG_LOCK, &frq_lock);
		if (rc < 0)
			goto ret;

		if (frq_lock)
			break;
		msleep(6);
	}

	/* Frequency didn't lock */
	if (frq_lock == 2)
		goto ret;

	/* Get AFC */
	rc = xc2028_get_reg(priv, XREG_FREQ_ERROR, &afc_reg);
	if (rc < 0)
999
		goto ret;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

	*afc = afc_reg * 15625; /* Hz */

	tuner_dbg("AFC is %d Hz\n", *afc);

ret:
	mutex_unlock(&priv->lock);

	return rc;
}

1011 1012
#define DIV 15625

1013
static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
1014
			    enum v4l2_tuner_type new_type,
1015 1016 1017
			    unsigned int type,
			    v4l2_std_id std,
			    u16 int_freq)
1018
{
1019
	struct xc2028_data *priv = fe->tuner_priv;
1020
	int		   rc = -EINVAL;
1021
	unsigned char	   buf[4];
1022
	u32		   div, offset = 0;
1023

1024
	tuner_dbg("%s called\n", __func__);
1025

1026 1027
	mutex_lock(&priv->lock);

1028
	tuner_dbg("should set frequency %d kHz\n", freq / 1000);
1029

1030
	if (check_firmware(fe, type, std, int_freq) < 0)
1031
		goto ret;
1032

1033 1034 1035 1036 1037 1038 1039
	/* On some cases xc2028 can disable video output, if
	 * very weak signals are received. By sending a soft
	 * reset, this is re-enabled. So, it is better to always
	 * send a soft reset before changing channels, to be sure
	 * that xc2028 will be in a safe state.
	 * Maybe this might also be needed for DTV.
	 */
1040 1041
	switch (new_type) {
	case V4L2_TUNER_ANALOG_TV:
1042
		rc = send_seq(priv, {0x00, 0x00});
1043

1044 1045 1046 1047 1048 1049
		/* Analog mode requires offset = 0 */
		break;
	case V4L2_TUNER_RADIO:
		/* Radio mode requires offset = 0 */
		break;
	case V4L2_TUNER_DIGITAL_TV:
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
		/*
		 * Digital modes require an offset to adjust to the
		 * proper frequency. The offset depends on what
		 * firmware version is used.
		 */

		/*
		 * Adjust to the center frequency. This is calculated by the
		 * formula: offset = 1.25MHz - BW/2
		 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
		 * further adjustment to get the frequency center on VHF
		 */
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075

		/*
		 * The firmware DTV78 used to work fine in UHF band (8 MHz
		 * bandwidth) but not at all in VHF band (7 MHz bandwidth).
		 * The real problem was connected to the formula used to
		 * calculate the center frequency offset in VHF band.
		 * In fact, removing the 500KHz adjustment fixed the problem.
		 * This is coherent to what was implemented for the DTV7
		 * firmware.
		 * In the end, now the center frequency is the same for all 3
		 * firmwares (DTV7, DTV8, DTV78) and doesn't depend on channel
		 * bandwidth.
		 */

1076 1077
		if (priv->cur_fw.type & DTV6)
			offset = 1750000;
1078
		else	/* DTV7 or DTV8 or DTV78 */
1079 1080
			offset = 2750000;

1081
		/*
1082 1083 1084 1085 1086
		 * xc3028 additional "magic"
		 * Depending on the firmware version, it needs some adjustments
		 * to properly centralize the frequency. This seems to be
		 * needed to compensate the SCODE table adjustments made by
		 * newer firmwares
1087
		 */
1088 1089 1090 1091 1092 1093 1094

		/*
		 * The proper adjustment would be to do it at s-code table.
		 * However, this didn't work, as reported by
		 * Robert Lowery <rglowery@exemail.com.au>
		 */

1095
#if 0
1096 1097 1098 1099
		/*
		 * Still need tests for XC3028L (firmware 3.2 or upper)
		 * So, for now, let's just comment the per-firmware
		 * version of this change. Reports with xc3028l working
1100
		 * with and without the lines below are welcome
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
		 */

		if (priv->firm_version < 0x0302) {
			if (priv->cur_fw.type & DTV7)
				offset += 500000;
		} else {
			if (priv->cur_fw.type & DTV7)
				offset -= 300000;
			else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
				offset += 200000;
		}
#endif
1113
		break;
1114 1115 1116
	default:
		tuner_err("Unsupported tuner type %d.\n", new_type);
		break;
1117
	}
1118

1119
	div = (freq - offset + DIV / 2) / DIV;
1120

1121
	/* CMD= Set frequency */
1122
	if (priv->firm_version < 0x0202)
1123
		rc = send_seq(priv, {0x00, XREG_RF_FREQ, 0x00, 0x00});
1124
	else
1125
		rc = send_seq(priv, {0x80, XREG_RF_FREQ, 0x00, 0x00});
1126 1127
	if (rc < 0)
		goto ret;
1128

1129 1130 1131 1132
	/* Return code shouldn't be checked.
	   The reset CLK is needed only with tm6000.
	   Driver should work fine even if this fails.
	 */
1133 1134
	if (priv->ctrl.msleep)
		msleep(priv->ctrl.msleep);
1135
	do_tuner_callback(fe, XC2028_RESET_CLK, 1);
1136 1137

	msleep(10);
1138

1139 1140 1141 1142
	buf[0] = 0xff & (div >> 24);
	buf[1] = 0xff & (div >> 16);
	buf[2] = 0xff & (div >> 8);
	buf[3] = 0xff & (div);
1143

1144
	rc = i2c_send(priv, buf, sizeof(buf));
1145
	if (rc < 0)
1146
		goto ret;
1147 1148
	msleep(100);

1149
	priv->frequency = freq;
1150

1151
	tuner_dbg("divisor= %*ph (freq=%d.%03d)\n", 4, buf,
1152
	       freq / 1000000, (freq % 1000000) / 1000);
1153

1154
	rc = 0;
1155

1156 1157
ret:
	mutex_unlock(&priv->lock);
1158

1159
	return rc;
1160 1161
}

1162
static int xc2028_set_analog_freq(struct dvb_frontend *fe,
1163
			      struct analog_parameters *p)
1164
{
1165
	struct xc2028_data *priv = fe->tuner_priv;
1166 1167
	unsigned int       type=0;

1168
	tuner_dbg("%s called\n", __func__);
1169

1170 1171 1172 1173 1174
	if (p->mode == V4L2_TUNER_RADIO) {
		type |= FM;
		if (priv->ctrl.input1)
			type |= INPUT1;
		return generic_set_freq(fe, (625l * p->frequency) / 10,
1175
				V4L2_TUNER_RADIO, type, 0, 0);
1176 1177
	}

1178 1179 1180 1181 1182
	/* if std is not defined, choose one */
	if (!p->std)
		p->std = V4L2_STD_MN;

	/* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
1183 1184
	if (!(p->std & V4L2_STD_MN))
		type |= F8MHZ;
1185

1186 1187
	/* Add audio hack to std mask */
	p->std |= parse_audio_std_option();
1188

1189
	return generic_set_freq(fe, 62500l * p->frequency,
1190
				V4L2_TUNER_ANALOG_TV, type, p->std, 0);
1191
}
1192

1193
static int xc2028_set_params(struct dvb_frontend *fe)
1194
{
1195 1196 1197
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
	u32 delsys = c->delivery_system;
	u32 bw = c->bandwidth_hz;
1198
	struct xc2028_data *priv = fe->tuner_priv;
1199 1200
	int rc;
	unsigned int       type = 0;
1201
	u16                demod = 0;
1202

1203
	tuner_dbg("%s called\n", __func__);
1204

1205 1206 1207 1208
	rc = check_device_status(priv);
	if (rc < 0)
		return rc;

1209 1210 1211
	switch (delsys) {
	case SYS_DVBT:
	case SYS_DVBT2:
1212 1213 1214 1215 1216
		/*
		 * The only countries with 6MHz seem to be Taiwan/Uruguay.
		 * Both seem to require QAM firmware for OFDM decoding
		 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
		 */
1217
		if (bw <= 6000000)
1218
			type |= QAM;
1219

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
		switch (priv->ctrl.type) {
		case XC2028_D2633:
			type |= D2633;
			break;
		case XC2028_D2620:
			type |= D2620;
			break;
		case XC2028_AUTO:
		default:
			/* Zarlink seems to need D2633 */
			if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
				type |= D2633;
			else
				type |= D2620;
		}
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
		break;
	case SYS_ATSC:
		/* The only ATSC firmware (at least on v2.7) is D2633 */
		type |= ATSC | D2633;
		break;
	/* DVB-S and pure QAM (FE_QAM) are not supported */
	default:
		return -EINVAL;
	}

	if (bw <= 6000000) {
		type |= DTV6;
		priv->ctrl.vhfbw7 = 0;
		priv->ctrl.uhfbw8 = 0;
	} else if (bw <= 7000000) {
		if (c->frequency < 470000000)
			priv->ctrl.vhfbw7 = 1;
		else
			priv->ctrl.uhfbw8 = 0;
		type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
		type |= F8MHZ;
	} else {
		if (c->frequency < 470000000)
			priv->ctrl.vhfbw7 = 0;
		else
			priv->ctrl.uhfbw8 = 1;
		type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
		type |= F8MHZ;
1263 1264
	}

1265
	/* All S-code tables need a 200kHz shift */
1266
	if (priv->ctrl.demod) {
1267 1268
		demod = priv->ctrl.demod;

1269 1270 1271 1272
		/*
		 * Newer firmwares require a 200 kHz offset only for ATSC
		 */
		if (type == ATSC || priv->firm_version < 0x0302)
1273
			demod += 200;
1274 1275 1276 1277 1278 1279
		/*
		 * The DTV7 S-code table needs a 700 kHz shift.
		 *
		 * DTV7 is only used in Australia.  Germany or Italy may also
		 * use this firmware after initialization, but a tune to a UHF
		 * channel should then cause DTV78 to be used.
1280 1281 1282 1283
		 *
		 * Unfortunately, on real-field tests, the s-code offset
		 * didn't work as expected, as reported by
		 * Robert Lowery <rglowery@exemail.com.au>
1284 1285
		 */
	}
1286

1287
	return generic_set_freq(fe, c->frequency,
1288
				V4L2_TUNER_DIGITAL_TV, type, 0, demod);
1289
}
1290

1291 1292 1293
static int xc2028_sleep(struct dvb_frontend *fe)
{
	struct xc2028_data *priv = fe->tuner_priv;
1294 1295 1296 1297 1298
	int rc;

	rc = check_device_status(priv);
	if (rc < 0)
		return rc;
1299

1300 1301
	/* Device is already in sleep mode */
	if (!rc)
1302
		return 0;
1303

1304 1305
	/* Avoid firmware reload on slow devices or if PM disabled */
	if (no_poweroff || priv->ctrl.disable_power_mgmt)
1306 1307
		return 0;

1308
	tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
1309 1310 1311 1312
	if (debug > 1) {
		tuner_dbg("Printing sleep stack trace:\n");
		dump_stack();
	}
1313 1314 1315 1316

	mutex_lock(&priv->lock);

	if (priv->firm_version < 0x0202)
1317
		rc = send_seq(priv, {0x00, XREG_POWER_DOWN, 0x00, 0x00});
1318
	else
1319
		rc = send_seq(priv, {0x80, XREG_POWER_DOWN, 0x00, 0x00});
1320

1321 1322
	if (rc >= 0)
		priv->state = XC2028_SLEEP;
1323 1324 1325 1326 1327

	mutex_unlock(&priv->lock);

	return rc;
}
1328

1329
static int xc2028_dvb_release(struct dvb_frontend *fe)
1330
{
1331 1332
	struct xc2028_data *priv = fe->tuner_priv;

1333
	tuner_dbg("%s called\n", __func__);
1334

1335 1336
	mutex_lock(&xc2028_list_mutex);

1337 1338
	/* only perform final cleanup if this is the last instance */
	if (hybrid_tuner_report_instance_count(priv) == 1) {
1339
		free_firmware(priv);
1340 1341
		kfree(priv->ctrl.fname);
		priv->ctrl.fname = NULL;
1342
	}
1343

1344 1345 1346
	if (priv)
		hybrid_tuner_release_state(priv);

1347 1348
	mutex_unlock(&xc2028_list_mutex);

1349 1350
	fe->tuner_priv = NULL;

1351 1352 1353
	return 0;
}

1354
static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1355
{
1356
	struct xc2028_data *priv = fe->tuner_priv;
1357
	int rc;
1358

1359
	tuner_dbg("%s called\n", __func__);
1360

1361 1362 1363 1364
	rc = check_device_status(priv);
	if (rc < 0)
		return rc;

1365
	*frequency = priv->frequency;
1366 1367 1368 1369

	return 0;
}

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
static void load_firmware_cb(const struct firmware *fw,
			     void *context)
{
	struct dvb_frontend *fe = context;
	struct xc2028_data *priv = fe->tuner_priv;
	int rc;

	tuner_dbg("request_firmware_nowait(): %s\n", fw ? "OK" : "error");
	if (!fw) {
		tuner_err("Could not load firmware %s.\n", priv->fname);
		priv->state = XC2028_NODEV;
		return;
	}

	rc = load_all_firmwares(fe, fw);

	release_firmware(fw);

	if (rc < 0)
		return;
1390
	priv->state = XC2028_ACTIVE;
1391 1392
}

1393
static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1394 1395 1396
{
	struct xc2028_data *priv = fe->tuner_priv;
	struct xc2028_ctrl *p    = priv_cfg;
1397
	int                 rc   = 0;
1398

1399
	tuner_dbg("%s called\n", __func__);
1400

1401 1402
	mutex_lock(&priv->lock);

1403 1404 1405 1406 1407
	/*
	 * Copy the config data.
	 * For the firmware name, keep a local copy of the string,
	 * in order to avoid troubles during device release.
	 */
1408
	kfree(priv->ctrl.fname);
1409
	priv->ctrl.fname = NULL;
1410 1411 1412
	memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
	if (p->fname) {
		priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1413 1414 1415 1416
		if (priv->ctrl.fname == NULL) {
			rc = -ENOMEM;
			goto unlock;
		}
1417 1418
	}

1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
	/*
	 * If firmware name changed, frees firmware. As free_firmware will
	 * reset the status to NO_FIRMWARE, this forces a new request_firmware
	 */
	if (!firmware_name[0] && p->fname &&
	    priv->fname && strcmp(p->fname, priv->fname))
		free_firmware(priv);

	if (priv->ctrl.max_len < 9)
		priv->ctrl.max_len = 13;

	if (priv->state == XC2028_NO_FIRMWARE) {
		if (!firmware_name[0])
			priv->fname = priv->ctrl.fname;
		else
			priv->fname = firmware_name;

		rc = request_firmware_nowait(THIS_MODULE, 1,
					     priv->fname,
					     priv->i2c_props.adap->dev.parent,
					     GFP_KERNEL,
					     fe, load_firmware_cb);
		if (rc < 0) {
			tuner_err("Failed to request firmware %s\n",
				  priv->fname);
			priv->state = XC2028_NODEV;
1445 1446
		} else
			priv->state = XC2028_WAITING_FIRMWARE;
1447
	}
1448
unlock:
1449 1450
	mutex_unlock(&priv->lock);

1451
	return rc;
1452 1453
}

1454
static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1455
	.info = {
1456 1457 1458 1459 1460
		 .name = "Xceive XC3028",
		 .frequency_min = 42000000,
		 .frequency_max = 864000000,
		 .frequency_step = 50000,
		 },
1461

1462
	.set_config	   = xc2028_set_config,
1463
	.set_analog_params = xc2028_set_analog_freq,
1464 1465 1466
	.release           = xc2028_dvb_release,
	.get_frequency     = xc2028_get_frequency,
	.get_rf_strength   = xc2028_signal,
1467
	.get_afc           = xc2028_get_afc,
1468
	.set_params        = xc2028_set_params,
1469
	.sleep             = xc2028_sleep,
1470 1471
};

1472 1473
struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
				   struct xc2028_config *cfg)
1474
{
1475
	struct xc2028_data *priv;
1476
	int instance;
1477

1478
	if (debug)
1479
		printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
1480

1481
	if (NULL == cfg)
1482
		return NULL;
1483

1484
	if (!fe) {
1485
		printk(KERN_ERR "xc2028: No frontend!\n");
1486
		return NULL;
1487 1488
	}

1489 1490
	mutex_lock(&xc2028_list_mutex);

1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
	instance = hybrid_tuner_request_state(struct xc2028_data, priv,
					      hybrid_tuner_instance_list,
					      cfg->i2c_adap, cfg->i2c_addr,
					      "xc2028");
	switch (instance) {
	case 0:
		/* memory allocation failure */
		goto fail;
	case 1:
		/* new tuner instance */
1501
		priv->ctrl.max_len = 13;
1502

1503 1504
		mutex_init(&priv->lock);

1505 1506 1507 1508 1509 1510 1511
		fe->tuner_priv = priv;
		break;
	case 2:
		/* existing tuner instance */
		fe->tuner_priv = priv;
		break;
	}
1512

1513
	memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1514
	       sizeof(xc2028_dvb_tuner_ops));
1515 1516 1517

	tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");

1518 1519 1520
	if (cfg->ctrl)
		xc2028_set_config(fe, cfg->ctrl);

1521 1522
	mutex_unlock(&xc2028_list_mutex);

1523
	return fe;
1524 1525 1526 1527 1528
fail:
	mutex_unlock(&xc2028_list_mutex);

	xc2028_dvb_release(fe);
	return NULL;
1529
}
1530

1531 1532
EXPORT_SYMBOL(xc2028_attach);

1533
MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1534
MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1535 1536
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
MODULE_LICENSE("GPL");
1537 1538
MODULE_FIRMWARE(XC2028_DEFAULT_FIRMWARE);
MODULE_FIRMWARE(XC3028L_DEFAULT_FIRMWARE);