]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/tuner-xc2028.c
V4L/DVB (6636): xc2028: protect device list
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / tuner-xc2028.c
CommitLineData
6cb45879
MCC
1/* tuner-xc2028
2 *
3 * Copyright (c) 2007 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
9dd659de 26#define PREFIX "xc2028"
215b95ba 27
83fb340b
MCC
28static int debug;
29module_param(debug, int, 0644);
30MODULE_PARM_DESC(debug, "enable verbose debug messages");
31
a82200fb
MCC
32static char audio_std[8];
33module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
34MODULE_PARM_DESC(audio_std,
35 "Audio standard. XC3028 audio decoder explicitly "
36 "needs to know what audio\n"
37 "standard is needed for some video standards with audio A2 or NICAM.\n"
38 "The valid values are:\n"
39 "A2\n"
40 "A2/A\n"
41 "A2/B\n"
42 "NICAM\n"
43 "NICAM/A\n"
44 "NICAM/B\n");
45
215b95ba 46static LIST_HEAD(xc2028_list);
aa501be9
CP
47static DEFINE_MUTEX(xc2028_list_mutex);
48
de3fe21b
MCC
49/* struct for storing firmware table */
50struct firmware_description {
51 unsigned int type;
52 v4l2_std_id id;
53 unsigned char *ptr;
54 unsigned int size;
55};
6cb45879
MCC
56
57struct xc2028_data {
215b95ba
MCC
58 struct list_head xc2028_list;
59 struct tuner_i2c_props i2c_props;
60 int (*tuner_callback) (void *dev,
61 int command, int arg);
215b95ba
MCC
62 void *video_dev;
63 int count;
de3fe21b
MCC
64 __u32 frequency;
65
66 struct firmware_description *firm;
67 int firm_size;
68
69 __u16 version;
70
71 struct xc2028_ctrl ctrl;
215b95ba 72
701672eb
ML
73 v4l2_std_id firm_type; /* video stds supported
74 by current firmware */
75 fe_bandwidth_t bandwidth; /* Firmware bandwidth:
76 6M, 7M or 8M */
77 int need_load_generic; /* The generic firmware
78 were loaded? */
de3fe21b
MCC
79
80 int max_len; /* Max firmware chunk */
81
701672eb
ML
82 enum tuner_mode mode;
83 struct i2c_client *i2c_client;
215b95ba
MCC
84
85 struct mutex lock;
6cb45879
MCC
86};
87
47cc5b78
CP
88#define i2c_send(priv, buf, size) ({ \
89 int _rc; \
90 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
91 if (size != _rc) \
92 tuner_info("i2c output error: rc = %d (should be %d)\n",\
93 _rc, (int)size); \
94 _rc; \
95})
96
97#define i2c_rcv(priv, buf, size) ({ \
98 int _rc; \
99 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
100 if (size != _rc) \
83fb340b 101 tuner_err("i2c input error: rc = %d (should be %d)\n", \
47cc5b78
CP
102 _rc, (int)size); \
103 _rc; \
104})
ab0b9fc6 105
7d58d111
CP
106#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
107 int _rc; \
108 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
109 ibuf, isize); \
110 if (isize != _rc) \
111 tuner_err("i2c input error: rc = %d (should be %d)\n", \
112 _rc, (int)isize); \
113 _rc; \
114})
115
47cc5b78 116#define send_seq(priv, data...) ({ \
215b95ba 117 static u8 _val[] = data; \
47cc5b78 118 int _rc; \
6cb45879 119 if (sizeof(_val) != \
47cc5b78 120 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
215b95ba 121 _val, sizeof(_val)))) { \
47cc5b78
CP
122 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
123 } else \
124 msleep(10); \
125 _rc; \
126})
6cb45879 127
7d58d111 128static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
6cb45879 129{
b873e1a3 130 unsigned char buf[2];
7d58d111 131 unsigned char ibuf[2];
215b95ba 132
7d58d111 133 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
6cb45879 134
7d58d111 135 buf[0] = reg >> 8;
80b52208 136 buf[1] = (unsigned char) reg;
6cb45879 137
7d58d111
CP
138 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
139 return -EIO;
6cb45879 140
7d58d111
CP
141 *val = (ibuf[1]) | (ibuf[0] << 8);
142 return 0;
6cb45879
MCC
143}
144
43efe702
MCC
145void dump_firm_type(unsigned int type)
146{
147 if (type & BASE)
148 printk("BASE ");
f380e1d2
MCC
149 if (type & INIT1)
150 printk("INIT1 ");
43efe702
MCC
151 if (type & F8MHZ)
152 printk("F8MHZ ");
153 if (type & MTS)
154 printk("MTS ");
155 if (type & D2620)
156 printk("D2620 ");
157 if (type & D2633)
158 printk("D2633 ");
159 if (type & DTV6)
160 printk("DTV6 ");
161 if (type & QAM)
162 printk("QAM ");
163 if (type & DTV7)
164 printk("DTV7 ");
165 if (type & DTV78)
166 printk("DTV78 ");
167 if (type & DTV8)
168 printk("DTV8 ");
169 if (type & FM)
170 printk("FM ");
171 if (type & INPUT1)
172 printk("INPUT1 ");
173 if (type & LCD)
174 printk("LCD ");
175 if (type & NOGD)
176 printk("NOGD ");
177 if (type & MONO)
178 printk("MONO ");
179 if (type & ATSC)
180 printk("ATSC ");
181 if (type & IF)
182 printk("IF ");
183 if (type & LG60)
184 printk("LG60 ");
185 if (type & ATI638)
186 printk("ATI638 ");
187 if (type & OREN538)
188 printk("OREN538 ");
189 if (type & OREN36)
190 printk("OREN36 ");
191 if (type & TOYOTA388)
192 printk("TOYOTA388 ");
193 if (type & TOYOTA794)
194 printk("TOYOTA794 ");
195 if (type & DIBCOM52)
196 printk("DIBCOM52 ");
197 if (type & ZARLINK456)
198 printk("ZARLINK456 ");
199 if (type & CHINA)
200 printk("CHINA ");
201 if (type & F6MHZ)
202 printk("F6MHZ ");
203 if (type & INPUT2)
204 printk("INPUT2 ");
205 if (type & SCODE)
206 printk("SCODE ");
207}
208
ef8c1888 209static v4l2_std_id parse_audio_std_option(void)
a82200fb 210{
e155d908 211 if (strcasecmp(audio_std, "A2") == 0)
a82200fb 212 return V4L2_STD_A2;
e155d908 213 if (strcasecmp(audio_std, "A2/A") == 0)
a82200fb 214 return V4L2_STD_A2_A;
e155d908 215 if (strcasecmp(audio_std, "A2/B") == 0)
a82200fb 216 return V4L2_STD_A2_B;
e155d908 217 if (strcasecmp(audio_std, "NICAM") == 0)
a82200fb 218 return V4L2_STD_NICAM;
e155d908 219 if (strcasecmp(audio_std, "NICAM/A") == 0)
a82200fb 220 return V4L2_STD_NICAM_A;
e155d908 221 if (strcasecmp(audio_std, "NICAM/B") == 0)
a82200fb
MCC
222 return V4L2_STD_NICAM_B;
223
224 return 0;
225}
226
ab0b9fc6 227static void free_firmware(struct xc2028_data *priv)
6cb45879 228{
de3fe21b
MCC
229 int i;
230
231 if (!priv->firm)
232 return;
233
ab0b9fc6
MCC
234 for (i = 0; i < priv->firm_size; i++)
235 kfree(priv->firm[i].ptr);
236
de3fe21b
MCC
237 kfree(priv->firm);
238
ab0b9fc6 239 priv->firm = NULL;
de3fe21b
MCC
240 priv->need_load_generic = 1;
241}
242
ab0b9fc6 243static int load_all_firmwares(struct dvb_frontend *fe)
de3fe21b
MCC
244{
245 struct xc2028_data *priv = fe->tuner_priv;
ab0b9fc6 246 const struct firmware *fw = NULL;
6cb45879 247 unsigned char *p, *endp;
ab0b9fc6
MCC
248 int rc = 0;
249 int n, n_array;
de3fe21b 250 char name[33];
6cb45879 251
83fb340b 252 tuner_dbg("%s called\n", __FUNCTION__);
215b95ba 253
f380e1d2 254 tuner_info("Reading firmware %s\n", priv->ctrl.fname);
a37b4c9b
ML
255 rc = request_firmware(&fw, priv->ctrl.fname,
256 &priv->i2c_props.adap->dev);
6cb45879 257 if (rc < 0) {
ab0b9fc6 258 if (rc == -ENOENT)
83fb340b 259 tuner_err("Error: firmware %s not found.\n",
de3fe21b 260 priv->ctrl.fname);
2e4160ca 261 else
83fb340b 262 tuner_err("Error %d while requesting firmware %s \n",
de3fe21b 263 rc, priv->ctrl.fname);
2e4160ca 264
6cb45879
MCC
265 return rc;
266 }
ab0b9fc6
MCC
267 p = fw->data;
268 endp = p + fw->size;
6cb45879 269
ab0b9fc6 270 if (fw->size < sizeof(name) - 1 + 2) {
83fb340b 271 tuner_err("Error: firmware size is zero!\n");
ab0b9fc6 272 rc = -EINVAL;
de3fe21b 273 goto done;
6cb45879 274 }
de3fe21b 275
ab0b9fc6
MCC
276 memcpy(name, p, sizeof(name) - 1);
277 name[sizeof(name) - 1] = 0;
278 p += sizeof(name) - 1;
de3fe21b 279
ab0b9fc6 280 priv->version = le16_to_cpu(*(__u16 *) p);
de3fe21b
MCC
281 p += 2;
282
83fb340b 283 tuner_info("Firmware: %s, ver %d.%d\n", name,
ab0b9fc6 284 priv->version >> 8, priv->version & 0xff);
de3fe21b 285
ab0b9fc6 286 if (p + 2 > endp)
de3fe21b
MCC
287 goto corrupt;
288
ab0b9fc6 289 n_array = le16_to_cpu(*(__u16 *) p);
de3fe21b
MCC
290 p += 2;
291
83fb340b
MCC
292 tuner_info("There are %d firmwares at %s\n",
293 n_array, priv->ctrl.fname);
de3fe21b 294
ab0b9fc6 295 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
de3fe21b
MCC
296
297 if (!fw) {
83fb340b 298 tuner_err("Not enough memory for reading firmware.\n");
ab0b9fc6 299 rc = -ENOMEM;
de3fe21b 300 goto done;
6cb45879
MCC
301 }
302
de3fe21b 303 priv->firm_size = n_array;
ab0b9fc6
MCC
304 n = -1;
305 while (p < endp) {
de3fe21b
MCC
306 __u32 type, size;
307 v4l2_std_id id;
308
309 n++;
310 if (n >= n_array) {
83fb340b 311 tuner_err("Too much firmwares at the file\n");
de3fe21b
MCC
312 goto corrupt;
313 }
314
315 /* Checks if there's enough bytes to read */
ab0b9fc6 316 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
83fb340b 317 tuner_err("Firmware header is incomplete!\n");
de3fe21b
MCC
318 goto corrupt;
319 }
320
ab0b9fc6 321 type = le32_to_cpu(*(__u32 *) p);
de3fe21b
MCC
322 p += sizeof(type);
323
ab0b9fc6 324 id = le64_to_cpu(*(v4l2_std_id *) p);
de3fe21b
MCC
325 p += sizeof(id);
326
2fc580ff 327 size = le32_to_cpu(*(__u32 *) p);
de3fe21b
MCC
328 p += sizeof(size);
329
ab0b9fc6 330 if ((!size) || (size + p > endp)) {
83fb340b 331 tuner_err("Firmware type ");
43efe702 332 dump_firm_type(type);
ef8c1888
MCC
333 printk("(%x), id %llx is corrupted "
334 "(size=%d, expected %d)\n",
91240dd9 335 type, (unsigned long long)id,
ef8c1888 336 (unsigned)(endp - p), size);
de3fe21b
MCC
337 goto corrupt;
338 }
339
ab0b9fc6 340 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
de3fe21b 341 if (!priv->firm[n].ptr) {
83fb340b 342 tuner_err("Not enough memory.\n");
ab0b9fc6 343 rc = -ENOMEM;
de3fe21b
MCC
344 goto err;
345 }
83fb340b 346 tuner_info("Reading firmware type ");
43efe702 347 dump_firm_type(type);
91240dd9
CP
348 printk("(%x), id %llx, size=%d.\n",
349 type, (unsigned long long)id, size);
de3fe21b
MCC
350
351 memcpy(priv->firm[n].ptr, p, size);
352 priv->firm[n].type = type;
353 priv->firm[n].id = id;
354 priv->firm[n].size = size;
355
356 p += size;
357 }
358
ab0b9fc6 359 if (n + 1 != priv->firm_size) {
83fb340b 360 tuner_err("Firmware file is incomplete!\n");
de3fe21b
MCC
361 goto corrupt;
362 }
363
364 goto done;
365
366corrupt:
ab0b9fc6 367 rc = -EINVAL;
83fb340b 368 tuner_err("Error: firmware file is corrupted!\n");
de3fe21b
MCC
369
370err:
371 tuner_info("Releasing loaded firmware file.\n");
372
373 free_firmware(priv);
374
375done:
376 release_firmware(fw);
83fb340b 377 tuner_dbg("Firmware files loaded.\n");
de3fe21b
MCC
378
379 return rc;
380}
381
f380e1d2
MCC
382static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
383 v4l2_std_id *id)
de3fe21b
MCC
384{
385 struct xc2028_data *priv = fe->tuner_priv;
f380e1d2 386 int i;
de3fe21b 387
83fb340b 388 tuner_dbg("%s called\n", __FUNCTION__);
de3fe21b
MCC
389
390 if (!priv->firm) {
83fb340b 391 tuner_err("Error! firmware not loaded\n");
de3fe21b
MCC
392 return -EINVAL;
393 }
394
f380e1d2 395 if (((type & ~SCODE) == 0) && (*id == 0))
ab0b9fc6 396 *id = V4L2_STD_PAL;
de3fe21b
MCC
397
398 /* Seek for exact match */
ab0b9fc6
MCC
399 for (i = 0; i < priv->firm_size; i++) {
400 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
de3fe21b
MCC
401 goto found;
402 }
403
404 /* Seek for generic video standard match */
ab0b9fc6
MCC
405 for (i = 0; i < priv->firm_size; i++) {
406 if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
de3fe21b
MCC
407 goto found;
408 }
409
410 /*FIXME: Would make sense to seek for type "hint" match ? */
411
f380e1d2
MCC
412 i = -EINVAL;
413 goto ret;
de3fe21b
MCC
414
415found:
416 *id = priv->firm[i].id;
de3fe21b 417
f380e1d2 418ret:
83fb340b
MCC
419 tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
420 if (debug) {
421 dump_firm_type(type);
91240dd9 422 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
83fb340b 423 }
f380e1d2
MCC
424 return i;
425}
426
427static int load_firmware(struct dvb_frontend *fe, unsigned int type,
428 v4l2_std_id *id)
429{
430 struct xc2028_data *priv = fe->tuner_priv;
431 int pos, rc;
432 unsigned char *p, *endp, buf[priv->max_len];
433
83fb340b 434 tuner_dbg("%s called\n", __FUNCTION__);
f380e1d2
MCC
435
436 pos = seek_firmware(fe, type, id);
437 if (pos < 0)
438 return pos;
439
83fb340b
MCC
440 tuner_info("Loading firmware for type=");
441 dump_firm_type(type);
91240dd9 442 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
83fb340b 443
f380e1d2 444 p = priv->firm[pos].ptr;
de3fe21b
MCC
445
446 if (!p) {
83fb340b 447 tuner_err("Firmware pointer were freed!");
de3fe21b 448 return -EINVAL;
6cb45879 449 }
f380e1d2 450 endp = p + priv->firm[pos].size;
6cb45879 451
ab0b9fc6 452 while (p < endp) {
de3fe21b
MCC
453 __u16 size;
454
455 /* Checks if there's enough bytes to read */
ab0b9fc6 456 if (p + sizeof(size) > endp) {
83fb340b 457 tuner_err("Firmware chunk size is wrong\n");
de3fe21b
MCC
458 return -EINVAL;
459 }
460
ab0b9fc6 461 size = le16_to_cpu(*(__u16 *) p);
de3fe21b
MCC
462 p += sizeof(size);
463
464 if (size == 0xffff)
465 return 0;
466
467 if (!size) {
6cb45879 468 /* Special callback command received */
215b95ba 469 rc = priv->tuner_callback(priv->video_dev,
ab0b9fc6
MCC
470 XC2028_TUNER_RESET, 0);
471 if (rc < 0) {
83fb340b 472 tuner_err("Error at RESET code %d\n",
ab0b9fc6 473 (*p) & 0x7f);
de3fe21b 474 return -EINVAL;
6cb45879 475 }
6cb45879
MCC
476 continue;
477 }
5403bbae
ML
478 if (size >= 0xff00) {
479 switch (size) {
480 case 0xff00:
481 rc = priv->tuner_callback(priv->video_dev,
482 XC2028_RESET_CLK, 0);
483 if (rc < 0) {
484 tuner_err("Error at RESET code %d\n",
485 (*p) & 0x7f);
486 return -EINVAL;
487 }
488 default:
489 tuner_info("Invalid RESET code %d\n",
490 size & 0x7f);
491 return -EINVAL;
492
493 }
2d4c0ac6 494 continue;
5403bbae 495 }
de3fe21b
MCC
496
497 /* Checks for a sleep command */
498 if (size & 0x8000) {
ab0b9fc6 499 msleep(size & 0x7fff);
de3fe21b 500 continue;
6cb45879
MCC
501 }
502
de3fe21b 503 if ((size + p > endp)) {
83fb340b 504 tuner_err("missing bytes: need %d, have %d\n",
ab0b9fc6 505 size, (int)(endp - p));
de3fe21b
MCC
506 return -EINVAL;
507 }
6cb45879 508
de3fe21b 509 buf[0] = *p;
6cb45879 510 p++;
de3fe21b 511 size--;
6cb45879 512
de3fe21b 513 /* Sends message chunks */
ab0b9fc6
MCC
514 while (size > 0) {
515 int len = (size < priv->max_len - 1) ?
516 size : priv->max_len - 1;
6cb45879 517
ab0b9fc6 518 memcpy(buf + 1, p, len);
6cb45879 519
47cc5b78 520 rc = i2c_send(priv, buf, len + 1);
ab0b9fc6 521 if (rc < 0) {
83fb340b 522 tuner_err("%d returned from send\n", rc);
de3fe21b
MCC
523 return -EINVAL;
524 }
525
526 p += len;
527 size -= len;
528 }
529 }
43efe702 530 return 0;
6cb45879
MCC
531}
532
f380e1d2
MCC
533static int load_scode(struct dvb_frontend *fe, unsigned int type,
534 v4l2_std_id *id, int scode)
535{
536 struct xc2028_data *priv = fe->tuner_priv;
537 int pos, rc;
538 unsigned char *p;
539
83fb340b 540 tuner_dbg("%s called\n", __FUNCTION__);
f380e1d2
MCC
541
542 pos = seek_firmware(fe, type, id);
543 if (pos < 0)
544 return pos;
545
546 p = priv->firm[pos].ptr;
547
548 if (!p) {
83fb340b 549 tuner_err("Firmware pointer were freed!");
f380e1d2
MCC
550 return -EINVAL;
551 }
552
553 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
554 return -EINVAL;
555
47cc5b78
CP
556 if (priv->version < 0x0202)
557 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
558 else
559 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
560 if (rc < 0)
561 return -EIO;
f380e1d2 562
47cc5b78
CP
563 rc = i2c_send(priv, p + 12 * scode, 12);
564 if (rc < 0)
565 return -EIO;
f380e1d2 566
47cc5b78
CP
567 rc = send_seq(priv, {0x00, 0x8c});
568 if (rc < 0)
569 return -EIO;
f380e1d2
MCC
570
571 return 0;
572}
573
215b95ba 574static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
ab0b9fc6 575 v4l2_std_id std, fe_bandwidth_t bandwidth)
6cb45879 576{
215b95ba 577 struct xc2028_data *priv = fe->tuner_priv;
7d58d111
CP
578 int rc;
579 u16 version, hwmodel;
ab0b9fc6
MCC
580 v4l2_std_id std0 = 0;
581 unsigned int type0 = 0, type = 0;
de3fe21b 582 int change_digital_bandwidth;
6cb45879 583
83fb340b 584 tuner_dbg("%s called\n", __FUNCTION__);
6cb45879 585
de3fe21b 586 if (!priv->firm) {
a37b4c9b
ML
587 if (!priv->ctrl.fname) {
588 tuner_info("xc2028/3028 firmware name not set!\n");
de3fe21b 589 return -EINVAL;
a37b4c9b 590 }
de3fe21b 591
ab0b9fc6
MCC
592 rc = load_all_firmwares(fe);
593 if (rc < 0)
de3fe21b
MCC
594 return rc;
595 }
596
83fb340b 597 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
ab0b9fc6 598 priv->mode, new_mode);
701672eb
ML
599
600 /* first of all, determine whether we have switched the mode */
ab0b9fc6 601 if (new_mode != priv->mode) {
215b95ba
MCC
602 priv->mode = new_mode;
603 priv->need_load_generic = 1;
701672eb
ML
604 }
605
215b95ba 606 change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
ab0b9fc6 607 && bandwidth != priv->bandwidth) ? 1 : 0;
83fb340b 608 tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
ab0b9fc6 609 bandwidth);
701672eb 610
215b95ba 611 if (priv->need_load_generic) {
6cb45879 612 /* Reset is needed before loading firmware */
215b95ba
MCC
613 rc = priv->tuner_callback(priv->video_dev,
614 XC2028_TUNER_RESET, 0);
ab0b9fc6 615 if (rc < 0)
6cb45879
MCC
616 return rc;
617
ab0b9fc6 618 type0 = BASE;
de3fe21b
MCC
619
620 if (priv->ctrl.type == XC2028_FIRM_MTS)
621 type0 |= MTS;
622
ab0b9fc6 623 if (priv->bandwidth == 8)
de3fe21b
MCC
624 type0 |= F8MHZ;
625
626 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
627
628 rc = load_firmware(fe, type0, &std0);
ab0b9fc6 629 if (rc < 0) {
83fb340b
MCC
630 tuner_err("Error %d while loading generic firmware\n",
631 rc);
6cb45879 632 return rc;
de3fe21b 633 }
6cb45879 634
ab0b9fc6
MCC
635 priv->need_load_generic = 0;
636 priv->firm_type = 0;
637 if (priv->mode == T_DIGITAL_TV)
638 change_digital_bandwidth = 1;
701672eb
ML
639 }
640
83fb340b 641 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
701672eb
ML
642
643 if (change_digital_bandwidth) {
de3fe21b
MCC
644
645 /*FIXME: Should allow selecting between D2620 and D2633 */
646 type |= D2620;
647
648 /* FIXME: When should select a DTV78 firmware?
649 */
ab0b9fc6 650 switch (bandwidth) {
de3fe21b
MCC
651 case BANDWIDTH_8_MHZ:
652 type |= DTV8;
701672eb 653 break;
de3fe21b
MCC
654 case BANDWIDTH_7_MHZ:
655 type |= DTV7;
701672eb 656 break;
de3fe21b
MCC
657 case BANDWIDTH_6_MHZ:
658 /* FIXME: Should allow select also ATSC */
43efe702 659 type |= DTV6 | QAM;
701672eb
ML
660 break;
661
de3fe21b 662 default:
83fb340b 663 tuner_err("error: bandwidth not supported.\n");
701672eb 664 };
215b95ba 665 priv->bandwidth = bandwidth;
6cb45879
MCC
666 }
667
5403bbae
ML
668 if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
669 return 0;
670
de3fe21b 671 /* Load INIT1, if needed */
83fb340b 672 tuner_dbg("Load init1 firmware, if exists\n");
f380e1d2 673 type0 = BASE | INIT1;
de3fe21b
MCC
674 if (priv->ctrl.type == XC2028_FIRM_MTS)
675 type0 |= MTS;
676
677 /* FIXME: Should handle errors - if INIT1 found */
678 rc = load_firmware(fe, type0, &std0);
679
680 /* FIXME: Should add support for FM radio
681 */
682
683 if (priv->ctrl.type == XC2028_FIRM_MTS)
684 type |= MTS;
685
215b95ba 686 if (priv->firm_type & std) {
83fb340b 687 tuner_dbg("Std-specific firmware already loaded.\n");
6cb45879 688 return 0;
2e4160ca 689 }
6cb45879 690
a82200fb
MCC
691 /* Add audio hack to std mask */
692 std |= parse_audio_std_option();
693
de3fe21b 694 rc = load_firmware(fe, type, &std);
ab0b9fc6 695 if (rc < 0)
6cb45879
MCC
696 return rc;
697
f380e1d2 698 /* Load SCODE firmware, if exists */
83fb340b 699 tuner_dbg("Trying to load scode 0\n");
f380e1d2
MCC
700 type |= SCODE;
701
702 rc = load_scode(fe, type, &std, 0);
43efe702 703
7d58d111
CP
704 xc2028_get_reg(priv, 0x0004, &version);
705 xc2028_get_reg(priv, 0x0008, &hwmodel);
80b52208
MCC
706
707 tuner_info("Device is Xceive %d version %d.%d, "
708 "firmware version %d.%d\n",
709 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
710 (version & 0xf0) >> 4, version & 0xf);
6cb45879 711
ab0b9fc6 712 priv->firm_type = std;
6cb45879
MCC
713
714 return 0;
715}
716
215b95ba 717static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
6cb45879 718{
215b95ba 719 struct xc2028_data *priv = fe->tuner_priv;
7d58d111
CP
720 u16 frq_lock, signal = 0;
721 int rc;
3b20532c 722
83fb340b 723 tuner_dbg("%s called\n", __FUNCTION__);
6cb45879 724
215b95ba 725 mutex_lock(&priv->lock);
6cb45879 726
80b52208 727 /* Sync Lock Indicator */
7d58d111
CP
728 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
729 if (rc < 0 || frq_lock == 0)
3b20532c 730 goto ret;
6cb45879
MCC
731
732 /* Frequency is locked. Return signal quality */
733
80b52208 734 /* Get SNR of the video signal */
7d58d111
CP
735 rc = xc2028_get_reg(priv, 0x0040, &signal);
736 if (rc < 0)
737 signal = -frq_lock;
3b20532c
MCC
738
739ret:
215b95ba
MCC
740 mutex_unlock(&priv->lock);
741
742 *strength = signal;
6cb45879 743
7d58d111 744 return rc;
6cb45879
MCC
745}
746
747#define DIV 15625
748
ab0b9fc6
MCC
749static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
750 enum tuner_mode new_mode,
751 v4l2_std_id std, fe_bandwidth_t bandwidth)
6cb45879 752{
215b95ba 753 struct xc2028_data *priv = fe->tuner_priv;
ab0b9fc6
MCC
754 int rc = -EINVAL;
755 unsigned char buf[5];
756 u32 div, offset = 0;
6cb45879 757
83fb340b 758 tuner_dbg("%s called\n", __FUNCTION__);
215b95ba 759
de3fe21b
MCC
760 mutex_lock(&priv->lock);
761
d4e76681
MCC
762 /* HACK: It seems that specific firmware need to be reloaded
763 when freq is changed */
701672eb 764
ab0b9fc6 765 priv->firm_type = 0;
701672eb 766
6cb45879 767 /* Reset GPIO 1 */
215b95ba 768 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
ab0b9fc6 769 if (rc < 0)
215b95ba
MCC
770 goto ret;
771
6cb45879 772 msleep(10);
83fb340b 773 tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
6cb45879 774
ab0b9fc6 775 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
3b20532c 776 goto ret;
2e4160ca 777
ab0b9fc6 778 if (new_mode == T_DIGITAL_TV)
d4e76681 779 offset = 2750000;
2e4160ca 780
ab0b9fc6 781 div = (freq - offset + DIV / 2) / DIV;
2e4160ca 782
6cb45879 783 /* CMD= Set frequency */
de3fe21b 784
47cc5b78
CP
785 if (priv->version < 0x0202)
786 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
787 else
788 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
789 if (rc < 0)
790 goto ret;
de3fe21b 791
215b95ba 792 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
ab0b9fc6 793 if (rc < 0)
215b95ba 794 goto ret;
6cb45879
MCC
795
796 msleep(10);
701672eb 797
ab0b9fc6
MCC
798 buf[0] = 0xff & (div >> 24);
799 buf[1] = 0xff & (div >> 16);
800 buf[2] = 0xff & (div >> 8);
801 buf[3] = 0xff & (div);
802 buf[4] = 0;
6cb45879 803
47cc5b78 804 rc = i2c_send(priv, buf, sizeof(buf));
ab0b9fc6 805 if (rc < 0)
3b20532c 806 goto ret;
6cb45879
MCC
807 msleep(100);
808
ab0b9fc6 809 priv->frequency = freq;
215b95ba 810
83fb340b 811 tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
ab0b9fc6
MCC
812 buf[1], buf[2], buf[3], buf[4],
813 freq / 1000000, (freq % 1000000) / 10000);
3b20532c 814
ab0b9fc6 815 rc = 0;
6cb45879 816
215b95ba
MCC
817ret:
818 mutex_unlock(&priv->lock);
6cb45879 819
215b95ba 820 return rc;
701672eb
ML
821}
822
215b95ba 823static int xc2028_set_tv_freq(struct dvb_frontend *fe,
ab0b9fc6 824 struct analog_parameters *p)
6cb45879 825{
215b95ba 826 struct xc2028_data *priv = fe->tuner_priv;
6cb45879 827
83fb340b 828 tuner_dbg("%s called\n", __FUNCTION__);
6cb45879 829
ab0b9fc6
MCC
830 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
831 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
215b95ba 832}
6cb45879 833
215b95ba
MCC
834static int xc2028_set_params(struct dvb_frontend *fe,
835 struct dvb_frontend_parameters *p)
6cb45879 836{
215b95ba 837 struct xc2028_data *priv = fe->tuner_priv;
6cb45879 838
83fb340b 839 tuner_dbg("%s called\n", __FUNCTION__);
701672eb 840
215b95ba
MCC
841 /* FIXME: Only OFDM implemented */
842 if (fe->ops.info.type != FE_OFDM) {
83fb340b 843 tuner_err("DTV type not implemented.\n");
215b95ba 844 return -EINVAL;
6cb45879 845 }
6cb45879 846
215b95ba 847 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
ab0b9fc6
MCC
848 0 /* NOT USED */,
849 p->u.ofdm.bandwidth);
6cb45879 850
6cb45879 851}
701672eb 852
215b95ba 853static int xc2028_dvb_release(struct dvb_frontend *fe)
701672eb 854{
215b95ba
MCC
855 struct xc2028_data *priv = fe->tuner_priv;
856
83fb340b 857 tuner_dbg("%s called\n", __FUNCTION__);
701672eb 858
aa501be9
CP
859 mutex_lock(&xc2028_list_mutex);
860
215b95ba 861 priv->count--;
701672eb 862
de3fe21b 863 if (!priv->count) {
1808a698
MCC
864 list_del(&priv->xc2028_list);
865
ab0b9fc6 866 kfree(priv->ctrl.fname);
de3fe21b
MCC
867
868 free_firmware(priv);
ab0b9fc6 869 kfree(priv);
de3fe21b 870 }
701672eb 871
aa501be9
CP
872 mutex_unlock(&xc2028_list_mutex);
873
701672eb
ML
874 return 0;
875}
876
215b95ba 877static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
701672eb 878{
215b95ba 879 struct xc2028_data *priv = fe->tuner_priv;
701672eb 880
83fb340b 881 tuner_dbg("%s called\n", __FUNCTION__);
701672eb 882
215b95ba 883 *frequency = priv->frequency;
701672eb
ML
884
885 return 0;
886}
887
ab0b9fc6 888static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
de3fe21b
MCC
889{
890 struct xc2028_data *priv = fe->tuner_priv;
891 struct xc2028_ctrl *p = priv_cfg;
892
83fb340b 893 tuner_dbg("%s called\n", __FUNCTION__);
de3fe21b
MCC
894
895 priv->ctrl.type = p->type;
896
897 if (p->fname) {
ab0b9fc6 898 kfree(priv->ctrl.fname);
de3fe21b 899
ab0b9fc6 900 priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
de3fe21b
MCC
901 if (!priv->ctrl.fname)
902 return -ENOMEM;
903
904 free_firmware(priv);
905 strcpy(priv->ctrl.fname, p->fname);
906 }
907
ab0b9fc6 908 if (p->max_len > 0)
352fae1d
MCC
909 priv->max_len = p->max_len;
910
de3fe21b
MCC
911 return 0;
912}
913
215b95ba 914static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
701672eb 915 .info = {
ab0b9fc6
MCC
916 .name = "Xceive XC3028",
917 .frequency_min = 42000000,
918 .frequency_max = 864000000,
919 .frequency_step = 50000,
920 },
701672eb 921
de3fe21b 922 .set_config = xc2028_set_config,
215b95ba
MCC
923 .set_analog_params = xc2028_set_tv_freq,
924 .release = xc2028_dvb_release,
925 .get_frequency = xc2028_get_frequency,
926 .get_rf_strength = xc2028_signal,
927 .set_params = xc2028_set_params,
701672eb 928
701672eb
ML
929};
930
a37b4c9b 931void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
701672eb 932{
215b95ba 933 struct xc2028_data *priv;
a37b4c9b 934 void *video_dev;
701672eb 935
83fb340b 936 if (debug)
3157ecef 937 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
701672eb 938
a37b4c9b
ML
939 if (NULL == cfg->video_dev)
940 return NULL;
215b95ba 941
a37b4c9b 942 if (!fe) {
3157ecef 943 printk(KERN_ERR PREFIX ": No frontend!\n");
a37b4c9b 944 return NULL;
215b95ba
MCC
945 }
946
a37b4c9b
ML
947 video_dev = cfg->video_dev;
948
aa501be9
CP
949 mutex_lock(&xc2028_list_mutex);
950
215b95ba 951 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
a37b4c9b
ML
952 if (priv->video_dev == cfg->video_dev) {
953 video_dev = NULL;
954 break;
955 }
215b95ba
MCC
956 }
957
a37b4c9b 958 if (video_dev) {
215b95ba 959 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
aa501be9
CP
960 if (priv == NULL) {
961 mutex_unlock(&xc2028_list_mutex);
a37b4c9b 962 return NULL;
aa501be9 963 }
3b20532c 964
ab0b9fc6
MCC
965 priv->bandwidth = BANDWIDTH_6_MHZ;
966 priv->need_load_generic = 1;
215b95ba 967 priv->mode = T_UNINITIALIZED;
a37b4c9b
ML
968 priv->i2c_props.addr = cfg->i2c_addr;
969 priv->i2c_props.adap = cfg->i2c_adap;
215b95ba 970 priv->video_dev = video_dev;
a37b4c9b 971 priv->tuner_callback = cfg->callback;
de3fe21b
MCC
972 priv->max_len = 13;
973
215b95ba
MCC
974 mutex_init(&priv->lock);
975
ab0b9fc6 976 list_add_tail(&priv->xc2028_list, &xc2028_list);
215b95ba 977 }
a37b4c9b
ML
978
979 fe->tuner_priv = priv;
1808a698 980 priv->count++;
215b95ba
MCC
981
982 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
ab0b9fc6 983 sizeof(xc2028_dvb_tuner_ops));
215b95ba
MCC
984
985 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
986
aa501be9
CP
987 mutex_unlock(&xc2028_list_mutex);
988
a37b4c9b 989 return fe;
215b95ba 990}
a37b4c9b 991
701672eb
ML
992EXPORT_SYMBOL(xc2028_attach);
993
215b95ba 994MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
983d214e 995MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
215b95ba
MCC
996MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
997MODULE_LICENSE("GPL");