]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/tuners/tuner-xc2028.c
Merge branch 'patchwork' into v4l_for_linus
[mirror_ubuntu-artful-kernel.git] / drivers / media / tuners / tuner-xc2028.c
1 /* tuner-xc2028
2 *
3 * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org)
4 *
5 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
7 *
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>
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 #include <linux/dvb/frontend.h>
25 #include "dvb_frontend.h"
26
27 /* Max transfer size done by I2C transfer functions */
28 #define MAX_XFER_SIZE 80
29
30 /* Registers (Write-only) */
31 #define XREG_INIT 0x00
32 #define XREG_RF_FREQ 0x02
33 #define XREG_POWER_DOWN 0x08
34
35 /* Registers (Read-only) */
36 #define XREG_FREQ_ERROR 0x01
37 #define XREG_LOCK 0x02
38 #define XREG_VERSION 0x04
39 #define XREG_PRODUCT_ID 0x08
40 #define XREG_HSYNC_FREQ 0x10
41 #define XREG_FRAME_LINES 0x20
42 #define XREG_SNR 0x40
43
44 #define XREG_ADC_ENV 0x0100
45
46 static int debug;
47 module_param(debug, int, 0644);
48 MODULE_PARM_DESC(debug, "enable verbose debug messages");
49
50 static int no_poweroff;
51 module_param(no_poweroff, int, 0644);
52 MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
53 "1 keep device energized and with tuner ready all the times.\n"
54 " Faster, but consumes more power and keeps the device hotter\n");
55
56 static char audio_std[8];
57 module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
58 MODULE_PARM_DESC(audio_std,
59 "Audio standard. XC3028 audio decoder explicitly needs to know what audio\n"
60 "standard is needed for some video standards with audio A2 or NICAM.\n"
61 "The valid values are:\n"
62 "A2\n"
63 "A2/A\n"
64 "A2/B\n"
65 "NICAM\n"
66 "NICAM/A\n"
67 "NICAM/B\n");
68
69 static char firmware_name[30];
70 module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
71 MODULE_PARM_DESC(firmware_name,
72 "Firmware file name. Allows overriding the default firmware name\n");
73
74 static LIST_HEAD(hybrid_tuner_instance_list);
75 static DEFINE_MUTEX(xc2028_list_mutex);
76
77 /* struct for storing firmware table */
78 struct firmware_description {
79 unsigned int type;
80 v4l2_std_id id;
81 __u16 int_freq;
82 unsigned char *ptr;
83 unsigned int size;
84 };
85
86 struct firmware_properties {
87 unsigned int type;
88 v4l2_std_id id;
89 v4l2_std_id std_req;
90 __u16 int_freq;
91 unsigned int scode_table;
92 int scode_nr;
93 };
94
95 enum xc2028_state {
96 XC2028_NO_FIRMWARE = 0,
97 XC2028_WAITING_FIRMWARE,
98 XC2028_ACTIVE,
99 XC2028_SLEEP,
100 XC2028_NODEV,
101 };
102
103 struct xc2028_data {
104 struct list_head hybrid_tuner_instance_list;
105 struct tuner_i2c_props i2c_props;
106 __u32 frequency;
107
108 enum xc2028_state state;
109 const char *fname;
110
111 struct firmware_description *firm;
112 int firm_size;
113 __u16 firm_version;
114
115 __u16 hwmodel;
116 __u16 hwvers;
117
118 struct xc2028_ctrl ctrl;
119
120 struct firmware_properties cur_fw;
121
122 struct mutex lock;
123 };
124
125 #define i2c_send(priv, buf, size) ({ \
126 int _rc; \
127 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
128 if (size != _rc) \
129 tuner_info("i2c output error: rc = %d (should be %d)\n",\
130 _rc, (int)size); \
131 if (priv->ctrl.msleep) \
132 msleep(priv->ctrl.msleep); \
133 _rc; \
134 })
135
136 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
137 int _rc; \
138 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
139 ibuf, isize); \
140 if (isize != _rc) \
141 tuner_err("i2c input error: rc = %d (should be %d)\n", \
142 _rc, (int)isize); \
143 if (priv->ctrl.msleep) \
144 msleep(priv->ctrl.msleep); \
145 _rc; \
146 })
147
148 #define send_seq(priv, data...) ({ \
149 static u8 _val[] = data; \
150 int _rc; \
151 if (sizeof(_val) != \
152 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
153 _val, sizeof(_val)))) { \
154 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
155 } else if (priv->ctrl.msleep) \
156 msleep(priv->ctrl.msleep); \
157 _rc; \
158 })
159
160 static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
161 {
162 unsigned char buf[2];
163 unsigned char ibuf[2];
164
165 tuner_dbg("%s %04x called\n", __func__, reg);
166
167 buf[0] = reg >> 8;
168 buf[1] = (unsigned char) reg;
169
170 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
171 return -EIO;
172
173 *val = (ibuf[1]) | (ibuf[0] << 8);
174 return 0;
175 }
176
177 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
178 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
179 {
180 if (type & BASE)
181 printk(KERN_CONT "BASE ");
182 if (type & INIT1)
183 printk(KERN_CONT "INIT1 ");
184 if (type & F8MHZ)
185 printk(KERN_CONT "F8MHZ ");
186 if (type & MTS)
187 printk(KERN_CONT "MTS ");
188 if (type & D2620)
189 printk(KERN_CONT "D2620 ");
190 if (type & D2633)
191 printk(KERN_CONT "D2633 ");
192 if (type & DTV6)
193 printk(KERN_CONT "DTV6 ");
194 if (type & QAM)
195 printk(KERN_CONT "QAM ");
196 if (type & DTV7)
197 printk(KERN_CONT "DTV7 ");
198 if (type & DTV78)
199 printk(KERN_CONT "DTV78 ");
200 if (type & DTV8)
201 printk(KERN_CONT "DTV8 ");
202 if (type & FM)
203 printk(KERN_CONT "FM ");
204 if (type & INPUT1)
205 printk(KERN_CONT "INPUT1 ");
206 if (type & LCD)
207 printk(KERN_CONT "LCD ");
208 if (type & NOGD)
209 printk(KERN_CONT "NOGD ");
210 if (type & MONO)
211 printk(KERN_CONT "MONO ");
212 if (type & ATSC)
213 printk(KERN_CONT "ATSC ");
214 if (type & IF)
215 printk(KERN_CONT "IF ");
216 if (type & LG60)
217 printk(KERN_CONT "LG60 ");
218 if (type & ATI638)
219 printk(KERN_CONT "ATI638 ");
220 if (type & OREN538)
221 printk(KERN_CONT "OREN538 ");
222 if (type & OREN36)
223 printk(KERN_CONT "OREN36 ");
224 if (type & TOYOTA388)
225 printk(KERN_CONT "TOYOTA388 ");
226 if (type & TOYOTA794)
227 printk(KERN_CONT "TOYOTA794 ");
228 if (type & DIBCOM52)
229 printk(KERN_CONT "DIBCOM52 ");
230 if (type & ZARLINK456)
231 printk(KERN_CONT "ZARLINK456 ");
232 if (type & CHINA)
233 printk(KERN_CONT "CHINA ");
234 if (type & F6MHZ)
235 printk(KERN_CONT "F6MHZ ");
236 if (type & INPUT2)
237 printk(KERN_CONT "INPUT2 ");
238 if (type & SCODE)
239 printk(KERN_CONT "SCODE ");
240 if (type & HAS_IF)
241 printk(KERN_CONT "HAS_IF_%d ", int_freq);
242 }
243
244 static v4l2_std_id parse_audio_std_option(void)
245 {
246 if (strcasecmp(audio_std, "A2") == 0)
247 return V4L2_STD_A2;
248 if (strcasecmp(audio_std, "A2/A") == 0)
249 return V4L2_STD_A2_A;
250 if (strcasecmp(audio_std, "A2/B") == 0)
251 return V4L2_STD_A2_B;
252 if (strcasecmp(audio_std, "NICAM") == 0)
253 return V4L2_STD_NICAM;
254 if (strcasecmp(audio_std, "NICAM/A") == 0)
255 return V4L2_STD_NICAM_A;
256 if (strcasecmp(audio_std, "NICAM/B") == 0)
257 return V4L2_STD_NICAM_B;
258
259 return 0;
260 }
261
262 static int check_device_status(struct xc2028_data *priv)
263 {
264 switch (priv->state) {
265 case XC2028_NO_FIRMWARE:
266 case XC2028_WAITING_FIRMWARE:
267 return -EAGAIN;
268 case XC2028_ACTIVE:
269 return 1;
270 case XC2028_SLEEP:
271 return 0;
272 case XC2028_NODEV:
273 return -ENODEV;
274 }
275 return 0;
276 }
277
278 static void free_firmware(struct xc2028_data *priv)
279 {
280 int i;
281 tuner_dbg("%s called\n", __func__);
282
283 /* free allocated f/w string */
284 if (priv->fname != firmware_name)
285 kfree(priv->fname);
286 priv->fname = NULL;
287
288 priv->state = XC2028_NO_FIRMWARE;
289 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
290
291 if (!priv->firm)
292 return;
293
294 for (i = 0; i < priv->firm_size; i++)
295 kfree(priv->firm[i].ptr);
296
297 kfree(priv->firm);
298
299 priv->firm = NULL;
300 priv->firm_size = 0;
301 }
302
303 static int load_all_firmwares(struct dvb_frontend *fe,
304 const struct firmware *fw)
305 {
306 struct xc2028_data *priv = fe->tuner_priv;
307 const unsigned char *p, *endp;
308 int rc = 0;
309 int n, n_array;
310 char name[33];
311
312 tuner_dbg("%s called\n", __func__);
313
314 p = fw->data;
315 endp = p + fw->size;
316
317 if (fw->size < sizeof(name) - 1 + 2 + 2) {
318 tuner_err("Error: firmware file %s has invalid size!\n",
319 priv->fname);
320 goto corrupt;
321 }
322
323 memcpy(name, p, sizeof(name) - 1);
324 name[sizeof(name) - 1] = 0;
325 p += sizeof(name) - 1;
326
327 priv->firm_version = get_unaligned_le16(p);
328 p += 2;
329
330 n_array = get_unaligned_le16(p);
331 p += 2;
332
333 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
334 n_array, priv->fname, name,
335 priv->firm_version >> 8, priv->firm_version & 0xff);
336
337 priv->firm = kcalloc(n_array, sizeof(*priv->firm), GFP_KERNEL);
338 if (priv->firm == NULL) {
339 tuner_err("Not enough memory to load firmware file.\n");
340 rc = -ENOMEM;
341 goto err;
342 }
343 priv->firm_size = n_array;
344
345 n = -1;
346 while (p < endp) {
347 __u32 type, size;
348 v4l2_std_id id;
349 __u16 int_freq = 0;
350
351 n++;
352 if (n >= n_array) {
353 tuner_err("More firmware images in file than were expected!\n");
354 goto corrupt;
355 }
356
357 /* Checks if there's enough bytes to read */
358 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
359 goto header;
360
361 type = get_unaligned_le32(p);
362 p += sizeof(type);
363
364 id = get_unaligned_le64(p);
365 p += sizeof(id);
366
367 if (type & HAS_IF) {
368 int_freq = get_unaligned_le16(p);
369 p += sizeof(int_freq);
370 if (endp - p < sizeof(size))
371 goto header;
372 }
373
374 size = get_unaligned_le32(p);
375 p += sizeof(size);
376
377 if (!size || size > endp - p) {
378 tuner_err("Firmware type ");
379 dump_firm_type(type);
380 printk(KERN_CONT
381 "(%x), id %llx is corrupted (size=%d, expected %d)\n",
382 type, (unsigned long long)id,
383 (unsigned)(endp - p), size);
384 goto corrupt;
385 }
386
387 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
388 if (priv->firm[n].ptr == NULL) {
389 tuner_err("Not enough memory to load firmware file.\n");
390 rc = -ENOMEM;
391 goto err;
392 }
393 tuner_dbg("Reading firmware type ");
394 if (debug) {
395 dump_firm_type_and_int_freq(type, int_freq);
396 printk(KERN_CONT "(%x), id %llx, size=%d.\n",
397 type, (unsigned long long)id, size);
398 }
399
400 memcpy(priv->firm[n].ptr, p, size);
401 priv->firm[n].type = type;
402 priv->firm[n].id = id;
403 priv->firm[n].size = size;
404 priv->firm[n].int_freq = int_freq;
405
406 p += size;
407 }
408
409 if (n + 1 != priv->firm_size) {
410 tuner_err("Firmware file is incomplete!\n");
411 goto corrupt;
412 }
413
414 goto done;
415
416 header:
417 tuner_err("Firmware header is incomplete!\n");
418 corrupt:
419 rc = -EINVAL;
420 tuner_err("Error: firmware file is corrupted!\n");
421
422 err:
423 tuner_info("Releasing partially loaded firmware file.\n");
424 free_firmware(priv);
425
426 done:
427 if (rc == 0)
428 tuner_dbg("Firmware files loaded.\n");
429 else
430 priv->state = XC2028_NODEV;
431
432 return rc;
433 }
434
435 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
436 v4l2_std_id *id)
437 {
438 struct xc2028_data *priv = fe->tuner_priv;
439 int i, best_i = -1, best_nr_matches = 0;
440 unsigned int type_mask = 0;
441
442 tuner_dbg("%s called, want type=", __func__);
443 if (debug) {
444 dump_firm_type(type);
445 printk(KERN_CONT "(%x), id %016llx.\n",
446 type, (unsigned long long)*id);
447 }
448
449 if (!priv->firm) {
450 tuner_err("Error! firmware not loaded\n");
451 return -EINVAL;
452 }
453
454 if (((type & ~SCODE) == 0) && (*id == 0))
455 *id = V4L2_STD_PAL;
456
457 if (type & BASE)
458 type_mask = BASE_TYPES;
459 else if (type & SCODE) {
460 type &= SCODE_TYPES;
461 type_mask = SCODE_TYPES & ~HAS_IF;
462 } else if (type & DTV_TYPES)
463 type_mask = DTV_TYPES;
464 else if (type & STD_SPECIFIC_TYPES)
465 type_mask = STD_SPECIFIC_TYPES;
466
467 type &= type_mask;
468
469 if (!(type & SCODE))
470 type_mask = ~0;
471
472 /* Seek for exact match */
473 for (i = 0; i < priv->firm_size; i++) {
474 if ((type == (priv->firm[i].type & type_mask)) &&
475 (*id == priv->firm[i].id))
476 goto found;
477 }
478
479 /* Seek for generic video standard match */
480 for (i = 0; i < priv->firm_size; i++) {
481 v4l2_std_id match_mask;
482 int nr_matches;
483
484 if (type != (priv->firm[i].type & type_mask))
485 continue;
486
487 match_mask = *id & priv->firm[i].id;
488 if (!match_mask)
489 continue;
490
491 if ((*id & match_mask) == *id)
492 goto found; /* Supports all the requested standards */
493
494 nr_matches = hweight64(match_mask);
495 if (nr_matches > best_nr_matches) {
496 best_nr_matches = nr_matches;
497 best_i = i;
498 }
499 }
500
501 if (best_nr_matches > 0) {
502 tuner_dbg("Selecting best matching firmware (%d bits) for type=",
503 best_nr_matches);
504 dump_firm_type(type);
505 printk(KERN_CONT
506 "(%x), id %016llx:\n", type, (unsigned long long)*id);
507 i = best_i;
508 goto found;
509 }
510
511 /*FIXME: Would make sense to seek for type "hint" match ? */
512
513 i = -ENOENT;
514 goto ret;
515
516 found:
517 *id = priv->firm[i].id;
518
519 ret:
520 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
521 if (debug) {
522 dump_firm_type(type);
523 printk(KERN_CONT "(%x), id %016llx.\n",
524 type, (unsigned long long)*id);
525 }
526 return i;
527 }
528
529 static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
530 {
531 struct xc2028_data *priv = fe->tuner_priv;
532
533 /* analog side (tuner-core) uses i2c_adap->algo_data.
534 * digital side is not guaranteed to have algo_data defined.
535 *
536 * digital side will always have fe->dvb defined.
537 * analog side (tuner-core) doesn't (yet) define fe->dvb.
538 */
539
540 return (!fe->callback) ? -EINVAL :
541 fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
542 fe->dvb->priv : priv->i2c_props.adap->algo_data,
543 DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
544 }
545
546 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
547 v4l2_std_id *id)
548 {
549 struct xc2028_data *priv = fe->tuner_priv;
550 int pos, rc;
551 unsigned char *p, *endp, buf[MAX_XFER_SIZE];
552
553 if (priv->ctrl.max_len > sizeof(buf))
554 priv->ctrl.max_len = sizeof(buf);
555
556 tuner_dbg("%s called\n", __func__);
557
558 pos = seek_firmware(fe, type, id);
559 if (pos < 0)
560 return pos;
561
562 tuner_info("Loading firmware for type=");
563 dump_firm_type(priv->firm[pos].type);
564 printk(KERN_CONT "(%x), id %016llx.\n",
565 priv->firm[pos].type, (unsigned long long)*id);
566
567 p = priv->firm[pos].ptr;
568 endp = p + priv->firm[pos].size;
569
570 while (p < endp) {
571 __u16 size;
572
573 /* Checks if there's enough bytes to read */
574 if (p + sizeof(size) > endp) {
575 tuner_err("Firmware chunk size is wrong\n");
576 return -EINVAL;
577 }
578
579 size = le16_to_cpu(*(__le16 *) p);
580 p += sizeof(size);
581
582 if (size == 0xffff)
583 return 0;
584
585 if (!size) {
586 /* Special callback command received */
587 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
588 if (rc < 0) {
589 tuner_err("Error at RESET code %d\n",
590 (*p) & 0x7f);
591 return -EINVAL;
592 }
593 continue;
594 }
595 if (size >= 0xff00) {
596 switch (size) {
597 case 0xff00:
598 rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
599 if (rc < 0) {
600 tuner_err("Error at RESET code %d\n",
601 (*p) & 0x7f);
602 return -EINVAL;
603 }
604 break;
605 default:
606 tuner_info("Invalid RESET code %d\n",
607 size & 0x7f);
608 return -EINVAL;
609
610 }
611 continue;
612 }
613
614 /* Checks for a sleep command */
615 if (size & 0x8000) {
616 msleep(size & 0x7fff);
617 continue;
618 }
619
620 if ((size + p > endp)) {
621 tuner_err("missing bytes: need %d, have %d\n",
622 size, (int)(endp - p));
623 return -EINVAL;
624 }
625
626 buf[0] = *p;
627 p++;
628 size--;
629
630 /* Sends message chunks */
631 while (size > 0) {
632 int len = (size < priv->ctrl.max_len - 1) ?
633 size : priv->ctrl.max_len - 1;
634
635 memcpy(buf + 1, p, len);
636
637 rc = i2c_send(priv, buf, len + 1);
638 if (rc < 0) {
639 tuner_err("%d returned from send\n", rc);
640 return -EINVAL;
641 }
642
643 p += len;
644 size -= len;
645 }
646
647 /* silently fail if the frontend doesn't support I2C flush */
648 rc = do_tuner_callback(fe, XC2028_I2C_FLUSH, 0);
649 if ((rc < 0) && (rc != -EINVAL)) {
650 tuner_err("error executing flush: %d\n", rc);
651 return rc;
652 }
653 }
654 return 0;
655 }
656
657 static int load_scode(struct dvb_frontend *fe, unsigned int type,
658 v4l2_std_id *id, __u16 int_freq, int scode)
659 {
660 struct xc2028_data *priv = fe->tuner_priv;
661 int pos, rc;
662 unsigned char *p;
663
664 tuner_dbg("%s called\n", __func__);
665
666 if (!int_freq) {
667 pos = seek_firmware(fe, type, id);
668 if (pos < 0)
669 return pos;
670 } else {
671 for (pos = 0; pos < priv->firm_size; pos++) {
672 if ((priv->firm[pos].int_freq == int_freq) &&
673 (priv->firm[pos].type & HAS_IF))
674 break;
675 }
676 if (pos == priv->firm_size)
677 return -ENOENT;
678 }
679
680 p = priv->firm[pos].ptr;
681
682 if (priv->firm[pos].type & HAS_IF) {
683 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
684 return -EINVAL;
685 p += 12 * scode;
686 } else {
687 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
688 * has a 2-byte size header in the firmware format. */
689 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
690 le16_to_cpu(*(__le16 *)(p + 14 * scode)) != 12)
691 return -EINVAL;
692 p += 14 * scode + 2;
693 }
694
695 tuner_info("Loading SCODE for type=");
696 dump_firm_type_and_int_freq(priv->firm[pos].type,
697 priv->firm[pos].int_freq);
698 printk(KERN_CONT "(%x), id %016llx.\n", priv->firm[pos].type,
699 (unsigned long long)*id);
700
701 if (priv->firm_version < 0x0202)
702 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
703 else
704 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
705 if (rc < 0)
706 return -EIO;
707
708 rc = i2c_send(priv, p, 12);
709 if (rc < 0)
710 return -EIO;
711
712 rc = send_seq(priv, {0x00, 0x8c});
713 if (rc < 0)
714 return -EIO;
715
716 return 0;
717 }
718
719 static int xc2028_sleep(struct dvb_frontend *fe);
720
721 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
722 v4l2_std_id std, __u16 int_freq)
723 {
724 struct xc2028_data *priv = fe->tuner_priv;
725 struct firmware_properties new_fw;
726 int rc, retry_count = 0;
727 u16 version, hwmodel;
728 v4l2_std_id std0;
729
730 tuner_dbg("%s called\n", __func__);
731
732 rc = check_device_status(priv);
733 if (rc < 0)
734 return rc;
735
736 if (priv->ctrl.mts && !(type & FM))
737 type |= MTS;
738
739 retry:
740 new_fw.type = type;
741 new_fw.id = std;
742 new_fw.std_req = std;
743 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
744 new_fw.scode_nr = 0;
745 new_fw.int_freq = int_freq;
746
747 tuner_dbg("checking firmware, user requested type=");
748 if (debug) {
749 dump_firm_type(new_fw.type);
750 printk(KERN_CONT "(%x), id %016llx, ", new_fw.type,
751 (unsigned long long)new_fw.std_req);
752 if (!int_freq) {
753 printk(KERN_CONT "scode_tbl ");
754 dump_firm_type(priv->ctrl.scode_table);
755 printk(KERN_CONT "(%x), ", priv->ctrl.scode_table);
756 } else
757 printk(KERN_CONT "int_freq %d, ", new_fw.int_freq);
758 printk(KERN_CONT "scode_nr %d\n", new_fw.scode_nr);
759 }
760
761 /*
762 * No need to reload base firmware if it matches and if the tuner
763 * is not at sleep mode
764 */
765 if ((priv->state == XC2028_ACTIVE) &&
766 (((BASE | new_fw.type) & BASE_TYPES) ==
767 (priv->cur_fw.type & BASE_TYPES))) {
768 tuner_dbg("BASE firmware not changed.\n");
769 goto skip_base;
770 }
771
772 /* Updating BASE - forget about all currently loaded firmware */
773 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
774
775 /* Reset is needed before loading firmware */
776 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
777 if (rc < 0)
778 goto fail;
779
780 /* BASE firmwares are all std0 */
781 std0 = 0;
782 rc = load_firmware(fe, BASE | new_fw.type, &std0);
783 if (rc < 0) {
784 tuner_err("Error %d while loading base firmware\n",
785 rc);
786 goto fail;
787 }
788
789 /* Load INIT1, if needed */
790 tuner_dbg("Load init1 firmware, if exists\n");
791
792 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
793 if (rc == -ENOENT)
794 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
795 &std0);
796 if (rc < 0 && rc != -ENOENT) {
797 tuner_err("Error %d while loading init1 firmware\n",
798 rc);
799 goto fail;
800 }
801
802 skip_base:
803 /*
804 * No need to reload standard specific firmware if base firmware
805 * was not reloaded and requested video standards have not changed.
806 */
807 if (priv->cur_fw.type == (BASE | new_fw.type) &&
808 priv->cur_fw.std_req == std) {
809 tuner_dbg("Std-specific firmware already loaded.\n");
810 goto skip_std_specific;
811 }
812
813 /* Reloading std-specific firmware forces a SCODE update */
814 priv->cur_fw.scode_table = 0;
815
816 rc = load_firmware(fe, new_fw.type, &new_fw.id);
817 if (rc == -ENOENT)
818 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
819
820 if (rc < 0)
821 goto fail;
822
823 skip_std_specific:
824 if (priv->cur_fw.scode_table == new_fw.scode_table &&
825 priv->cur_fw.scode_nr == new_fw.scode_nr) {
826 tuner_dbg("SCODE firmware already loaded.\n");
827 goto check_device;
828 }
829
830 if (new_fw.type & FM)
831 goto check_device;
832
833 /* Load SCODE firmware, if exists */
834 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
835
836 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
837 new_fw.int_freq, new_fw.scode_nr);
838
839 check_device:
840 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
841 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
842 tuner_err("Unable to read tuner registers.\n");
843 goto fail;
844 }
845
846 tuner_dbg("Device is Xceive %d version %d.%d, firmware version %d.%d\n",
847 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
848 (version & 0xf0) >> 4, version & 0xf);
849
850
851 if (priv->ctrl.read_not_reliable)
852 goto read_not_reliable;
853
854 /* Check firmware version against what we downloaded. */
855 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
856 if (!priv->ctrl.read_not_reliable) {
857 tuner_err("Incorrect readback of firmware version.\n");
858 goto fail;
859 } else {
860 tuner_err("Returned an incorrect version. However, read is not reliable enough. Ignoring it.\n");
861 hwmodel = 3028;
862 }
863 }
864
865 /* Check that the tuner hardware model remains consistent over time. */
866 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
867 priv->hwmodel = hwmodel;
868 priv->hwvers = version & 0xff00;
869 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
870 priv->hwvers != (version & 0xff00)) {
871 tuner_err("Read invalid device hardware information - tuner hung?\n");
872 goto fail;
873 }
874
875 read_not_reliable:
876 priv->cur_fw = new_fw;
877
878 /*
879 * By setting BASE in cur_fw.type only after successfully loading all
880 * firmwares, we can:
881 * 1. Identify that BASE firmware with type=0 has been loaded;
882 * 2. Tell whether BASE firmware was just changed the next time through.
883 */
884 priv->cur_fw.type |= BASE;
885 priv->state = XC2028_ACTIVE;
886
887 return 0;
888
889 fail:
890 free_firmware(priv);
891
892 if (retry_count < 8) {
893 msleep(50);
894 retry_count++;
895 tuner_dbg("Retrying firmware load\n");
896 goto retry;
897 }
898
899 /* Firmware didn't load. Put the device to sleep */
900 xc2028_sleep(fe);
901
902 if (rc == -ENOENT)
903 rc = -EINVAL;
904 return rc;
905 }
906
907 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
908 {
909 struct xc2028_data *priv = fe->tuner_priv;
910 u16 frq_lock, signal = 0;
911 int rc, i;
912
913 tuner_dbg("%s called\n", __func__);
914
915 rc = check_device_status(priv);
916 if (rc < 0)
917 return rc;
918
919 /* If the device is sleeping, no channel is tuned */
920 if (!rc) {
921 *strength = 0;
922 return 0;
923 }
924
925 mutex_lock(&priv->lock);
926
927 /* Sync Lock Indicator */
928 for (i = 0; i < 3; i++) {
929 rc = xc2028_get_reg(priv, XREG_LOCK, &frq_lock);
930 if (rc < 0)
931 goto ret;
932
933 if (frq_lock)
934 break;
935 msleep(6);
936 }
937
938 /* Frequency didn't lock */
939 if (frq_lock == 2)
940 goto ret;
941
942 /* Get SNR of the video signal */
943 rc = xc2028_get_reg(priv, XREG_SNR, &signal);
944 if (rc < 0)
945 goto ret;
946
947 /* Signal level is 3 bits only */
948
949 signal = ((1 << 12) - 1) | ((signal & 0x07) << 12);
950
951 ret:
952 mutex_unlock(&priv->lock);
953
954 *strength = signal;
955
956 tuner_dbg("signal strength is %d\n", signal);
957
958 return rc;
959 }
960
961 static int xc2028_get_afc(struct dvb_frontend *fe, s32 *afc)
962 {
963 struct xc2028_data *priv = fe->tuner_priv;
964 int i, rc;
965 u16 frq_lock = 0;
966 s16 afc_reg = 0;
967
968 rc = check_device_status(priv);
969 if (rc < 0)
970 return rc;
971
972 /* If the device is sleeping, no channel is tuned */
973 if (!rc) {
974 *afc = 0;
975 return 0;
976 }
977
978 mutex_lock(&priv->lock);
979
980 /* Sync Lock Indicator */
981 for (i = 0; i < 3; i++) {
982 rc = xc2028_get_reg(priv, XREG_LOCK, &frq_lock);
983 if (rc < 0)
984 goto ret;
985
986 if (frq_lock)
987 break;
988 msleep(6);
989 }
990
991 /* Frequency didn't lock */
992 if (frq_lock == 2)
993 goto ret;
994
995 /* Get AFC */
996 rc = xc2028_get_reg(priv, XREG_FREQ_ERROR, &afc_reg);
997 if (rc < 0)
998 goto ret;
999
1000 *afc = afc_reg * 15625; /* Hz */
1001
1002 tuner_dbg("AFC is %d Hz\n", *afc);
1003
1004 ret:
1005 mutex_unlock(&priv->lock);
1006
1007 return rc;
1008 }
1009
1010 #define DIV 15625
1011
1012 static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
1013 enum v4l2_tuner_type new_type,
1014 unsigned int type,
1015 v4l2_std_id std,
1016 u16 int_freq)
1017 {
1018 struct xc2028_data *priv = fe->tuner_priv;
1019 int rc = -EINVAL;
1020 unsigned char buf[4];
1021 u32 div, offset = 0;
1022
1023 tuner_dbg("%s called\n", __func__);
1024
1025 mutex_lock(&priv->lock);
1026
1027 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
1028
1029 if (check_firmware(fe, type, std, int_freq) < 0)
1030 goto ret;
1031
1032 /* On some cases xc2028 can disable video output, if
1033 * very weak signals are received. By sending a soft
1034 * reset, this is re-enabled. So, it is better to always
1035 * send a soft reset before changing channels, to be sure
1036 * that xc2028 will be in a safe state.
1037 * Maybe this might also be needed for DTV.
1038 */
1039 switch (new_type) {
1040 case V4L2_TUNER_ANALOG_TV:
1041 rc = send_seq(priv, {0x00, 0x00});
1042
1043 /* Analog mode requires offset = 0 */
1044 break;
1045 case V4L2_TUNER_RADIO:
1046 /* Radio mode requires offset = 0 */
1047 break;
1048 case V4L2_TUNER_DIGITAL_TV:
1049 /*
1050 * Digital modes require an offset to adjust to the
1051 * proper frequency. The offset depends on what
1052 * firmware version is used.
1053 */
1054
1055 /*
1056 * Adjust to the center frequency. This is calculated by the
1057 * formula: offset = 1.25MHz - BW/2
1058 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
1059 * further adjustment to get the frequency center on VHF
1060 */
1061
1062 /*
1063 * The firmware DTV78 used to work fine in UHF band (8 MHz
1064 * bandwidth) but not at all in VHF band (7 MHz bandwidth).
1065 * The real problem was connected to the formula used to
1066 * calculate the center frequency offset in VHF band.
1067 * In fact, removing the 500KHz adjustment fixed the problem.
1068 * This is coherent to what was implemented for the DTV7
1069 * firmware.
1070 * In the end, now the center frequency is the same for all 3
1071 * firmwares (DTV7, DTV8, DTV78) and doesn't depend on channel
1072 * bandwidth.
1073 */
1074
1075 if (priv->cur_fw.type & DTV6)
1076 offset = 1750000;
1077 else /* DTV7 or DTV8 or DTV78 */
1078 offset = 2750000;
1079
1080 /*
1081 * xc3028 additional "magic"
1082 * Depending on the firmware version, it needs some adjustments
1083 * to properly centralize the frequency. This seems to be
1084 * needed to compensate the SCODE table adjustments made by
1085 * newer firmwares
1086 */
1087
1088 /*
1089 * The proper adjustment would be to do it at s-code table.
1090 * However, this didn't work, as reported by
1091 * Robert Lowery <rglowery@exemail.com.au>
1092 */
1093
1094 #if 0
1095 /*
1096 * Still need tests for XC3028L (firmware 3.2 or upper)
1097 * So, for now, let's just comment the per-firmware
1098 * version of this change. Reports with xc3028l working
1099 * with and without the lines below are welcome
1100 */
1101
1102 if (priv->firm_version < 0x0302) {
1103 if (priv->cur_fw.type & DTV7)
1104 offset += 500000;
1105 } else {
1106 if (priv->cur_fw.type & DTV7)
1107 offset -= 300000;
1108 else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
1109 offset += 200000;
1110 }
1111 #endif
1112 break;
1113 default:
1114 tuner_err("Unsupported tuner type %d.\n", new_type);
1115 break;
1116 }
1117
1118 div = (freq - offset + DIV / 2) / DIV;
1119
1120 /* CMD= Set frequency */
1121 if (priv->firm_version < 0x0202)
1122 rc = send_seq(priv, {0x00, XREG_RF_FREQ, 0x00, 0x00});
1123 else
1124 rc = send_seq(priv, {0x80, XREG_RF_FREQ, 0x00, 0x00});
1125 if (rc < 0)
1126 goto ret;
1127
1128 /* Return code shouldn't be checked.
1129 The reset CLK is needed only with tm6000.
1130 Driver should work fine even if this fails.
1131 */
1132 if (priv->ctrl.msleep)
1133 msleep(priv->ctrl.msleep);
1134 do_tuner_callback(fe, XC2028_RESET_CLK, 1);
1135
1136 msleep(10);
1137
1138 buf[0] = 0xff & (div >> 24);
1139 buf[1] = 0xff & (div >> 16);
1140 buf[2] = 0xff & (div >> 8);
1141 buf[3] = 0xff & (div);
1142
1143 rc = i2c_send(priv, buf, sizeof(buf));
1144 if (rc < 0)
1145 goto ret;
1146 msleep(100);
1147
1148 priv->frequency = freq;
1149
1150 tuner_dbg("divisor= %*ph (freq=%d.%03d)\n", 4, buf,
1151 freq / 1000000, (freq % 1000000) / 1000);
1152
1153 rc = 0;
1154
1155 ret:
1156 mutex_unlock(&priv->lock);
1157
1158 return rc;
1159 }
1160
1161 static int xc2028_set_analog_freq(struct dvb_frontend *fe,
1162 struct analog_parameters *p)
1163 {
1164 struct xc2028_data *priv = fe->tuner_priv;
1165 unsigned int type=0;
1166
1167 tuner_dbg("%s called\n", __func__);
1168
1169 if (p->mode == V4L2_TUNER_RADIO) {
1170 type |= FM;
1171 if (priv->ctrl.input1)
1172 type |= INPUT1;
1173 return generic_set_freq(fe, (625l * p->frequency) / 10,
1174 V4L2_TUNER_RADIO, type, 0, 0);
1175 }
1176
1177 /* if std is not defined, choose one */
1178 if (!p->std)
1179 p->std = V4L2_STD_MN;
1180
1181 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
1182 if (!(p->std & V4L2_STD_MN))
1183 type |= F8MHZ;
1184
1185 /* Add audio hack to std mask */
1186 p->std |= parse_audio_std_option();
1187
1188 return generic_set_freq(fe, 62500l * p->frequency,
1189 V4L2_TUNER_ANALOG_TV, type, p->std, 0);
1190 }
1191
1192 static int xc2028_set_params(struct dvb_frontend *fe)
1193 {
1194 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1195 u32 delsys = c->delivery_system;
1196 u32 bw = c->bandwidth_hz;
1197 struct xc2028_data *priv = fe->tuner_priv;
1198 int rc;
1199 unsigned int type = 0;
1200 u16 demod = 0;
1201
1202 tuner_dbg("%s called\n", __func__);
1203
1204 rc = check_device_status(priv);
1205 if (rc < 0)
1206 return rc;
1207
1208 switch (delsys) {
1209 case SYS_DVBT:
1210 case SYS_DVBT2:
1211 /*
1212 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1213 * Both seem to require QAM firmware for OFDM decoding
1214 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1215 */
1216 if (bw <= 6000000)
1217 type |= QAM;
1218
1219 switch (priv->ctrl.type) {
1220 case XC2028_D2633:
1221 type |= D2633;
1222 break;
1223 case XC2028_D2620:
1224 type |= D2620;
1225 break;
1226 case XC2028_AUTO:
1227 default:
1228 /* Zarlink seems to need D2633 */
1229 if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
1230 type |= D2633;
1231 else
1232 type |= D2620;
1233 }
1234 break;
1235 case SYS_ATSC:
1236 /* The only ATSC firmware (at least on v2.7) is D2633 */
1237 type |= ATSC | D2633;
1238 break;
1239 /* DVB-S and pure QAM (FE_QAM) are not supported */
1240 default:
1241 return -EINVAL;
1242 }
1243
1244 if (bw <= 6000000) {
1245 type |= DTV6;
1246 priv->ctrl.vhfbw7 = 0;
1247 priv->ctrl.uhfbw8 = 0;
1248 } else if (bw <= 7000000) {
1249 if (c->frequency < 470000000)
1250 priv->ctrl.vhfbw7 = 1;
1251 else
1252 priv->ctrl.uhfbw8 = 0;
1253 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1254 type |= F8MHZ;
1255 } else {
1256 if (c->frequency < 470000000)
1257 priv->ctrl.vhfbw7 = 0;
1258 else
1259 priv->ctrl.uhfbw8 = 1;
1260 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1261 type |= F8MHZ;
1262 }
1263
1264 /* All S-code tables need a 200kHz shift */
1265 if (priv->ctrl.demod) {
1266 demod = priv->ctrl.demod;
1267
1268 /*
1269 * Newer firmwares require a 200 kHz offset only for ATSC
1270 */
1271 if (type == ATSC || priv->firm_version < 0x0302)
1272 demod += 200;
1273 /*
1274 * The DTV7 S-code table needs a 700 kHz shift.
1275 *
1276 * DTV7 is only used in Australia. Germany or Italy may also
1277 * use this firmware after initialization, but a tune to a UHF
1278 * channel should then cause DTV78 to be used.
1279 *
1280 * Unfortunately, on real-field tests, the s-code offset
1281 * didn't work as expected, as reported by
1282 * Robert Lowery <rglowery@exemail.com.au>
1283 */
1284 }
1285
1286 return generic_set_freq(fe, c->frequency,
1287 V4L2_TUNER_DIGITAL_TV, type, 0, demod);
1288 }
1289
1290 static int xc2028_sleep(struct dvb_frontend *fe)
1291 {
1292 struct xc2028_data *priv = fe->tuner_priv;
1293 int rc;
1294
1295 rc = check_device_status(priv);
1296 if (rc < 0)
1297 return rc;
1298
1299 /* Device is already in sleep mode */
1300 if (!rc)
1301 return 0;
1302
1303 /* Avoid firmware reload on slow devices or if PM disabled */
1304 if (no_poweroff || priv->ctrl.disable_power_mgmt)
1305 return 0;
1306
1307 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
1308 if (debug > 1) {
1309 tuner_dbg("Printing sleep stack trace:\n");
1310 dump_stack();
1311 }
1312
1313 mutex_lock(&priv->lock);
1314
1315 if (priv->firm_version < 0x0202)
1316 rc = send_seq(priv, {0x00, XREG_POWER_DOWN, 0x00, 0x00});
1317 else
1318 rc = send_seq(priv, {0x80, XREG_POWER_DOWN, 0x00, 0x00});
1319
1320 if (rc >= 0)
1321 priv->state = XC2028_SLEEP;
1322
1323 mutex_unlock(&priv->lock);
1324
1325 return rc;
1326 }
1327
1328 static void xc2028_dvb_release(struct dvb_frontend *fe)
1329 {
1330 struct xc2028_data *priv = fe->tuner_priv;
1331
1332 tuner_dbg("%s called\n", __func__);
1333
1334 mutex_lock(&xc2028_list_mutex);
1335
1336 /* only perform final cleanup if this is the last instance */
1337 if (hybrid_tuner_report_instance_count(priv) == 1)
1338 free_firmware(priv);
1339
1340 if (priv)
1341 hybrid_tuner_release_state(priv);
1342
1343 mutex_unlock(&xc2028_list_mutex);
1344
1345 fe->tuner_priv = NULL;
1346 }
1347
1348 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1349 {
1350 struct xc2028_data *priv = fe->tuner_priv;
1351 int rc;
1352
1353 tuner_dbg("%s called\n", __func__);
1354
1355 rc = check_device_status(priv);
1356 if (rc < 0)
1357 return rc;
1358
1359 *frequency = priv->frequency;
1360
1361 return 0;
1362 }
1363
1364 static void load_firmware_cb(const struct firmware *fw,
1365 void *context)
1366 {
1367 struct dvb_frontend *fe = context;
1368 struct xc2028_data *priv = fe->tuner_priv;
1369 int rc;
1370
1371 tuner_dbg("request_firmware_nowait(): %s\n", fw ? "OK" : "error");
1372 if (!fw) {
1373 tuner_err("Could not load firmware %s.\n", priv->fname);
1374 priv->state = XC2028_NODEV;
1375 return;
1376 }
1377
1378 rc = load_all_firmwares(fe, fw);
1379
1380 release_firmware(fw);
1381
1382 if (rc < 0)
1383 return;
1384 priv->state = XC2028_ACTIVE;
1385 }
1386
1387 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1388 {
1389 struct xc2028_data *priv = fe->tuner_priv;
1390 struct xc2028_ctrl *p = priv_cfg;
1391 int rc = 0;
1392
1393 tuner_dbg("%s called\n", __func__);
1394
1395 mutex_lock(&priv->lock);
1396
1397 /*
1398 * Copy the config data.
1399 */
1400 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1401
1402 /*
1403 * If firmware name changed, frees firmware. As free_firmware will
1404 * reset the status to NO_FIRMWARE, this forces a new request_firmware
1405 */
1406 if (!firmware_name[0] && p->fname &&
1407 priv->fname && strcmp(p->fname, priv->fname))
1408 free_firmware(priv);
1409
1410 if (priv->ctrl.max_len < 9)
1411 priv->ctrl.max_len = 13;
1412
1413 if (priv->state == XC2028_NO_FIRMWARE) {
1414 if (!firmware_name[0])
1415 priv->fname = kstrdup(p->fname, GFP_KERNEL);
1416 else
1417 priv->fname = firmware_name;
1418
1419 if (!priv->fname) {
1420 rc = -ENOMEM;
1421 goto unlock;
1422 }
1423
1424 rc = request_firmware_nowait(THIS_MODULE, 1,
1425 priv->fname,
1426 priv->i2c_props.adap->dev.parent,
1427 GFP_KERNEL,
1428 fe, load_firmware_cb);
1429 if (rc < 0) {
1430 tuner_err("Failed to request firmware %s\n",
1431 priv->fname);
1432 priv->state = XC2028_NODEV;
1433 } else
1434 priv->state = XC2028_WAITING_FIRMWARE;
1435 }
1436 unlock:
1437 mutex_unlock(&priv->lock);
1438
1439 return rc;
1440 }
1441
1442 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1443 .info = {
1444 .name = "Xceive XC3028",
1445 .frequency_min = 42000000,
1446 .frequency_max = 864000000,
1447 .frequency_step = 50000,
1448 },
1449
1450 .set_config = xc2028_set_config,
1451 .set_analog_params = xc2028_set_analog_freq,
1452 .release = xc2028_dvb_release,
1453 .get_frequency = xc2028_get_frequency,
1454 .get_rf_strength = xc2028_signal,
1455 .get_afc = xc2028_get_afc,
1456 .set_params = xc2028_set_params,
1457 .sleep = xc2028_sleep,
1458 };
1459
1460 struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1461 struct xc2028_config *cfg)
1462 {
1463 struct xc2028_data *priv;
1464 int instance;
1465
1466 if (debug)
1467 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
1468
1469 if (NULL == cfg)
1470 return NULL;
1471
1472 if (!fe) {
1473 printk(KERN_ERR "xc2028: No frontend!\n");
1474 return NULL;
1475 }
1476
1477 mutex_lock(&xc2028_list_mutex);
1478
1479 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1480 hybrid_tuner_instance_list,
1481 cfg->i2c_adap, cfg->i2c_addr,
1482 "xc2028");
1483 switch (instance) {
1484 case 0:
1485 /* memory allocation failure */
1486 goto fail;
1487 case 1:
1488 /* new tuner instance */
1489 priv->ctrl.max_len = 13;
1490
1491 mutex_init(&priv->lock);
1492
1493 fe->tuner_priv = priv;
1494 break;
1495 case 2:
1496 /* existing tuner instance */
1497 fe->tuner_priv = priv;
1498 break;
1499 }
1500
1501 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1502 sizeof(xc2028_dvb_tuner_ops));
1503
1504 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1505
1506 if (cfg->ctrl)
1507 xc2028_set_config(fe, cfg->ctrl);
1508
1509 mutex_unlock(&xc2028_list_mutex);
1510
1511 return fe;
1512 fail:
1513 mutex_unlock(&xc2028_list_mutex);
1514
1515 xc2028_dvb_release(fe);
1516 return NULL;
1517 }
1518
1519 EXPORT_SYMBOL(xc2028_attach);
1520
1521 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1522 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1523 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1524 MODULE_LICENSE("GPL");
1525 MODULE_FIRMWARE(XC2028_DEFAULT_FIRMWARE);
1526 MODULE_FIRMWARE(XC3028L_DEFAULT_FIRMWARE);