Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / media / common / tuners / tuner-xc2028.c
blobb5ee3ebfcfca03e2f9fd5973cd5d72b52d1c5f53
1 /* tuner-xc2028
3 * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org)
5 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
8 * This code is placed under the terms of the GNU General Public License v2
9 */
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"
24 #include <linux/dvb/frontend.h>
25 #include "dvb_frontend.h"
27 /* Registers (Write-only) */
28 #define XREG_INIT 0x00
29 #define XREG_RF_FREQ 0x02
30 #define XREG_POWER_DOWN 0x08
32 /* Registers (Read-only) */
33 #define XREG_FREQ_ERROR 0x01
34 #define XREG_LOCK 0x02
35 #define XREG_VERSION 0x04
36 #define XREG_PRODUCT_ID 0x08
37 #define XREG_HSYNC_FREQ 0x10
38 #define XREG_FRAME_LINES 0x20
39 #define XREG_SNR 0x40
41 #define XREG_ADC_ENV 0x0100
43 static int debug;
44 module_param(debug, int, 0644);
45 MODULE_PARM_DESC(debug, "enable verbose debug messages");
47 static int no_poweroff;
48 module_param(no_poweroff, int, 0644);
49 MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
50 "1 keep device energized and with tuner ready all the times.\n"
51 " Faster, but consumes more power and keeps the device hotter\n");
53 static char audio_std[8];
54 module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
55 MODULE_PARM_DESC(audio_std,
56 "Audio standard. XC3028 audio decoder explicitly "
57 "needs to know what audio\n"
58 "standard is needed for some video standards with audio A2 or NICAM.\n"
59 "The valid values are:\n"
60 "A2\n"
61 "A2/A\n"
62 "A2/B\n"
63 "NICAM\n"
64 "NICAM/A\n"
65 "NICAM/B\n");
67 static char firmware_name[30];
68 module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
69 MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
70 "default firmware name\n");
72 static LIST_HEAD(hybrid_tuner_instance_list);
73 static DEFINE_MUTEX(xc2028_list_mutex);
75 /* struct for storing firmware table */
76 struct firmware_description {
77 unsigned int type;
78 v4l2_std_id id;
79 __u16 int_freq;
80 unsigned char *ptr;
81 unsigned int size;
84 struct firmware_properties {
85 unsigned int type;
86 v4l2_std_id id;
87 v4l2_std_id std_req;
88 __u16 int_freq;
89 unsigned int scode_table;
90 int scode_nr;
93 struct xc2028_data {
94 struct list_head hybrid_tuner_instance_list;
95 struct tuner_i2c_props i2c_props;
96 __u32 frequency;
98 struct firmware_description *firm;
99 int firm_size;
100 __u16 firm_version;
102 __u16 hwmodel;
103 __u16 hwvers;
105 struct xc2028_ctrl ctrl;
107 struct firmware_properties cur_fw;
109 struct mutex lock;
112 #define i2c_send(priv, buf, size) ({ \
113 int _rc; \
114 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
115 if (size != _rc) \
116 tuner_info("i2c output error: rc = %d (should be %d)\n",\
117 _rc, (int)size); \
118 if (priv->ctrl.msleep) \
119 msleep(priv->ctrl.msleep); \
120 _rc; \
123 #define i2c_rcv(priv, buf, size) ({ \
124 int _rc; \
125 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
126 if (size != _rc) \
127 tuner_err("i2c input error: rc = %d (should be %d)\n", \
128 _rc, (int)size); \
129 _rc; \
132 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
133 int _rc; \
134 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
135 ibuf, isize); \
136 if (isize != _rc) \
137 tuner_err("i2c input error: rc = %d (should be %d)\n", \
138 _rc, (int)isize); \
139 if (priv->ctrl.msleep) \
140 msleep(priv->ctrl.msleep); \
141 _rc; \
144 #define send_seq(priv, data...) ({ \
145 static u8 _val[] = data; \
146 int _rc; \
147 if (sizeof(_val) != \
148 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
149 _val, sizeof(_val)))) { \
150 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
151 } else if (priv->ctrl.msleep) \
152 msleep(priv->ctrl.msleep); \
153 _rc; \
156 static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
158 unsigned char buf[2];
159 unsigned char ibuf[2];
161 tuner_dbg("%s %04x called\n", __func__, reg);
163 buf[0] = reg >> 8;
164 buf[1] = (unsigned char) reg;
166 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
167 return -EIO;
169 *val = (ibuf[1]) | (ibuf[0] << 8);
170 return 0;
173 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
174 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
176 if (type & BASE)
177 printk("BASE ");
178 if (type & INIT1)
179 printk("INIT1 ");
180 if (type & F8MHZ)
181 printk("F8MHZ ");
182 if (type & MTS)
183 printk("MTS ");
184 if (type & D2620)
185 printk("D2620 ");
186 if (type & D2633)
187 printk("D2633 ");
188 if (type & DTV6)
189 printk("DTV6 ");
190 if (type & QAM)
191 printk("QAM ");
192 if (type & DTV7)
193 printk("DTV7 ");
194 if (type & DTV78)
195 printk("DTV78 ");
196 if (type & DTV8)
197 printk("DTV8 ");
198 if (type & FM)
199 printk("FM ");
200 if (type & INPUT1)
201 printk("INPUT1 ");
202 if (type & LCD)
203 printk("LCD ");
204 if (type & NOGD)
205 printk("NOGD ");
206 if (type & MONO)
207 printk("MONO ");
208 if (type & ATSC)
209 printk("ATSC ");
210 if (type & IF)
211 printk("IF ");
212 if (type & LG60)
213 printk("LG60 ");
214 if (type & ATI638)
215 printk("ATI638 ");
216 if (type & OREN538)
217 printk("OREN538 ");
218 if (type & OREN36)
219 printk("OREN36 ");
220 if (type & TOYOTA388)
221 printk("TOYOTA388 ");
222 if (type & TOYOTA794)
223 printk("TOYOTA794 ");
224 if (type & DIBCOM52)
225 printk("DIBCOM52 ");
226 if (type & ZARLINK456)
227 printk("ZARLINK456 ");
228 if (type & CHINA)
229 printk("CHINA ");
230 if (type & F6MHZ)
231 printk("F6MHZ ");
232 if (type & INPUT2)
233 printk("INPUT2 ");
234 if (type & SCODE)
235 printk("SCODE ");
236 if (type & HAS_IF)
237 printk("HAS_IF_%d ", int_freq);
240 static v4l2_std_id parse_audio_std_option(void)
242 if (strcasecmp(audio_std, "A2") == 0)
243 return V4L2_STD_A2;
244 if (strcasecmp(audio_std, "A2/A") == 0)
245 return V4L2_STD_A2_A;
246 if (strcasecmp(audio_std, "A2/B") == 0)
247 return V4L2_STD_A2_B;
248 if (strcasecmp(audio_std, "NICAM") == 0)
249 return V4L2_STD_NICAM;
250 if (strcasecmp(audio_std, "NICAM/A") == 0)
251 return V4L2_STD_NICAM_A;
252 if (strcasecmp(audio_std, "NICAM/B") == 0)
253 return V4L2_STD_NICAM_B;
255 return 0;
258 static void free_firmware(struct xc2028_data *priv)
260 int i;
261 tuner_dbg("%s called\n", __func__);
263 if (!priv->firm)
264 return;
266 for (i = 0; i < priv->firm_size; i++)
267 kfree(priv->firm[i].ptr);
269 kfree(priv->firm);
271 priv->firm = NULL;
272 priv->firm_size = 0;
274 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
277 static int load_all_firmwares(struct dvb_frontend *fe)
279 struct xc2028_data *priv = fe->tuner_priv;
280 const struct firmware *fw = NULL;
281 const unsigned char *p, *endp;
282 int rc = 0;
283 int n, n_array;
284 char name[33];
285 char *fname;
287 tuner_dbg("%s called\n", __func__);
289 if (!firmware_name[0])
290 fname = priv->ctrl.fname;
291 else
292 fname = firmware_name;
294 tuner_dbg("Reading firmware %s\n", fname);
295 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
296 if (rc < 0) {
297 if (rc == -ENOENT)
298 tuner_err("Error: firmware %s not found.\n",
299 fname);
300 else
301 tuner_err("Error %d while requesting firmware %s \n",
302 rc, fname);
304 return rc;
306 p = fw->data;
307 endp = p + fw->size;
309 if (fw->size < sizeof(name) - 1 + 2 + 2) {
310 tuner_err("Error: firmware file %s has invalid size!\n",
311 fname);
312 goto corrupt;
315 memcpy(name, p, sizeof(name) - 1);
316 name[sizeof(name) - 1] = 0;
317 p += sizeof(name) - 1;
319 priv->firm_version = get_unaligned_le16(p);
320 p += 2;
322 n_array = get_unaligned_le16(p);
323 p += 2;
325 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
326 n_array, fname, name,
327 priv->firm_version >> 8, priv->firm_version & 0xff);
329 priv->firm = kcalloc(n_array, sizeof(*priv->firm), GFP_KERNEL);
330 if (priv->firm == NULL) {
331 tuner_err("Not enough memory to load firmware file.\n");
332 rc = -ENOMEM;
333 goto err;
335 priv->firm_size = n_array;
337 n = -1;
338 while (p < endp) {
339 __u32 type, size;
340 v4l2_std_id id;
341 __u16 int_freq = 0;
343 n++;
344 if (n >= n_array) {
345 tuner_err("More firmware images in file than "
346 "were expected!\n");
347 goto corrupt;
350 /* Checks if there's enough bytes to read */
351 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
352 goto header;
354 type = get_unaligned_le32(p);
355 p += sizeof(type);
357 id = get_unaligned_le64(p);
358 p += sizeof(id);
360 if (type & HAS_IF) {
361 int_freq = get_unaligned_le16(p);
362 p += sizeof(int_freq);
363 if (endp - p < sizeof(size))
364 goto header;
367 size = get_unaligned_le32(p);
368 p += sizeof(size);
370 if (!size || size > endp - p) {
371 tuner_err("Firmware type ");
372 dump_firm_type(type);
373 printk("(%x), id %llx is corrupted "
374 "(size=%d, expected %d)\n",
375 type, (unsigned long long)id,
376 (unsigned)(endp - p), size);
377 goto corrupt;
380 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
381 if (priv->firm[n].ptr == NULL) {
382 tuner_err("Not enough memory to load firmware file.\n");
383 rc = -ENOMEM;
384 goto err;
386 tuner_dbg("Reading firmware type ");
387 if (debug) {
388 dump_firm_type_and_int_freq(type, int_freq);
389 printk("(%x), id %llx, size=%d.\n",
390 type, (unsigned long long)id, size);
393 memcpy(priv->firm[n].ptr, p, size);
394 priv->firm[n].type = type;
395 priv->firm[n].id = id;
396 priv->firm[n].size = size;
397 priv->firm[n].int_freq = int_freq;
399 p += size;
402 if (n + 1 != priv->firm_size) {
403 tuner_err("Firmware file is incomplete!\n");
404 goto corrupt;
407 goto done;
409 header:
410 tuner_err("Firmware header is incomplete!\n");
411 corrupt:
412 rc = -EINVAL;
413 tuner_err("Error: firmware file is corrupted!\n");
415 err:
416 tuner_info("Releasing partially loaded firmware file.\n");
417 free_firmware(priv);
419 done:
420 release_firmware(fw);
421 if (rc == 0)
422 tuner_dbg("Firmware files loaded.\n");
424 return rc;
427 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
428 v4l2_std_id *id)
430 struct xc2028_data *priv = fe->tuner_priv;
431 int i, best_i = -1, best_nr_matches = 0;
432 unsigned int type_mask = 0;
434 tuner_dbg("%s called, want type=", __func__);
435 if (debug) {
436 dump_firm_type(type);
437 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
440 if (!priv->firm) {
441 tuner_err("Error! firmware not loaded\n");
442 return -EINVAL;
445 if (((type & ~SCODE) == 0) && (*id == 0))
446 *id = V4L2_STD_PAL;
448 if (type & BASE)
449 type_mask = BASE_TYPES;
450 else if (type & SCODE) {
451 type &= SCODE_TYPES;
452 type_mask = SCODE_TYPES & ~HAS_IF;
453 } else if (type & DTV_TYPES)
454 type_mask = DTV_TYPES;
455 else if (type & STD_SPECIFIC_TYPES)
456 type_mask = STD_SPECIFIC_TYPES;
458 type &= type_mask;
460 if (!(type & SCODE))
461 type_mask = ~0;
463 /* Seek for exact match */
464 for (i = 0; i < priv->firm_size; i++) {
465 if ((type == (priv->firm[i].type & type_mask)) &&
466 (*id == priv->firm[i].id))
467 goto found;
470 /* Seek for generic video standard match */
471 for (i = 0; i < priv->firm_size; i++) {
472 v4l2_std_id match_mask;
473 int nr_matches;
475 if (type != (priv->firm[i].type & type_mask))
476 continue;
478 match_mask = *id & priv->firm[i].id;
479 if (!match_mask)
480 continue;
482 if ((*id & match_mask) == *id)
483 goto found; /* Supports all the requested standards */
485 nr_matches = hweight64(match_mask);
486 if (nr_matches > best_nr_matches) {
487 best_nr_matches = nr_matches;
488 best_i = i;
492 if (best_nr_matches > 0) {
493 tuner_dbg("Selecting best matching firmware (%d bits) for "
494 "type=", best_nr_matches);
495 dump_firm_type(type);
496 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
497 i = best_i;
498 goto found;
501 /*FIXME: Would make sense to seek for type "hint" match ? */
503 i = -ENOENT;
504 goto ret;
506 found:
507 *id = priv->firm[i].id;
509 ret:
510 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
511 if (debug) {
512 dump_firm_type(type);
513 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
515 return i;
518 static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
520 struct xc2028_data *priv = fe->tuner_priv;
522 /* analog side (tuner-core) uses i2c_adap->algo_data.
523 * digital side is not guaranteed to have algo_data defined.
525 * digital side will always have fe->dvb defined.
526 * analog side (tuner-core) doesn't (yet) define fe->dvb.
529 return (!fe->callback) ? -EINVAL :
530 fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
531 fe->dvb->priv : priv->i2c_props.adap->algo_data,
532 DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
535 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
536 v4l2_std_id *id)
538 struct xc2028_data *priv = fe->tuner_priv;
539 int pos, rc;
540 unsigned char *p, *endp, buf[priv->ctrl.max_len];
542 tuner_dbg("%s called\n", __func__);
544 pos = seek_firmware(fe, type, id);
545 if (pos < 0)
546 return pos;
548 tuner_info("Loading firmware for type=");
549 dump_firm_type(priv->firm[pos].type);
550 printk("(%x), id %016llx.\n", priv->firm[pos].type,
551 (unsigned long long)*id);
553 p = priv->firm[pos].ptr;
554 endp = p + priv->firm[pos].size;
556 while (p < endp) {
557 __u16 size;
559 /* Checks if there's enough bytes to read */
560 if (p + sizeof(size) > endp) {
561 tuner_err("Firmware chunk size is wrong\n");
562 return -EINVAL;
565 size = le16_to_cpu(*(__u16 *) p);
566 p += sizeof(size);
568 if (size == 0xffff)
569 return 0;
571 if (!size) {
572 /* Special callback command received */
573 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
574 if (rc < 0) {
575 tuner_err("Error at RESET code %d\n",
576 (*p) & 0x7f);
577 return -EINVAL;
579 continue;
581 if (size >= 0xff00) {
582 switch (size) {
583 case 0xff00:
584 rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
585 if (rc < 0) {
586 tuner_err("Error at RESET code %d\n",
587 (*p) & 0x7f);
588 return -EINVAL;
590 break;
591 default:
592 tuner_info("Invalid RESET code %d\n",
593 size & 0x7f);
594 return -EINVAL;
597 continue;
600 /* Checks for a sleep command */
601 if (size & 0x8000) {
602 msleep(size & 0x7fff);
603 continue;
606 if ((size + p > endp)) {
607 tuner_err("missing bytes: need %d, have %d\n",
608 size, (int)(endp - p));
609 return -EINVAL;
612 buf[0] = *p;
613 p++;
614 size--;
616 /* Sends message chunks */
617 while (size > 0) {
618 int len = (size < priv->ctrl.max_len - 1) ?
619 size : priv->ctrl.max_len - 1;
621 memcpy(buf + 1, p, len);
623 rc = i2c_send(priv, buf, len + 1);
624 if (rc < 0) {
625 tuner_err("%d returned from send\n", rc);
626 return -EINVAL;
629 p += len;
630 size -= len;
633 /* silently fail if the frontend doesn't support I2C flush */
634 rc = do_tuner_callback(fe, XC2028_I2C_FLUSH, 0);
635 if ((rc < 0) && (rc != -EINVAL)) {
636 tuner_err("error executing flush: %d\n", rc);
637 return rc;
640 return 0;
643 static int load_scode(struct dvb_frontend *fe, unsigned int type,
644 v4l2_std_id *id, __u16 int_freq, int scode)
646 struct xc2028_data *priv = fe->tuner_priv;
647 int pos, rc;
648 unsigned char *p;
650 tuner_dbg("%s called\n", __func__);
652 if (!int_freq) {
653 pos = seek_firmware(fe, type, id);
654 if (pos < 0)
655 return pos;
656 } else {
657 for (pos = 0; pos < priv->firm_size; pos++) {
658 if ((priv->firm[pos].int_freq == int_freq) &&
659 (priv->firm[pos].type & HAS_IF))
660 break;
662 if (pos == priv->firm_size)
663 return -ENOENT;
666 p = priv->firm[pos].ptr;
668 if (priv->firm[pos].type & HAS_IF) {
669 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
670 return -EINVAL;
671 p += 12 * scode;
672 } else {
673 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
674 * has a 2-byte size header in the firmware format. */
675 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
676 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
677 return -EINVAL;
678 p += 14 * scode + 2;
681 tuner_info("Loading SCODE for type=");
682 dump_firm_type_and_int_freq(priv->firm[pos].type,
683 priv->firm[pos].int_freq);
684 printk("(%x), id %016llx.\n", priv->firm[pos].type,
685 (unsigned long long)*id);
687 if (priv->firm_version < 0x0202)
688 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
689 else
690 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
691 if (rc < 0)
692 return -EIO;
694 rc = i2c_send(priv, p, 12);
695 if (rc < 0)
696 return -EIO;
698 rc = send_seq(priv, {0x00, 0x8c});
699 if (rc < 0)
700 return -EIO;
702 return 0;
705 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
706 v4l2_std_id std, __u16 int_freq)
708 struct xc2028_data *priv = fe->tuner_priv;
709 struct firmware_properties new_fw;
710 int rc = 0, retry_count = 0;
711 u16 version, hwmodel;
712 v4l2_std_id std0;
714 tuner_dbg("%s called\n", __func__);
716 if (!priv->firm) {
717 if (!priv->ctrl.fname) {
718 tuner_info("xc2028/3028 firmware name not set!\n");
719 return -EINVAL;
722 rc = load_all_firmwares(fe);
723 if (rc < 0)
724 return rc;
727 if (priv->ctrl.mts && !(type & FM))
728 type |= MTS;
730 retry:
731 new_fw.type = type;
732 new_fw.id = std;
733 new_fw.std_req = std;
734 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
735 new_fw.scode_nr = 0;
736 new_fw.int_freq = int_freq;
738 tuner_dbg("checking firmware, user requested type=");
739 if (debug) {
740 dump_firm_type(new_fw.type);
741 printk("(%x), id %016llx, ", new_fw.type,
742 (unsigned long long)new_fw.std_req);
743 if (!int_freq) {
744 printk("scode_tbl ");
745 dump_firm_type(priv->ctrl.scode_table);
746 printk("(%x), ", priv->ctrl.scode_table);
747 } else
748 printk("int_freq %d, ", new_fw.int_freq);
749 printk("scode_nr %d\n", new_fw.scode_nr);
752 /* No need to reload base firmware if it matches */
753 if (((BASE | new_fw.type) & BASE_TYPES) ==
754 (priv->cur_fw.type & BASE_TYPES)) {
755 tuner_dbg("BASE firmware not changed.\n");
756 goto skip_base;
759 /* Updating BASE - forget about all currently loaded firmware */
760 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
762 /* Reset is needed before loading firmware */
763 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
764 if (rc < 0)
765 goto fail;
767 /* BASE firmwares are all std0 */
768 std0 = 0;
769 rc = load_firmware(fe, BASE | new_fw.type, &std0);
770 if (rc < 0) {
771 tuner_err("Error %d while loading base firmware\n",
772 rc);
773 goto fail;
776 /* Load INIT1, if needed */
777 tuner_dbg("Load init1 firmware, if exists\n");
779 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
780 if (rc == -ENOENT)
781 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
782 &std0);
783 if (rc < 0 && rc != -ENOENT) {
784 tuner_err("Error %d while loading init1 firmware\n",
785 rc);
786 goto fail;
789 skip_base:
791 * No need to reload standard specific firmware if base firmware
792 * was not reloaded and requested video standards have not changed.
794 if (priv->cur_fw.type == (BASE | new_fw.type) &&
795 priv->cur_fw.std_req == std) {
796 tuner_dbg("Std-specific firmware already loaded.\n");
797 goto skip_std_specific;
800 /* Reloading std-specific firmware forces a SCODE update */
801 priv->cur_fw.scode_table = 0;
803 rc = load_firmware(fe, new_fw.type, &new_fw.id);
804 if (rc == -ENOENT)
805 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
807 if (rc < 0)
808 goto fail;
810 skip_std_specific:
811 if (priv->cur_fw.scode_table == new_fw.scode_table &&
812 priv->cur_fw.scode_nr == new_fw.scode_nr) {
813 tuner_dbg("SCODE firmware already loaded.\n");
814 goto check_device;
817 if (new_fw.type & FM)
818 goto check_device;
820 /* Load SCODE firmware, if exists */
821 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
823 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
824 new_fw.int_freq, new_fw.scode_nr);
826 check_device:
827 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
828 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
829 tuner_err("Unable to read tuner registers.\n");
830 goto fail;
833 tuner_dbg("Device is Xceive %d version %d.%d, "
834 "firmware version %d.%d\n",
835 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
836 (version & 0xf0) >> 4, version & 0xf);
839 if (priv->ctrl.read_not_reliable)
840 goto read_not_reliable;
842 /* Check firmware version against what we downloaded. */
843 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
844 if (!priv->ctrl.read_not_reliable) {
845 tuner_err("Incorrect readback of firmware version.\n");
846 goto fail;
847 } else {
848 tuner_err("Returned an incorrect version. However, "
849 "read is not reliable enough. Ignoring it.\n");
850 hwmodel = 3028;
854 /* Check that the tuner hardware model remains consistent over time. */
855 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
856 priv->hwmodel = hwmodel;
857 priv->hwvers = version & 0xff00;
858 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
859 priv->hwvers != (version & 0xff00)) {
860 tuner_err("Read invalid device hardware information - tuner "
861 "hung?\n");
862 goto fail;
865 read_not_reliable:
866 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
869 * By setting BASE in cur_fw.type only after successfully loading all
870 * firmwares, we can:
871 * 1. Identify that BASE firmware with type=0 has been loaded;
872 * 2. Tell whether BASE firmware was just changed the next time through.
874 priv->cur_fw.type |= BASE;
876 return 0;
878 fail:
879 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
880 if (retry_count < 8) {
881 msleep(50);
882 retry_count++;
883 tuner_dbg("Retrying firmware load\n");
884 goto retry;
887 if (rc == -ENOENT)
888 rc = -EINVAL;
889 return rc;
892 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
894 struct xc2028_data *priv = fe->tuner_priv;
895 u16 frq_lock, signal = 0;
896 int rc;
898 tuner_dbg("%s called\n", __func__);
900 mutex_lock(&priv->lock);
902 /* Sync Lock Indicator */
903 rc = xc2028_get_reg(priv, XREG_LOCK, &frq_lock);
904 if (rc < 0)
905 goto ret;
907 /* Frequency is locked */
908 if (frq_lock == 1)
909 signal = 1 << 11;
911 /* Get SNR of the video signal */
912 rc = xc2028_get_reg(priv, XREG_SNR, &signal);
913 if (rc < 0)
914 goto ret;
916 /* Use both frq_lock and signal to generate the result */
917 signal = signal || ((signal & 0x07) << 12);
919 ret:
920 mutex_unlock(&priv->lock);
922 *strength = signal;
924 tuner_dbg("signal strength is %d\n", signal);
926 return rc;
929 #define DIV 15625
931 static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
932 enum v4l2_tuner_type new_type,
933 unsigned int type,
934 v4l2_std_id std,
935 u16 int_freq)
937 struct xc2028_data *priv = fe->tuner_priv;
938 int rc = -EINVAL;
939 unsigned char buf[4];
940 u32 div, offset = 0;
942 tuner_dbg("%s called\n", __func__);
944 mutex_lock(&priv->lock);
946 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
948 if (check_firmware(fe, type, std, int_freq) < 0)
949 goto ret;
951 /* On some cases xc2028 can disable video output, if
952 * very weak signals are received. By sending a soft
953 * reset, this is re-enabled. So, it is better to always
954 * send a soft reset before changing channels, to be sure
955 * that xc2028 will be in a safe state.
956 * Maybe this might also be needed for DTV.
958 switch (new_type) {
959 case V4L2_TUNER_ANALOG_TV:
960 rc = send_seq(priv, {0x00, 0x00});
962 /* Analog mode requires offset = 0 */
963 break;
964 case V4L2_TUNER_RADIO:
965 /* Radio mode requires offset = 0 */
966 break;
967 case V4L2_TUNER_DIGITAL_TV:
969 * Digital modes require an offset to adjust to the
970 * proper frequency. The offset depends on what
971 * firmware version is used.
975 * Adjust to the center frequency. This is calculated by the
976 * formula: offset = 1.25MHz - BW/2
977 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
978 * further adjustment to get the frequency center on VHF
982 * The firmware DTV78 used to work fine in UHF band (8 MHz
983 * bandwidth) but not at all in VHF band (7 MHz bandwidth).
984 * The real problem was connected to the formula used to
985 * calculate the center frequency offset in VHF band.
986 * In fact, removing the 500KHz adjustment fixed the problem.
987 * This is coherent to what was implemented for the DTV7
988 * firmware.
989 * In the end, now the center frequency is the same for all 3
990 * firmwares (DTV7, DTV8, DTV78) and doesn't depend on channel
991 * bandwidth.
994 if (priv->cur_fw.type & DTV6)
995 offset = 1750000;
996 else /* DTV7 or DTV8 or DTV78 */
997 offset = 2750000;
1000 * xc3028 additional "magic"
1001 * Depending on the firmware version, it needs some adjustments
1002 * to properly centralize the frequency. This seems to be
1003 * needed to compensate the SCODE table adjustments made by
1004 * newer firmwares
1008 * The proper adjustment would be to do it at s-code table.
1009 * However, this didn't work, as reported by
1010 * Robert Lowery <rglowery@exemail.com.au>
1013 #if 0
1015 * Still need tests for XC3028L (firmware 3.2 or upper)
1016 * So, for now, let's just comment the per-firmware
1017 * version of this change. Reports with xc3028l working
1018 * with and without the lines bellow are welcome
1021 if (priv->firm_version < 0x0302) {
1022 if (priv->cur_fw.type & DTV7)
1023 offset += 500000;
1024 } else {
1025 if (priv->cur_fw.type & DTV7)
1026 offset -= 300000;
1027 else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
1028 offset += 200000;
1030 #endif
1033 div = (freq - offset + DIV / 2) / DIV;
1035 /* CMD= Set frequency */
1036 if (priv->firm_version < 0x0202)
1037 rc = send_seq(priv, {0x00, XREG_RF_FREQ, 0x00, 0x00});
1038 else
1039 rc = send_seq(priv, {0x80, XREG_RF_FREQ, 0x00, 0x00});
1040 if (rc < 0)
1041 goto ret;
1043 /* Return code shouldn't be checked.
1044 The reset CLK is needed only with tm6000.
1045 Driver should work fine even if this fails.
1047 if (priv->ctrl.msleep)
1048 msleep(priv->ctrl.msleep);
1049 do_tuner_callback(fe, XC2028_RESET_CLK, 1);
1051 msleep(10);
1053 buf[0] = 0xff & (div >> 24);
1054 buf[1] = 0xff & (div >> 16);
1055 buf[2] = 0xff & (div >> 8);
1056 buf[3] = 0xff & (div);
1058 rc = i2c_send(priv, buf, sizeof(buf));
1059 if (rc < 0)
1060 goto ret;
1061 msleep(100);
1063 priv->frequency = freq;
1065 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
1066 buf[0], buf[1], buf[2], buf[3],
1067 freq / 1000000, (freq % 1000000) / 1000);
1069 rc = 0;
1071 ret:
1072 mutex_unlock(&priv->lock);
1074 return rc;
1077 static int xc2028_set_analog_freq(struct dvb_frontend *fe,
1078 struct analog_parameters *p)
1080 struct xc2028_data *priv = fe->tuner_priv;
1081 unsigned int type=0;
1083 tuner_dbg("%s called\n", __func__);
1085 if (p->mode == V4L2_TUNER_RADIO) {
1086 type |= FM;
1087 if (priv->ctrl.input1)
1088 type |= INPUT1;
1089 return generic_set_freq(fe, (625l * p->frequency) / 10,
1090 V4L2_TUNER_RADIO, type, 0, 0);
1093 /* if std is not defined, choose one */
1094 if (!p->std)
1095 p->std = V4L2_STD_MN;
1097 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
1098 if (!(p->std & V4L2_STD_MN))
1099 type |= F8MHZ;
1101 /* Add audio hack to std mask */
1102 p->std |= parse_audio_std_option();
1104 return generic_set_freq(fe, 62500l * p->frequency,
1105 V4L2_TUNER_ANALOG_TV, type, p->std, 0);
1108 static int xc2028_set_params(struct dvb_frontend *fe)
1110 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1111 u32 delsys = c->delivery_system;
1112 u32 bw = c->bandwidth_hz;
1113 struct xc2028_data *priv = fe->tuner_priv;
1114 unsigned int type=0;
1115 u16 demod = 0;
1117 tuner_dbg("%s called\n", __func__);
1119 switch (delsys) {
1120 case SYS_DVBT:
1121 case SYS_DVBT2:
1123 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1124 * Both seem to require QAM firmware for OFDM decoding
1125 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1127 if (bw <= 6000000)
1128 type |= QAM;
1130 switch (priv->ctrl.type) {
1131 case XC2028_D2633:
1132 type |= D2633;
1133 break;
1134 case XC2028_D2620:
1135 type |= D2620;
1136 break;
1137 case XC2028_AUTO:
1138 default:
1139 /* Zarlink seems to need D2633 */
1140 if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
1141 type |= D2633;
1142 else
1143 type |= D2620;
1145 break;
1146 case SYS_ATSC:
1147 /* The only ATSC firmware (at least on v2.7) is D2633 */
1148 type |= ATSC | D2633;
1149 break;
1150 /* DVB-S and pure QAM (FE_QAM) are not supported */
1151 default:
1152 return -EINVAL;
1155 if (bw <= 6000000) {
1156 type |= DTV6;
1157 priv->ctrl.vhfbw7 = 0;
1158 priv->ctrl.uhfbw8 = 0;
1159 } else if (bw <= 7000000) {
1160 if (c->frequency < 470000000)
1161 priv->ctrl.vhfbw7 = 1;
1162 else
1163 priv->ctrl.uhfbw8 = 0;
1164 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1165 type |= F8MHZ;
1166 } else {
1167 if (c->frequency < 470000000)
1168 priv->ctrl.vhfbw7 = 0;
1169 else
1170 priv->ctrl.uhfbw8 = 1;
1171 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1172 type |= F8MHZ;
1175 /* All S-code tables need a 200kHz shift */
1176 if (priv->ctrl.demod) {
1177 demod = priv->ctrl.demod;
1180 * Newer firmwares require a 200 kHz offset only for ATSC
1182 if (type == ATSC || priv->firm_version < 0x0302)
1183 demod += 200;
1185 * The DTV7 S-code table needs a 700 kHz shift.
1187 * DTV7 is only used in Australia. Germany or Italy may also
1188 * use this firmware after initialization, but a tune to a UHF
1189 * channel should then cause DTV78 to be used.
1191 * Unfortunately, on real-field tests, the s-code offset
1192 * didn't work as expected, as reported by
1193 * Robert Lowery <rglowery@exemail.com.au>
1197 return generic_set_freq(fe, c->frequency,
1198 V4L2_TUNER_DIGITAL_TV, type, 0, demod);
1201 static int xc2028_sleep(struct dvb_frontend *fe)
1203 struct xc2028_data *priv = fe->tuner_priv;
1204 int rc = 0;
1206 /* Avoid firmware reload on slow devices or if PM disabled */
1207 if (no_poweroff || priv->ctrl.disable_power_mgmt)
1208 return 0;
1210 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
1211 if (debug > 1) {
1212 tuner_dbg("Printing sleep stack trace:\n");
1213 dump_stack();
1216 mutex_lock(&priv->lock);
1218 if (priv->firm_version < 0x0202)
1219 rc = send_seq(priv, {0x00, XREG_POWER_DOWN, 0x00, 0x00});
1220 else
1221 rc = send_seq(priv, {0x80, XREG_POWER_DOWN, 0x00, 0x00});
1223 priv->cur_fw.type = 0; /* need firmware reload */
1225 mutex_unlock(&priv->lock);
1227 return rc;
1230 static int xc2028_dvb_release(struct dvb_frontend *fe)
1232 struct xc2028_data *priv = fe->tuner_priv;
1234 tuner_dbg("%s called\n", __func__);
1236 mutex_lock(&xc2028_list_mutex);
1238 /* only perform final cleanup if this is the last instance */
1239 if (hybrid_tuner_report_instance_count(priv) == 1) {
1240 kfree(priv->ctrl.fname);
1241 free_firmware(priv);
1244 if (priv)
1245 hybrid_tuner_release_state(priv);
1247 mutex_unlock(&xc2028_list_mutex);
1249 fe->tuner_priv = NULL;
1251 return 0;
1254 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1256 struct xc2028_data *priv = fe->tuner_priv;
1258 tuner_dbg("%s called\n", __func__);
1260 *frequency = priv->frequency;
1262 return 0;
1265 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1267 struct xc2028_data *priv = fe->tuner_priv;
1268 struct xc2028_ctrl *p = priv_cfg;
1269 int rc = 0;
1271 tuner_dbg("%s called\n", __func__);
1273 mutex_lock(&priv->lock);
1275 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1276 if (priv->ctrl.max_len < 9)
1277 priv->ctrl.max_len = 13;
1279 if (p->fname) {
1280 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1281 kfree(priv->ctrl.fname);
1282 free_firmware(priv);
1285 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1286 if (priv->ctrl.fname == NULL)
1287 rc = -ENOMEM;
1290 mutex_unlock(&priv->lock);
1292 return rc;
1295 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1296 .info = {
1297 .name = "Xceive XC3028",
1298 .frequency_min = 42000000,
1299 .frequency_max = 864000000,
1300 .frequency_step = 50000,
1303 .set_config = xc2028_set_config,
1304 .set_analog_params = xc2028_set_analog_freq,
1305 .release = xc2028_dvb_release,
1306 .get_frequency = xc2028_get_frequency,
1307 .get_rf_strength = xc2028_signal,
1308 .set_params = xc2028_set_params,
1309 .sleep = xc2028_sleep,
1312 struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1313 struct xc2028_config *cfg)
1315 struct xc2028_data *priv;
1316 int instance;
1318 if (debug)
1319 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
1321 if (NULL == cfg)
1322 return NULL;
1324 if (!fe) {
1325 printk(KERN_ERR "xc2028: No frontend!\n");
1326 return NULL;
1329 mutex_lock(&xc2028_list_mutex);
1331 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1332 hybrid_tuner_instance_list,
1333 cfg->i2c_adap, cfg->i2c_addr,
1334 "xc2028");
1335 switch (instance) {
1336 case 0:
1337 /* memory allocation failure */
1338 goto fail;
1339 break;
1340 case 1:
1341 /* new tuner instance */
1342 priv->ctrl.max_len = 13;
1344 mutex_init(&priv->lock);
1346 fe->tuner_priv = priv;
1347 break;
1348 case 2:
1349 /* existing tuner instance */
1350 fe->tuner_priv = priv;
1351 break;
1354 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1355 sizeof(xc2028_dvb_tuner_ops));
1357 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1359 if (cfg->ctrl)
1360 xc2028_set_config(fe, cfg->ctrl);
1362 mutex_unlock(&xc2028_list_mutex);
1364 return fe;
1365 fail:
1366 mutex_unlock(&xc2028_list_mutex);
1368 xc2028_dvb_release(fe);
1369 return NULL;
1372 EXPORT_SYMBOL(xc2028_attach);
1374 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1375 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1376 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1377 MODULE_LICENSE("GPL");