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