]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/tuner-xc2028.c
V4L/DVB (7739): mt312.h: dubious one-bit signed bitfield
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / tuner-xc2028.c
CommitLineData
6cb45879
MCC
1/* tuner-xc2028
2 *
33e53161 3 * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org)
983d214e 4 *
701672eb
ML
5 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
983d214e 7 *
6cb45879
MCC
8 * This code is placed under the terms of the GNU General Public License v2
9 */
10
11#include <linux/i2c.h>
12#include <asm/div64.h>
13#include <linux/firmware.h>
ab0b9fc6 14#include <linux/videodev2.h>
6cb45879 15#include <linux/delay.h>
701672eb 16#include <media/tuner.h>
3b20532c 17#include <linux/mutex.h>
215b95ba 18#include "tuner-i2c.h"
6cb45879 19#include "tuner-xc2028.h"
de3fe21b 20#include "tuner-xc2028-types.h"
6cb45879 21
701672eb
ML
22#include <linux/dvb/frontend.h>
23#include "dvb_frontend.h"
24
ef8c1888 25
83fb340b
MCC
26static int debug;
27module_param(debug, int, 0644);
28MODULE_PARM_DESC(debug, "enable verbose debug messages");
29
a82200fb
MCC
30static char audio_std[8];
31module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
32MODULE_PARM_DESC(audio_std,
33 "Audio standard. XC3028 audio decoder explicitly "
34 "needs to know what audio\n"
35 "standard is needed for some video standards with audio A2 or NICAM.\n"
36 "The valid values are:\n"
37 "A2\n"
38 "A2/A\n"
39 "A2/B\n"
40 "NICAM\n"
41 "NICAM/A\n"
42 "NICAM/B\n");
43
5c913c05
MCC
44static char firmware_name[FIRMWARE_NAME_MAX];
45module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
46MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
47 "default firmware name\n");
48
215b95ba 49static LIST_HEAD(xc2028_list);
aa501be9
CP
50static DEFINE_MUTEX(xc2028_list_mutex);
51
de3fe21b
MCC
52/* struct for storing firmware table */
53struct firmware_description {
54 unsigned int type;
55 v4l2_std_id id;
66c2d53d 56 __u16 int_freq;
de3fe21b
MCC
57 unsigned char *ptr;
58 unsigned int size;
59};
6cb45879 60
e0f0b37a
CP
61struct firmware_properties {
62 unsigned int type;
63 v4l2_std_id id;
64 v4l2_std_id std_req;
66c2d53d 65 __u16 int_freq;
e0f0b37a
CP
66 unsigned int scode_table;
67 int scode_nr;
68};
69
6cb45879 70struct xc2028_data {
215b95ba
MCC
71 struct list_head xc2028_list;
72 struct tuner_i2c_props i2c_props;
73 int (*tuner_callback) (void *dev,
74 int command, int arg);
215b95ba
MCC
75 void *video_dev;
76 int count;
de3fe21b
MCC
77 __u32 frequency;
78
79 struct firmware_description *firm;
80 int firm_size;
06fd82dc 81 __u16 firm_version;
de3fe21b 82
8bf799a6
CP
83 __u16 hwmodel;
84 __u16 hwvers;
85
de3fe21b 86 struct xc2028_ctrl ctrl;
215b95ba 87
e0f0b37a 88 struct firmware_properties cur_fw;
215b95ba
MCC
89
90 struct mutex lock;
6cb45879
MCC
91};
92
47cc5b78
CP
93#define i2c_send(priv, buf, size) ({ \
94 int _rc; \
95 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
96 if (size != _rc) \
97 tuner_info("i2c output error: rc = %d (should be %d)\n",\
98 _rc, (int)size); \
99 _rc; \
100})
101
102#define i2c_rcv(priv, buf, size) ({ \
103 int _rc; \
104 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
105 if (size != _rc) \
83fb340b 106 tuner_err("i2c input error: rc = %d (should be %d)\n", \
47cc5b78
CP
107 _rc, (int)size); \
108 _rc; \
109})
ab0b9fc6 110
7d58d111
CP
111#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
112 int _rc; \
113 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
114 ibuf, isize); \
115 if (isize != _rc) \
116 tuner_err("i2c input error: rc = %d (should be %d)\n", \
117 _rc, (int)isize); \
118 _rc; \
119})
120
47cc5b78 121#define send_seq(priv, data...) ({ \
215b95ba 122 static u8 _val[] = data; \
47cc5b78 123 int _rc; \
6cb45879 124 if (sizeof(_val) != \
47cc5b78 125 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
215b95ba 126 _val, sizeof(_val)))) { \
47cc5b78
CP
127 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
128 } else \
129 msleep(10); \
130 _rc; \
131})
6cb45879 132
83244025 133static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
6cb45879 134{
b873e1a3 135 unsigned char buf[2];
7d58d111 136 unsigned char ibuf[2];
215b95ba 137
7e28adb2 138 tuner_dbg("%s %04x called\n", __func__, reg);
6cb45879 139
7d58d111 140 buf[0] = reg >> 8;
80b52208 141 buf[1] = (unsigned char) reg;
6cb45879 142
7d58d111
CP
143 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
144 return -EIO;
6cb45879 145
7d58d111
CP
146 *val = (ibuf[1]) | (ibuf[0] << 8);
147 return 0;
6cb45879
MCC
148}
149
e0262688 150#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
29bec0bf 151static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
43efe702
MCC
152{
153 if (type & BASE)
154 printk("BASE ");
f380e1d2
MCC
155 if (type & INIT1)
156 printk("INIT1 ");
43efe702
MCC
157 if (type & F8MHZ)
158 printk("F8MHZ ");
159 if (type & MTS)
160 printk("MTS ");
161 if (type & D2620)
162 printk("D2620 ");
163 if (type & D2633)
164 printk("D2633 ");
165 if (type & DTV6)
166 printk("DTV6 ");
167 if (type & QAM)
168 printk("QAM ");
169 if (type & DTV7)
170 printk("DTV7 ");
171 if (type & DTV78)
172 printk("DTV78 ");
173 if (type & DTV8)
174 printk("DTV8 ");
175 if (type & FM)
176 printk("FM ");
177 if (type & INPUT1)
178 printk("INPUT1 ");
179 if (type & LCD)
180 printk("LCD ");
181 if (type & NOGD)
182 printk("NOGD ");
183 if (type & MONO)
184 printk("MONO ");
185 if (type & ATSC)
186 printk("ATSC ");
187 if (type & IF)
188 printk("IF ");
189 if (type & LG60)
190 printk("LG60 ");
191 if (type & ATI638)
192 printk("ATI638 ");
193 if (type & OREN538)
194 printk("OREN538 ");
195 if (type & OREN36)
196 printk("OREN36 ");
197 if (type & TOYOTA388)
198 printk("TOYOTA388 ");
199 if (type & TOYOTA794)
200 printk("TOYOTA794 ");
201 if (type & DIBCOM52)
202 printk("DIBCOM52 ");
203 if (type & ZARLINK456)
204 printk("ZARLINK456 ");
205 if (type & CHINA)
206 printk("CHINA ");
207 if (type & F6MHZ)
208 printk("F6MHZ ");
209 if (type & INPUT2)
210 printk("INPUT2 ");
211 if (type & SCODE)
212 printk("SCODE ");
e0262688
CP
213 if (type & HAS_IF)
214 printk("HAS_IF_%d ", int_freq);
43efe702
MCC
215}
216
ef8c1888 217static v4l2_std_id parse_audio_std_option(void)
a82200fb 218{
e155d908 219 if (strcasecmp(audio_std, "A2") == 0)
a82200fb 220 return V4L2_STD_A2;
e155d908 221 if (strcasecmp(audio_std, "A2/A") == 0)
a82200fb 222 return V4L2_STD_A2_A;
e155d908 223 if (strcasecmp(audio_std, "A2/B") == 0)
a82200fb 224 return V4L2_STD_A2_B;
e155d908 225 if (strcasecmp(audio_std, "NICAM") == 0)
a82200fb 226 return V4L2_STD_NICAM;
e155d908 227 if (strcasecmp(audio_std, "NICAM/A") == 0)
a82200fb 228 return V4L2_STD_NICAM_A;
e155d908 229 if (strcasecmp(audio_std, "NICAM/B") == 0)
a82200fb
MCC
230 return V4L2_STD_NICAM_B;
231
232 return 0;
233}
234
ab0b9fc6 235static void free_firmware(struct xc2028_data *priv)
6cb45879 236{
de3fe21b 237 int i;
92b75ab0 238 tuner_dbg("%s called\n", __func__);
de3fe21b
MCC
239
240 if (!priv->firm)
241 return;
242
ab0b9fc6
MCC
243 for (i = 0; i < priv->firm_size; i++)
244 kfree(priv->firm[i].ptr);
245
de3fe21b
MCC
246 kfree(priv->firm);
247
ab0b9fc6 248 priv->firm = NULL;
06fd82dc 249 priv->firm_size = 0;
e0f0b37a
CP
250
251 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
de3fe21b
MCC
252}
253
ab0b9fc6 254static int load_all_firmwares(struct dvb_frontend *fe)
de3fe21b
MCC
255{
256 struct xc2028_data *priv = fe->tuner_priv;
ab0b9fc6 257 const struct firmware *fw = NULL;
6cb45879 258 unsigned char *p, *endp;
ab0b9fc6
MCC
259 int rc = 0;
260 int n, n_array;
de3fe21b 261 char name[33];
5c913c05 262 char *fname;
6cb45879 263
7e28adb2 264 tuner_dbg("%s called\n", __func__);
215b95ba 265
5c913c05
MCC
266 if (!firmware_name[0])
267 fname = priv->ctrl.fname;
268 else
269 fname = firmware_name;
270
271 tuner_dbg("Reading firmware %s\n", fname);
272 rc = request_firmware(&fw, fname, &priv->i2c_props.adap->dev);
6cb45879 273 if (rc < 0) {
ab0b9fc6 274 if (rc == -ENOENT)
83fb340b 275 tuner_err("Error: firmware %s not found.\n",
5c913c05 276 fname);
2e4160ca 277 else
83fb340b 278 tuner_err("Error %d while requesting firmware %s \n",
5c913c05 279 rc, fname);
2e4160ca 280
6cb45879
MCC
281 return rc;
282 }
ab0b9fc6
MCC
283 p = fw->data;
284 endp = p + fw->size;
6cb45879 285
06fd82dc
CP
286 if (fw->size < sizeof(name) - 1 + 2 + 2) {
287 tuner_err("Error: firmware file %s has invalid size!\n",
5c913c05 288 fname);
06fd82dc 289 goto corrupt;
6cb45879 290 }
de3fe21b 291
ab0b9fc6
MCC
292 memcpy(name, p, sizeof(name) - 1);
293 name[sizeof(name) - 1] = 0;
294 p += sizeof(name) - 1;
de3fe21b 295
06fd82dc 296 priv->firm_version = le16_to_cpu(*(__u16 *) p);
de3fe21b
MCC
297 p += 2;
298
ab0b9fc6 299 n_array = le16_to_cpu(*(__u16 *) p);
de3fe21b
MCC
300 p += 2;
301
06fd82dc 302 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
5c913c05 303 n_array, fname, name,
06fd82dc 304 priv->firm_version >> 8, priv->firm_version & 0xff);
de3fe21b 305
ab0b9fc6 306 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
06fd82dc
CP
307 if (priv->firm == NULL) {
308 tuner_err("Not enough memory to load firmware file.\n");
ab0b9fc6 309 rc = -ENOMEM;
06fd82dc 310 goto err;
6cb45879 311 }
de3fe21b 312 priv->firm_size = n_array;
06fd82dc 313
ab0b9fc6
MCC
314 n = -1;
315 while (p < endp) {
de3fe21b
MCC
316 __u32 type, size;
317 v4l2_std_id id;
66c2d53d 318 __u16 int_freq = 0;
de3fe21b
MCC
319
320 n++;
321 if (n >= n_array) {
06fd82dc
CP
322 tuner_err("More firmware images in file than "
323 "were expected!\n");
de3fe21b
MCC
324 goto corrupt;
325 }
326
327 /* Checks if there's enough bytes to read */
ab0b9fc6 328 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
83fb340b 329 tuner_err("Firmware header is incomplete!\n");
de3fe21b
MCC
330 goto corrupt;
331 }
332
ab0b9fc6 333 type = le32_to_cpu(*(__u32 *) p);
de3fe21b
MCC
334 p += sizeof(type);
335
ab0b9fc6 336 id = le64_to_cpu(*(v4l2_std_id *) p);
de3fe21b
MCC
337 p += sizeof(id);
338
66c2d53d
MCC
339 if (type & HAS_IF) {
340 int_freq = le16_to_cpu(*(__u16 *) p);
341 p += sizeof(int_freq);
342 }
343
2fc580ff 344 size = le32_to_cpu(*(__u32 *) p);
de3fe21b
MCC
345 p += sizeof(size);
346
ab0b9fc6 347 if ((!size) || (size + p > endp)) {
83fb340b 348 tuner_err("Firmware type ");
43efe702 349 dump_firm_type(type);
ef8c1888
MCC
350 printk("(%x), id %llx is corrupted "
351 "(size=%d, expected %d)\n",
91240dd9 352 type, (unsigned long long)id,
ef8c1888 353 (unsigned)(endp - p), size);
de3fe21b
MCC
354 goto corrupt;
355 }
356
ab0b9fc6 357 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
06fd82dc
CP
358 if (priv->firm[n].ptr == NULL) {
359 tuner_err("Not enough memory to load firmware file.\n");
ab0b9fc6 360 rc = -ENOMEM;
de3fe21b
MCC
361 goto err;
362 }
06fd82dc
CP
363 tuner_dbg("Reading firmware type ");
364 if (debug) {
e0262688 365 dump_firm_type_and_int_freq(type, int_freq);
06fd82dc 366 printk("(%x), id %llx, size=%d.\n",
e0262688 367 type, (unsigned long long)id, size);
06fd82dc 368 }
de3fe21b
MCC
369
370 memcpy(priv->firm[n].ptr, p, size);
371 priv->firm[n].type = type;
372 priv->firm[n].id = id;
373 priv->firm[n].size = size;
66c2d53d 374 priv->firm[n].int_freq = int_freq;
de3fe21b
MCC
375
376 p += size;
377 }
378
ab0b9fc6 379 if (n + 1 != priv->firm_size) {
83fb340b 380 tuner_err("Firmware file is incomplete!\n");
de3fe21b
MCC
381 goto corrupt;
382 }
383
384 goto done;
385
386corrupt:
ab0b9fc6 387 rc = -EINVAL;
83fb340b 388 tuner_err("Error: firmware file is corrupted!\n");
de3fe21b
MCC
389
390err:
06fd82dc 391 tuner_info("Releasing partially loaded firmware file.\n");
de3fe21b
MCC
392 free_firmware(priv);
393
394done:
395 release_firmware(fw);
06fd82dc
CP
396 if (rc == 0)
397 tuner_dbg("Firmware files loaded.\n");
de3fe21b
MCC
398
399 return rc;
400}
401
f380e1d2
MCC
402static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
403 v4l2_std_id *id)
de3fe21b
MCC
404{
405 struct xc2028_data *priv = fe->tuner_priv;
b1535293 406 int i, best_i = -1, best_nr_matches = 0;
33e53161 407 unsigned int type_mask = 0;
de3fe21b 408
7e28adb2 409 tuner_dbg("%s called, want type=", __func__);
b1535293
CP
410 if (debug) {
411 dump_firm_type(type);
412 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
413 }
de3fe21b
MCC
414
415 if (!priv->firm) {
83fb340b 416 tuner_err("Error! firmware not loaded\n");
de3fe21b
MCC
417 return -EINVAL;
418 }
419
f380e1d2 420 if (((type & ~SCODE) == 0) && (*id == 0))
ab0b9fc6 421 *id = V4L2_STD_PAL;
de3fe21b 422
e0f0b37a 423 if (type & BASE)
33e53161 424 type_mask = BASE_TYPES;
ef207fed 425 else if (type & SCODE) {
e0f0b37a 426 type &= SCODE_TYPES;
33e53161 427 type_mask = SCODE_TYPES & ~HAS_IF;
ef207fed 428 } else if (type & DTV_TYPES)
33e53161 429 type_mask = DTV_TYPES;
11a9eff9 430 else if (type & STD_SPECIFIC_TYPES)
33e53161
MCC
431 type_mask = STD_SPECIFIC_TYPES;
432
433 type &= type_mask;
434
435 if (!type & SCODE)
436 type_mask = ~0;
e0f0b37a 437
de3fe21b 438 /* Seek for exact match */
ab0b9fc6 439 for (i = 0; i < priv->firm_size; i++) {
33e53161 440 if ((type == (priv->firm[i].type & type_mask)) &&
ef207fed 441 (*id == priv->firm[i].id))
de3fe21b
MCC
442 goto found;
443 }
444
445 /* Seek for generic video standard match */
ab0b9fc6 446 for (i = 0; i < priv->firm_size; i++) {
b1535293
CP
447 v4l2_std_id match_mask;
448 int nr_matches;
449
33e53161 450 if (type != (priv->firm[i].type & type_mask))
b1535293
CP
451 continue;
452
453 match_mask = *id & priv->firm[i].id;
454 if (!match_mask)
455 continue;
456
457 if ((*id & match_mask) == *id)
458 goto found; /* Supports all the requested standards */
459
460 nr_matches = hweight64(match_mask);
461 if (nr_matches > best_nr_matches) {
462 best_nr_matches = nr_matches;
463 best_i = i;
464 }
465 }
466
467 if (best_nr_matches > 0) {
468 tuner_dbg("Selecting best matching firmware (%d bits) for "
469 "type=", best_nr_matches);
470 dump_firm_type(type);
471 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
472 i = best_i;
473 goto found;
de3fe21b
MCC
474 }
475
476 /*FIXME: Would make sense to seek for type "hint" match ? */
477
b1535293 478 i = -ENOENT;
f380e1d2 479 goto ret;
de3fe21b
MCC
480
481found:
482 *id = priv->firm[i].id;
de3fe21b 483
f380e1d2 484ret:
b1535293 485 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
83fb340b
MCC
486 if (debug) {
487 dump_firm_type(type);
91240dd9 488 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
83fb340b 489 }
f380e1d2
MCC
490 return i;
491}
492
493static int load_firmware(struct dvb_frontend *fe, unsigned int type,
494 v4l2_std_id *id)
495{
496 struct xc2028_data *priv = fe->tuner_priv;
497 int pos, rc;
0a196b6f 498 unsigned char *p, *endp, buf[priv->ctrl.max_len];
f380e1d2 499
7e28adb2 500 tuner_dbg("%s called\n", __func__);
f380e1d2
MCC
501
502 pos = seek_firmware(fe, type, id);
503 if (pos < 0)
504 return pos;
505
83fb340b 506 tuner_info("Loading firmware for type=");
b1535293
CP
507 dump_firm_type(priv->firm[pos].type);
508 printk("(%x), id %016llx.\n", priv->firm[pos].type,
509 (unsigned long long)*id);
83fb340b 510
f380e1d2 511 p = priv->firm[pos].ptr;
f380e1d2 512 endp = p + priv->firm[pos].size;
6cb45879 513
ab0b9fc6 514 while (p < endp) {
de3fe21b
MCC
515 __u16 size;
516
517 /* Checks if there's enough bytes to read */
ab0b9fc6 518 if (p + sizeof(size) > endp) {
83fb340b 519 tuner_err("Firmware chunk size is wrong\n");
de3fe21b
MCC
520 return -EINVAL;
521 }
522
ab0b9fc6 523 size = le16_to_cpu(*(__u16 *) p);
de3fe21b
MCC
524 p += sizeof(size);
525
526 if (size == 0xffff)
527 return 0;
528
529 if (!size) {
6cb45879 530 /* Special callback command received */
215b95ba 531 rc = priv->tuner_callback(priv->video_dev,
ab0b9fc6
MCC
532 XC2028_TUNER_RESET, 0);
533 if (rc < 0) {
83fb340b 534 tuner_err("Error at RESET code %d\n",
ab0b9fc6 535 (*p) & 0x7f);
de3fe21b 536 return -EINVAL;
6cb45879 537 }
6cb45879
MCC
538 continue;
539 }
5403bbae
ML
540 if (size >= 0xff00) {
541 switch (size) {
542 case 0xff00:
543 rc = priv->tuner_callback(priv->video_dev,
544 XC2028_RESET_CLK, 0);
545 if (rc < 0) {
546 tuner_err("Error at RESET code %d\n",
547 (*p) & 0x7f);
548 return -EINVAL;
549 }
b32f9fb9 550 break;
5403bbae
ML
551 default:
552 tuner_info("Invalid RESET code %d\n",
553 size & 0x7f);
554 return -EINVAL;
555
556 }
2d4c0ac6 557 continue;
5403bbae 558 }
de3fe21b
MCC
559
560 /* Checks for a sleep command */
561 if (size & 0x8000) {
ab0b9fc6 562 msleep(size & 0x7fff);
de3fe21b 563 continue;
6cb45879
MCC
564 }
565
de3fe21b 566 if ((size + p > endp)) {
83fb340b 567 tuner_err("missing bytes: need %d, have %d\n",
ab0b9fc6 568 size, (int)(endp - p));
de3fe21b
MCC
569 return -EINVAL;
570 }
6cb45879 571
de3fe21b 572 buf[0] = *p;
6cb45879 573 p++;
de3fe21b 574 size--;
6cb45879 575
de3fe21b 576 /* Sends message chunks */
ab0b9fc6 577 while (size > 0) {
0a196b6f
CP
578 int len = (size < priv->ctrl.max_len - 1) ?
579 size : priv->ctrl.max_len - 1;
6cb45879 580
ab0b9fc6 581 memcpy(buf + 1, p, len);
6cb45879 582
47cc5b78 583 rc = i2c_send(priv, buf, len + 1);
ab0b9fc6 584 if (rc < 0) {
83fb340b 585 tuner_err("%d returned from send\n", rc);
de3fe21b
MCC
586 return -EINVAL;
587 }
588
589 p += len;
590 size -= len;
591 }
592 }
43efe702 593 return 0;
6cb45879
MCC
594}
595
f380e1d2 596static int load_scode(struct dvb_frontend *fe, unsigned int type,
66c2d53d 597 v4l2_std_id *id, __u16 int_freq, int scode)
f380e1d2
MCC
598{
599 struct xc2028_data *priv = fe->tuner_priv;
600 int pos, rc;
601 unsigned char *p;
602
7e28adb2 603 tuner_dbg("%s called\n", __func__);
f380e1d2 604
66c2d53d
MCC
605 if (!int_freq) {
606 pos = seek_firmware(fe, type, id);
607 if (pos < 0)
608 return pos;
609 } else {
610 for (pos = 0; pos < priv->firm_size; pos++) {
611 if ((priv->firm[pos].int_freq == int_freq) &&
9ca01e78 612 (priv->firm[pos].type & HAS_IF))
66c2d53d
MCC
613 break;
614 }
615 if (pos == priv->firm_size)
616 return -ENOENT;
617 }
f380e1d2
MCC
618
619 p = priv->firm[pos].ptr;
620
9ca01e78 621 if (priv->firm[pos].type & HAS_IF) {
66c2d53d
MCC
622 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
623 return -EINVAL;
624 p += 12 * scode;
625 } else {
626 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
627 * has a 2-byte size header in the firmware format. */
628 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
629 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
630 return -EINVAL;
631 p += 14 * scode + 2;
632 }
f380e1d2 633
d7b22c5c 634 tuner_info("Loading SCODE for type=");
e0262688
CP
635 dump_firm_type_and_int_freq(priv->firm[pos].type,
636 priv->firm[pos].int_freq);
d7b22c5c
CP
637 printk("(%x), id %016llx.\n", priv->firm[pos].type,
638 (unsigned long long)*id);
639
06fd82dc 640 if (priv->firm_version < 0x0202)
47cc5b78
CP
641 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
642 else
643 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
644 if (rc < 0)
645 return -EIO;
f380e1d2 646
66c2d53d 647 rc = i2c_send(priv, p, 12);
47cc5b78
CP
648 if (rc < 0)
649 return -EIO;
f380e1d2 650
47cc5b78
CP
651 rc = send_seq(priv, {0x00, 0x8c});
652 if (rc < 0)
653 return -EIO;
f380e1d2
MCC
654
655 return 0;
656}
657
00deff1a 658static int check_firmware(struct dvb_frontend *fe, unsigned int type,
66c2d53d 659 v4l2_std_id std, __u16 int_freq)
6cb45879 660{
00deff1a 661 struct xc2028_data *priv = fe->tuner_priv;
e0f0b37a 662 struct firmware_properties new_fw;
00deff1a
MCC
663 int rc = 0, is_retry = 0;
664 u16 version, hwmodel;
665 v4l2_std_id std0;
6cb45879 666
7e28adb2 667 tuner_dbg("%s called\n", __func__);
6cb45879 668
de3fe21b 669 if (!priv->firm) {
a37b4c9b
ML
670 if (!priv->ctrl.fname) {
671 tuner_info("xc2028/3028 firmware name not set!\n");
de3fe21b 672 return -EINVAL;
a37b4c9b 673 }
de3fe21b 674
ab0b9fc6
MCC
675 rc = load_all_firmwares(fe);
676 if (rc < 0)
de3fe21b
MCC
677 return rc;
678 }
679
0f6dac18 680 if (priv->ctrl.mts && !(type & FM))
e0f0b37a 681 type |= MTS;
6cb45879 682
8bf799a6 683retry:
e0f0b37a
CP
684 new_fw.type = type;
685 new_fw.id = std;
686 new_fw.std_req = std;
687 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
688 new_fw.scode_nr = 0;
66c2d53d 689 new_fw.int_freq = int_freq;
e0f0b37a
CP
690
691 tuner_dbg("checking firmware, user requested type=");
692 if (debug) {
693 dump_firm_type(new_fw.type);
e0262688 694 printk("(%x), id %016llx, ", new_fw.type,
e0f0b37a 695 (unsigned long long)new_fw.std_req);
e0262688
CP
696 if (!int_freq) {
697 printk("scode_tbl ");
698 dump_firm_type(priv->ctrl.scode_table);
699 printk("(%x), ", priv->ctrl.scode_table);
700 } else
701 printk("int_freq %d, ", new_fw.int_freq);
702 printk("scode_nr %d\n", new_fw.scode_nr);
e0f0b37a
CP
703 }
704
705 /* No need to reload base firmware if it matches */
706 if (((BASE | new_fw.type) & BASE_TYPES) ==
707 (priv->cur_fw.type & BASE_TYPES)) {
708 tuner_dbg("BASE firmware not changed.\n");
709 goto skip_base;
710 }
711
712 /* Updating BASE - forget about all currently loaded firmware */
713 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
714
715 /* Reset is needed before loading firmware */
716 rc = priv->tuner_callback(priv->video_dev,
717 XC2028_TUNER_RESET, 0);
718 if (rc < 0)
719 goto fail;
720
47bd5bc6
CP
721 /* BASE firmwares are all std0 */
722 std0 = 0;
723 rc = load_firmware(fe, BASE | new_fw.type, &std0);
e0f0b37a
CP
724 if (rc < 0) {
725 tuner_err("Error %d while loading base firmware\n",
726 rc);
727 goto fail;
728 }
5403bbae 729
de3fe21b 730 /* Load INIT1, if needed */
83fb340b 731 tuner_dbg("Load init1 firmware, if exists\n");
de3fe21b 732
47bd5bc6 733 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1ad0b796
CP
734 if (rc == -ENOENT)
735 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
736 &std0);
e0f0b37a
CP
737 if (rc < 0 && rc != -ENOENT) {
738 tuner_err("Error %d while loading init1 firmware\n",
739 rc);
740 goto fail;
741 }
de3fe21b 742
e0f0b37a
CP
743skip_base:
744 /*
745 * No need to reload standard specific firmware if base firmware
746 * was not reloaded and requested video standards have not changed.
de3fe21b 747 */
e0f0b37a
CP
748 if (priv->cur_fw.type == (BASE | new_fw.type) &&
749 priv->cur_fw.std_req == std) {
83fb340b 750 tuner_dbg("Std-specific firmware already loaded.\n");
e0f0b37a 751 goto skip_std_specific;
2e4160ca 752 }
6cb45879 753
e0f0b37a
CP
754 /* Reloading std-specific firmware forces a SCODE update */
755 priv->cur_fw.scode_table = 0;
756
e0f0b37a 757 rc = load_firmware(fe, new_fw.type, &new_fw.id);
cca83798
MCC
758 if (rc == -ENOENT)
759 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
760
ab0b9fc6 761 if (rc < 0)
e0f0b37a
CP
762 goto fail;
763
764skip_std_specific:
765 if (priv->cur_fw.scode_table == new_fw.scode_table &&
766 priv->cur_fw.scode_nr == new_fw.scode_nr) {
767 tuner_dbg("SCODE firmware already loaded.\n");
768 goto check_device;
769 }
6cb45879 770
40ae91a7
MCC
771 if (new_fw.type & FM)
772 goto check_device;
773
f380e1d2 774 /* Load SCODE firmware, if exists */
e0f0b37a 775 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
f380e1d2 776
66c2d53d
MCC
777 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
778 new_fw.int_freq, new_fw.scode_nr);
43efe702 779
e0f0b37a 780check_device:
8bf799a6
CP
781 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
782 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
783 tuner_err("Unable to read tuner registers.\n");
784 goto fail;
785 }
80b52208 786
b37f2d6a
DH
787 tuner_dbg("Device is Xceive %d version %d.%d, "
788 "firmware version %d.%d\n",
789 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
790 (version & 0xf0) >> 4, version & 0xf);
6cb45879 791
8bf799a6
CP
792 /* Check firmware version against what we downloaded. */
793 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
794 tuner_err("Incorrect readback of firmware version.\n");
795 goto fail;
796 }
797
798 /* Check that the tuner hardware model remains consistent over time. */
799 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
800 priv->hwmodel = hwmodel;
801 priv->hwvers = version & 0xff00;
802 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
803 priv->hwvers != (version & 0xff00)) {
804 tuner_err("Read invalid device hardware information - tuner "
805 "hung?\n");
806 goto fail;
807 }
808
e0f0b37a
CP
809 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
810
811 /*
812 * By setting BASE in cur_fw.type only after successfully loading all
813 * firmwares, we can:
814 * 1. Identify that BASE firmware with type=0 has been loaded;
815 * 2. Tell whether BASE firmware was just changed the next time through.
816 */
817 priv->cur_fw.type |= BASE;
6cb45879
MCC
818
819 return 0;
e0f0b37a
CP
820
821fail:
822 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
8bf799a6
CP
823 if (!is_retry) {
824 msleep(50);
825 is_retry = 1;
826 tuner_dbg("Retrying firmware load\n");
827 goto retry;
828 }
829
e0f0b37a
CP
830 if (rc == -ENOENT)
831 rc = -EINVAL;
832 return rc;
6cb45879
MCC
833}
834
215b95ba 835static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
6cb45879 836{
215b95ba 837 struct xc2028_data *priv = fe->tuner_priv;
7d58d111
CP
838 u16 frq_lock, signal = 0;
839 int rc;
3b20532c 840
7e28adb2 841 tuner_dbg("%s called\n", __func__);
6cb45879 842
215b95ba 843 mutex_lock(&priv->lock);
6cb45879 844
80b52208 845 /* Sync Lock Indicator */
7d58d111 846 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
b0166ab3 847 if (rc < 0)
3b20532c 848 goto ret;
6cb45879 849
b0166ab3
MCC
850 /* Frequency is locked */
851 if (frq_lock == 1)
852 signal = 32768;
6cb45879 853
80b52208 854 /* Get SNR of the video signal */
7d58d111
CP
855 rc = xc2028_get_reg(priv, 0x0040, &signal);
856 if (rc < 0)
b0166ab3
MCC
857 goto ret;
858
859 /* Use both frq_lock and signal to generate the result */
860 signal = signal || ((signal & 0x07) << 12);
3b20532c
MCC
861
862ret:
215b95ba
MCC
863 mutex_unlock(&priv->lock);
864
865 *strength = signal;
6cb45879 866
b0166ab3
MCC
867 tuner_dbg("signal strength is %d\n", signal);
868
7d58d111 869 return rc;
6cb45879
MCC
870}
871
872#define DIV 15625
873
00deff1a 874static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
66c2d53d
MCC
875 enum tuner_mode new_mode,
876 unsigned int type,
877 v4l2_std_id std,
878 u16 int_freq)
6cb45879 879{
215b95ba 880 struct xc2028_data *priv = fe->tuner_priv;
ab0b9fc6 881 int rc = -EINVAL;
2ce4b3aa 882 unsigned char buf[4];
ab0b9fc6 883 u32 div, offset = 0;
6cb45879 884
7e28adb2 885 tuner_dbg("%s called\n", __func__);
215b95ba 886
de3fe21b
MCC
887 mutex_lock(&priv->lock);
888
2ce4b3aa 889 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
6cb45879 890
66c2d53d 891 if (check_firmware(fe, type, std, int_freq) < 0)
3b20532c 892 goto ret;
2e4160ca 893
2800ae9c
MCC
894 /* On some cases xc2028 can disable video output, if
895 * very weak signals are received. By sending a soft
896 * reset, this is re-enabled. So, it is better to always
897 * send a soft reset before changing channels, to be sure
898 * that xc2028 will be in a safe state.
899 * Maybe this might also be needed for DTV.
900 */
00deff1a 901 if (new_mode == T_ANALOG_TV) {
2800ae9c 902 rc = send_seq(priv, {0x00, 0x00});
d536c9df
MK
903 } else if (priv->cur_fw.type & ATSC) {
904 offset = 1750000;
905 } else {
d4e76681 906 offset = 2750000;
897b8422
CP
907 /*
908 * We must adjust the offset by 500kHz in two cases in order
909 * to correctly center the IF output:
910 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
911 * selected and a 7MHz channel is tuned;
912 * 2) When tuning a VHF channel with DTV78 firmware.
913 */
914 if (((priv->cur_fw.type & DTV7) &&
915 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
916 ((priv->cur_fw.type & DTV78) && freq < 470000000))
a44f1c43
CP
917 offset -= 500000;
918 }
2e4160ca 919
ab0b9fc6 920 div = (freq - offset + DIV / 2) / DIV;
2e4160ca 921
6cb45879 922 /* CMD= Set frequency */
06fd82dc 923 if (priv->firm_version < 0x0202)
47cc5b78
CP
924 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
925 else
926 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
927 if (rc < 0)
928 goto ret;
de3fe21b 929
1fe87369
MCC
930 /* Return code shouldn't be checked.
931 The reset CLK is needed only with tm6000.
932 Driver should work fine even if this fails.
933 */
934 priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
6cb45879
MCC
935
936 msleep(10);
701672eb 937
ab0b9fc6
MCC
938 buf[0] = 0xff & (div >> 24);
939 buf[1] = 0xff & (div >> 16);
940 buf[2] = 0xff & (div >> 8);
941 buf[3] = 0xff & (div);
6cb45879 942
47cc5b78 943 rc = i2c_send(priv, buf, sizeof(buf));
ab0b9fc6 944 if (rc < 0)
3b20532c 945 goto ret;
6cb45879
MCC
946 msleep(100);
947
ab0b9fc6 948 priv->frequency = freq;
215b95ba 949
2ce4b3aa
CP
950 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
951 buf[0], buf[1], buf[2], buf[3],
952 freq / 1000000, (freq % 1000000) / 1000);
3b20532c 953
ab0b9fc6 954 rc = 0;
6cb45879 955
215b95ba
MCC
956ret:
957 mutex_unlock(&priv->lock);
6cb45879 958
215b95ba 959 return rc;
701672eb
ML
960}
961
00deff1a 962static int xc2028_set_analog_freq(struct dvb_frontend *fe,
ab0b9fc6 963 struct analog_parameters *p)
6cb45879 964{
215b95ba 965 struct xc2028_data *priv = fe->tuner_priv;
00deff1a
MCC
966 unsigned int type=0;
967
7e28adb2 968 tuner_dbg("%s called\n", __func__);
c71d4bc5 969
d74cb25e
MCC
970 if (p->mode == V4L2_TUNER_RADIO) {
971 type |= FM;
972 if (priv->ctrl.input1)
973 type |= INPUT1;
974 return generic_set_freq(fe, (625l * p->frequency) / 10,
66c2d53d 975 T_ANALOG_TV, type, 0, 0);
d74cb25e
MCC
976 }
977
a5e9fe14
MCC
978 /* if std is not defined, choose one */
979 if (!p->std)
980 p->std = V4L2_STD_MN;
981
982 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
00deff1a
MCC
983 if (!(p->std & V4L2_STD_MN))
984 type |= F8MHZ;
6cb45879 985
00deff1a
MCC
986 /* Add audio hack to std mask */
987 p->std |= parse_audio_std_option();
6cb45879 988
00deff1a 989 return generic_set_freq(fe, 62500l * p->frequency,
66c2d53d 990 T_ANALOG_TV, type, p->std, 0);
215b95ba 991}
6cb45879 992
215b95ba
MCC
993static int xc2028_set_params(struct dvb_frontend *fe,
994 struct dvb_frontend_parameters *p)
6cb45879 995{
215b95ba 996 struct xc2028_data *priv = fe->tuner_priv;
00deff1a 997 unsigned int type=0;
d04aa54a 998 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
ad35ce9e 999 u16 demod = 0;
6cb45879 1000
7e28adb2 1001 tuner_dbg("%s called\n", __func__);
701672eb 1002
00deff1a
MCC
1003 if (priv->ctrl.d2633)
1004 type |= D2633;
1005 else
1006 type |= D2620;
1007
d04aa54a 1008 switch(fe->ops.info.type) {
d04aa54a
MCC
1009 case FE_OFDM:
1010 bw = p->u.ofdm.bandwidth;
1011 break;
1012 case FE_QAM:
5c15648a
MCC
1013 tuner_info("WARN: There are some reports that "
1014 "QAM 6 MHz doesn't work.\n"
1015 "If this works for you, please report by "
1016 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
d04aa54a
MCC
1017 bw = BANDWIDTH_6_MHZ;
1018 type |= QAM;
1019 break;
1020 case FE_ATSC:
1021 bw = BANDWIDTH_6_MHZ;
4bfae52b
MCC
1022 /* The only ATSC firmware (at least on v2.7) is D2633,
1023 so overrides ctrl->d2633 */
d04aa54a 1024 type |= ATSC| D2633;
4bfae52b 1025 type &= ~D2620;
d04aa54a 1026 break;
5c15648a
MCC
1027 /* DVB-S is not supported */
1028 default:
1029 return -EINVAL;
d04aa54a
MCC
1030 }
1031
00deff1a
MCC
1032 switch (bw) {
1033 case BANDWIDTH_8_MHZ:
3dfefc50
CP
1034 if (p->frequency < 470000000)
1035 priv->ctrl.vhfbw7 = 0;
1036 else
1037 priv->ctrl.uhfbw8 = 1;
1038 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1039 type |= F8MHZ;
00deff1a
MCC
1040 break;
1041 case BANDWIDTH_7_MHZ:
3dfefc50
CP
1042 if (p->frequency < 470000000)
1043 priv->ctrl.vhfbw7 = 1;
1044 else
1045 priv->ctrl.uhfbw8 = 0;
1046 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1047 type |= F8MHZ;
00deff1a
MCC
1048 break;
1049 case BANDWIDTH_6_MHZ:
3dfefc50
CP
1050 type |= DTV6;
1051 priv->ctrl.vhfbw7 = 0;
1052 priv->ctrl.uhfbw8 = 0;
00deff1a
MCC
1053 break;
1054 default:
1055 tuner_err("error: bandwidth not supported.\n");
1056 };
1057
66c2d53d
MCC
1058 /* All S-code tables need a 200kHz shift */
1059 if (priv->ctrl.demod)
ad35ce9e 1060 demod = priv->ctrl.demod + 200;
b542dfdc 1061
00deff1a 1062 return generic_set_freq(fe, p->frequency,
ad35ce9e 1063 T_DIGITAL_TV, type, 0, demod);
6cb45879 1064}
701672eb 1065
45819c38 1066
215b95ba 1067static int xc2028_dvb_release(struct dvb_frontend *fe)
701672eb 1068{
215b95ba
MCC
1069 struct xc2028_data *priv = fe->tuner_priv;
1070
7e28adb2 1071 tuner_dbg("%s called\n", __func__);
701672eb 1072
aa501be9
CP
1073 mutex_lock(&xc2028_list_mutex);
1074
215b95ba 1075 priv->count--;
701672eb 1076
de3fe21b 1077 if (!priv->count) {
1808a698
MCC
1078 list_del(&priv->xc2028_list);
1079
ab0b9fc6 1080 kfree(priv->ctrl.fname);
de3fe21b
MCC
1081
1082 free_firmware(priv);
ab0b9fc6 1083 kfree(priv);
06fd82dc 1084 fe->tuner_priv = NULL;
de3fe21b 1085 }
701672eb 1086
aa501be9
CP
1087 mutex_unlock(&xc2028_list_mutex);
1088
701672eb
ML
1089 return 0;
1090}
1091
215b95ba 1092static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
701672eb 1093{
215b95ba 1094 struct xc2028_data *priv = fe->tuner_priv;
701672eb 1095
7e28adb2 1096 tuner_dbg("%s called\n", __func__);
701672eb 1097
215b95ba 1098 *frequency = priv->frequency;
701672eb
ML
1099
1100 return 0;
1101}
1102
ab0b9fc6 1103static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
de3fe21b
MCC
1104{
1105 struct xc2028_data *priv = fe->tuner_priv;
1106 struct xc2028_ctrl *p = priv_cfg;
0a196b6f 1107 int rc = 0;
de3fe21b 1108
7e28adb2 1109 tuner_dbg("%s called\n", __func__);
de3fe21b 1110
06fd82dc
CP
1111 mutex_lock(&priv->lock);
1112
0a196b6f 1113 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
92b75ab0
MCC
1114 if (priv->ctrl.max_len < 9)
1115 priv->ctrl.max_len = 13;
de3fe21b 1116
0a196b6f 1117 if (p->fname) {
92b75ab0
MCC
1118 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1119 kfree(priv->ctrl.fname);
1120 free_firmware(priv);
1121 }
1122
0a196b6f
CP
1123 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1124 if (priv->ctrl.fname == NULL)
1125 rc = -ENOMEM;
de3fe21b
MCC
1126 }
1127
06fd82dc
CP
1128 mutex_unlock(&priv->lock);
1129
0a196b6f 1130 return rc;
de3fe21b
MCC
1131}
1132
215b95ba 1133static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
701672eb 1134 .info = {
ab0b9fc6
MCC
1135 .name = "Xceive XC3028",
1136 .frequency_min = 42000000,
1137 .frequency_max = 864000000,
1138 .frequency_step = 50000,
1139 },
701672eb 1140
de3fe21b 1141 .set_config = xc2028_set_config,
00deff1a 1142 .set_analog_params = xc2028_set_analog_freq,
215b95ba
MCC
1143 .release = xc2028_dvb_release,
1144 .get_frequency = xc2028_get_frequency,
1145 .get_rf_strength = xc2028_signal,
1146 .set_params = xc2028_set_params,
701672eb
ML
1147};
1148
7972f988
MK
1149struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1150 struct xc2028_config *cfg)
701672eb 1151{
215b95ba 1152 struct xc2028_data *priv;
a37b4c9b 1153 void *video_dev;
701672eb 1154
83fb340b 1155 if (debug)
2756665c 1156 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
701672eb 1157
b412ba78 1158 if (NULL == cfg)
a37b4c9b 1159 return NULL;
215b95ba 1160
a37b4c9b 1161 if (!fe) {
2756665c 1162 printk(KERN_ERR "xc2028: No frontend!\n");
a37b4c9b 1163 return NULL;
215b95ba
MCC
1164 }
1165
b412ba78
MCC
1166 video_dev = cfg->i2c_adap->algo_data;
1167
1168 if (debug)
1169 printk(KERN_DEBUG "xc2028: video_dev =%p\n", video_dev);
a37b4c9b 1170
aa501be9
CP
1171 mutex_lock(&xc2028_list_mutex);
1172
215b95ba 1173 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
b412ba78 1174 if (&priv->i2c_props.adap->dev == &cfg->i2c_adap->dev) {
a37b4c9b 1175 video_dev = NULL;
b412ba78
MCC
1176 if (debug)
1177 printk(KERN_DEBUG "xc2028: reusing device\n");
1178
a37b4c9b
ML
1179 break;
1180 }
215b95ba
MCC
1181 }
1182
a37b4c9b 1183 if (video_dev) {
215b95ba 1184 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
aa501be9
CP
1185 if (priv == NULL) {
1186 mutex_unlock(&xc2028_list_mutex);
a37b4c9b 1187 return NULL;
aa501be9 1188 }
3b20532c 1189
a37b4c9b
ML
1190 priv->i2c_props.addr = cfg->i2c_addr;
1191 priv->i2c_props.adap = cfg->i2c_adap;
2756665c
MK
1192 priv->i2c_props.name = "xc2028";
1193
215b95ba 1194 priv->video_dev = video_dev;
a37b4c9b 1195 priv->tuner_callback = cfg->callback;
0a196b6f 1196 priv->ctrl.max_len = 13;
de3fe21b 1197
215b95ba
MCC
1198 mutex_init(&priv->lock);
1199
ab0b9fc6 1200 list_add_tail(&priv->xc2028_list, &xc2028_list);
215b95ba 1201 }
a37b4c9b
ML
1202
1203 fe->tuner_priv = priv;
1808a698 1204 priv->count++;
215b95ba 1205
b412ba78
MCC
1206 if (debug)
1207 printk(KERN_DEBUG "xc2028: usage count is %i\n", priv->count);
1208
215b95ba 1209 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
ab0b9fc6 1210 sizeof(xc2028_dvb_tuner_ops));
215b95ba
MCC
1211
1212 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1213
71a2ee37
MCC
1214 if (cfg->ctrl)
1215 xc2028_set_config(fe, cfg->ctrl);
1216
aa501be9
CP
1217 mutex_unlock(&xc2028_list_mutex);
1218
a37b4c9b 1219 return fe;
215b95ba 1220}
a37b4c9b 1221
701672eb
ML
1222EXPORT_SYMBOL(xc2028_attach);
1223
215b95ba 1224MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
983d214e 1225MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
215b95ba
MCC
1226MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1227MODULE_LICENSE("GPL");