blob: a6b05dfd670327ffc1e00e2c7b27e80738b49000 [file] [log] [blame]
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001/* tuner-xc2028
2 *
3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03004 *
Michel Ludwig701672e2007-07-18 10:29:10 -03005 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03007 *
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03008 * 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>
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -030014#include <linux/videodev2.h>
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030015#include <linux/delay.h>
Michel Ludwig701672e2007-07-18 10:29:10 -030016#include <media/tuner.h>
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -030017#include <linux/mutex.h>
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030018#include "tuner-i2c.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030019#include "tuner-xc2028.h"
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030020#include "tuner-xc2028-types.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030021
Michel Ludwig701672e2007-07-18 10:29:10 -030022#include <linux/dvb/frontend.h>
23#include "dvb_frontend.h"
24
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -030025
Hans Verkuil9dd659d2007-11-04 11:03:36 -030026#define PREFIX "xc2028"
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030027
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030028static int debug;
29module_param(debug, int, 0644);
30MODULE_PARM_DESC(debug, "enable verbose debug messages");
31
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -030032static 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
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030046static LIST_HEAD(xc2028_list);
Chris Pascoeaa501be2007-11-19 04:45:38 -030047static DEFINE_MUTEX(xc2028_list_mutex);
48
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030049/* struct for storing firmware table */
50struct firmware_description {
51 unsigned int type;
52 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030053 __u16 int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030054 unsigned char *ptr;
55 unsigned int size;
56};
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030057
Chris Pascoee0f0b372007-11-19 11:22:03 -030058struct firmware_properties {
59 unsigned int type;
60 v4l2_std_id id;
61 v4l2_std_id std_req;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030062 __u16 int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -030063 unsigned int scode_table;
64 int scode_nr;
65};
66
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030067struct xc2028_data {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030068 struct list_head xc2028_list;
69 struct tuner_i2c_props i2c_props;
70 int (*tuner_callback) (void *dev,
71 int command, int arg);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030072 void *video_dev;
73 int count;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030074 __u32 frequency;
75
76 struct firmware_description *firm;
77 int firm_size;
Chris Pascoe06fd82d2007-11-19 06:06:08 -030078 __u16 firm_version;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030079
Chris Pascoe8bf799a2007-11-19 11:35:45 -030080 __u16 hwmodel;
81 __u16 hwvers;
82
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030083 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030084
Chris Pascoee0f0b372007-11-19 11:22:03 -030085 struct firmware_properties cur_fw;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030086
87 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030088};
89
Chris Pascoe47cc5b72007-11-19 04:14:23 -030090#define i2c_send(priv, buf, size) ({ \
91 int _rc; \
92 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
93 if (size != _rc) \
94 tuner_info("i2c output error: rc = %d (should be %d)\n",\
95 _rc, (int)size); \
96 _rc; \
97})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030098
Chris Pascoe47cc5b72007-11-19 04:14:23 -030099#define i2c_rcv(priv, buf, size) ({ \
100 int _rc; \
101 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
102 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300103 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300104 _rc, (int)size); \
105 _rc; \
106})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300107
Chris Pascoe7d58d112007-11-19 04:31:58 -0300108#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
109 int _rc; \
110 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
111 ibuf, isize); \
112 if (isize != _rc) \
113 tuner_err("i2c input error: rc = %d (should be %d)\n", \
114 _rc, (int)isize); \
115 _rc; \
116})
117
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300118#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300119 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300120 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300121 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300122 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300123 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300124 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
125 } else \
126 msleep(10); \
127 _rc; \
128})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300129
Chris Pascoe7d58d112007-11-19 04:31:58 -0300130static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300131{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300132 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300133 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300134
Chris Pascoe7d58d112007-11-19 04:31:58 -0300135 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300136
Chris Pascoe7d58d112007-11-19 04:31:58 -0300137 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300138 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300139
Chris Pascoe7d58d112007-11-19 04:31:58 -0300140 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
141 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300142
Chris Pascoe7d58d112007-11-19 04:31:58 -0300143 *val = (ibuf[1]) | (ibuf[0] << 8);
144 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300145}
146
Chris Pascoee0262682007-12-02 06:30:50 -0300147#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
148void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300149{
150 if (type & BASE)
151 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300152 if (type & INIT1)
153 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300154 if (type & F8MHZ)
155 printk("F8MHZ ");
156 if (type & MTS)
157 printk("MTS ");
158 if (type & D2620)
159 printk("D2620 ");
160 if (type & D2633)
161 printk("D2633 ");
162 if (type & DTV6)
163 printk("DTV6 ");
164 if (type & QAM)
165 printk("QAM ");
166 if (type & DTV7)
167 printk("DTV7 ");
168 if (type & DTV78)
169 printk("DTV78 ");
170 if (type & DTV8)
171 printk("DTV8 ");
172 if (type & FM)
173 printk("FM ");
174 if (type & INPUT1)
175 printk("INPUT1 ");
176 if (type & LCD)
177 printk("LCD ");
178 if (type & NOGD)
179 printk("NOGD ");
180 if (type & MONO)
181 printk("MONO ");
182 if (type & ATSC)
183 printk("ATSC ");
184 if (type & IF)
185 printk("IF ");
186 if (type & LG60)
187 printk("LG60 ");
188 if (type & ATI638)
189 printk("ATI638 ");
190 if (type & OREN538)
191 printk("OREN538 ");
192 if (type & OREN36)
193 printk("OREN36 ");
194 if (type & TOYOTA388)
195 printk("TOYOTA388 ");
196 if (type & TOYOTA794)
197 printk("TOYOTA794 ");
198 if (type & DIBCOM52)
199 printk("DIBCOM52 ");
200 if (type & ZARLINK456)
201 printk("ZARLINK456 ");
202 if (type & CHINA)
203 printk("CHINA ");
204 if (type & F6MHZ)
205 printk("F6MHZ ");
206 if (type & INPUT2)
207 printk("INPUT2 ");
208 if (type & SCODE)
209 printk("SCODE ");
Chris Pascoee0262682007-12-02 06:30:50 -0300210 if (type & HAS_IF)
211 printk("HAS_IF_%d ", int_freq);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300212}
213
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300214static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300215{
Chris Pascoee155d902007-11-19 04:16:47 -0300216 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300217 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300218 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300219 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300220 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300221 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300222 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300223 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300224 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300225 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300226 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300227 return V4L2_STD_NICAM_B;
228
229 return 0;
230}
231
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300232static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300233{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300234 int i;
235
236 if (!priv->firm)
237 return;
238
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300239 for (i = 0; i < priv->firm_size; i++)
240 kfree(priv->firm[i].ptr);
241
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300242 kfree(priv->firm);
243
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300244 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300245 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300246
247 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300248}
249
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300250static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300251{
252 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300253 const struct firmware *fw = NULL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300254 unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300255 int rc = 0;
256 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300257 char name[33];
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300258
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300259 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300260
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300261 tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300262 rc = request_firmware(&fw, priv->ctrl.fname,
263 &priv->i2c_props.adap->dev);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300264 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300265 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300266 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300267 priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300268 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300269 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300270 rc, priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300271
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300272 return rc;
273 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300274 p = fw->data;
275 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300276
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300277 if (fw->size < sizeof(name) - 1 + 2 + 2) {
278 tuner_err("Error: firmware file %s has invalid size!\n",
279 priv->ctrl.fname);
280 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300281 }
282
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300283 memcpy(name, p, sizeof(name) - 1);
284 name[sizeof(name) - 1] = 0;
285 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300286
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300287 priv->firm_version = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300288 p += 2;
289
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300290 n_array = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300291 p += 2;
292
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300293 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
294 n_array, priv->ctrl.fname, name,
295 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300296
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300297 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300298 if (priv->firm == NULL) {
299 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300300 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300301 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300302 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300303 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300304
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300305 n = -1;
306 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300307 __u32 type, size;
308 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300309 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300310
311 n++;
312 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300313 tuner_err("More firmware images in file than "
314 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300315 goto corrupt;
316 }
317
318 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300319 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300320 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300321 goto corrupt;
322 }
323
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300324 type = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300325 p += sizeof(type);
326
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300327 id = le64_to_cpu(*(v4l2_std_id *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300328 p += sizeof(id);
329
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300330 if (type & HAS_IF) {
331 int_freq = le16_to_cpu(*(__u16 *) p);
332 p += sizeof(int_freq);
333 }
334
Michel Ludwig2fc580f2007-11-16 07:19:35 -0300335 size = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300336 p += sizeof(size);
337
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300338 if ((!size) || (size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300339 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300340 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300341 printk("(%x), id %llx is corrupted "
342 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300343 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300344 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300345 goto corrupt;
346 }
347
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300348 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300349 if (priv->firm[n].ptr == NULL) {
350 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300351 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300352 goto err;
353 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300354 tuner_dbg("Reading firmware type ");
355 if (debug) {
Chris Pascoee0262682007-12-02 06:30:50 -0300356 dump_firm_type_and_int_freq(type, int_freq);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300357 printk("(%x), id %llx, size=%d.\n",
Chris Pascoee0262682007-12-02 06:30:50 -0300358 type, (unsigned long long)id, size);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300359 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300360
361 memcpy(priv->firm[n].ptr, p, size);
362 priv->firm[n].type = type;
363 priv->firm[n].id = id;
364 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300365 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300366
367 p += size;
368 }
369
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300370 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300371 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300372 goto corrupt;
373 }
374
375 goto done;
376
377corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300378 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300379 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300380
381err:
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300382 tuner_info("Releasing partially loaded firmware file.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300383 free_firmware(priv);
384
385done:
386 release_firmware(fw);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300387 if (rc == 0)
388 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300389
390 return rc;
391}
392
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300393static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
394 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300395{
396 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoeb1535292007-11-19 10:04:06 -0300397 int i, best_i = -1, best_nr_matches = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300398
Chris Pascoeb1535292007-11-19 10:04:06 -0300399 tuner_dbg("%s called, want type=", __FUNCTION__);
400 if (debug) {
401 dump_firm_type(type);
402 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
403 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300404
405 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300406 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300407 return -EINVAL;
408 }
409
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300410 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300411 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300412
Chris Pascoee0f0b372007-11-19 11:22:03 -0300413 if (type & BASE)
414 type &= BASE_TYPES;
415 else if (type & SCODE)
416 type &= SCODE_TYPES;
417 else if (type & DTV_TYPES)
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300418 type &= DTV_TYPES;
419 else if (type & STD_SPECIFIC_TYPES)
420 type &= STD_SPECIFIC_TYPES;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300421
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300422 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300423 for (i = 0; i < priv->firm_size; i++) {
424 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300425 goto found;
426 }
427
428 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300429 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300430 v4l2_std_id match_mask;
431 int nr_matches;
432
433 if (type != priv->firm[i].type)
434 continue;
435
436 match_mask = *id & priv->firm[i].id;
437 if (!match_mask)
438 continue;
439
440 if ((*id & match_mask) == *id)
441 goto found; /* Supports all the requested standards */
442
443 nr_matches = hweight64(match_mask);
444 if (nr_matches > best_nr_matches) {
445 best_nr_matches = nr_matches;
446 best_i = i;
447 }
448 }
449
450 if (best_nr_matches > 0) {
451 tuner_dbg("Selecting best matching firmware (%d bits) for "
452 "type=", best_nr_matches);
453 dump_firm_type(type);
454 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
455 i = best_i;
456 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300457 }
458
459 /*FIXME: Would make sense to seek for type "hint" match ? */
460
Chris Pascoeb1535292007-11-19 10:04:06 -0300461 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300462 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300463
464found:
465 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300466
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300467ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300468 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300469 if (debug) {
470 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300471 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300472 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300473 return i;
474}
475
476static int load_firmware(struct dvb_frontend *fe, unsigned int type,
477 v4l2_std_id *id)
478{
479 struct xc2028_data *priv = fe->tuner_priv;
480 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300481 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300482
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300483 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300484
485 pos = seek_firmware(fe, type, id);
486 if (pos < 0)
487 return pos;
488
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300489 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300490 dump_firm_type(priv->firm[pos].type);
491 printk("(%x), id %016llx.\n", priv->firm[pos].type,
492 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300493
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300494 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300495 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300496
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300497 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300498 __u16 size;
499
500 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300501 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300502 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300503 return -EINVAL;
504 }
505
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300506 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300507 p += sizeof(size);
508
509 if (size == 0xffff)
510 return 0;
511
512 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300513 /* Special callback command received */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300514 rc = priv->tuner_callback(priv->video_dev,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300515 XC2028_TUNER_RESET, 0);
516 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300517 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300518 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300519 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300520 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300521 continue;
522 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300523 if (size >= 0xff00) {
524 switch (size) {
525 case 0xff00:
526 rc = priv->tuner_callback(priv->video_dev,
527 XC2028_RESET_CLK, 0);
528 if (rc < 0) {
529 tuner_err("Error at RESET code %d\n",
530 (*p) & 0x7f);
531 return -EINVAL;
532 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300533 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300534 default:
535 tuner_info("Invalid RESET code %d\n",
536 size & 0x7f);
537 return -EINVAL;
538
539 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300540 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300541 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300542
543 /* Checks for a sleep command */
544 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300545 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300546 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300547 }
548
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300549 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300550 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300551 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300552 return -EINVAL;
553 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300554
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300555 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300556 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300557 size--;
558
559 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300560 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300561 int len = (size < priv->ctrl.max_len - 1) ?
562 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300563
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300564 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300565
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300566 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300567 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300568 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300569 return -EINVAL;
570 }
571
572 p += len;
573 size -= len;
574 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300575 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300576 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300577}
578
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300579static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300580 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300581{
582 struct xc2028_data *priv = fe->tuner_priv;
583 int pos, rc;
584 unsigned char *p;
585
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300586 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300587
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300588 if (!int_freq) {
589 pos = seek_firmware(fe, type, id);
590 if (pos < 0)
591 return pos;
592 } else {
593 for (pos = 0; pos < priv->firm_size; pos++) {
594 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300595 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300596 break;
597 }
598 if (pos == priv->firm_size)
599 return -ENOENT;
600 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300601
602 p = priv->firm[pos].ptr;
603
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300604 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300605 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
606 return -EINVAL;
607 p += 12 * scode;
608 } else {
609 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
610 * has a 2-byte size header in the firmware format. */
611 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
612 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
613 return -EINVAL;
614 p += 14 * scode + 2;
615 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300616
Chris Pascoed7b22c52007-11-19 10:12:45 -0300617 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300618 dump_firm_type_and_int_freq(priv->firm[pos].type,
619 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300620 printk("(%x), id %016llx.\n", priv->firm[pos].type,
621 (unsigned long long)*id);
622
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300623 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300624 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
625 else
626 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
627 if (rc < 0)
628 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300629
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300630 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300631 if (rc < 0)
632 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300633
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300634 rc = send_seq(priv, {0x00, 0x8c});
635 if (rc < 0)
636 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300637
638 return 0;
639}
640
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300641static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300642 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300643{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300644 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300645 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300646 int rc = 0, is_retry = 0;
647 u16 version, hwmodel;
648 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300649
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300650 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300651
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300652 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300653 if (!priv->ctrl.fname) {
654 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300655 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300656 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300657
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300658 rc = load_all_firmwares(fe);
659 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300660 return rc;
661 }
662
Mauro Carvalho Chehab5add9a62007-11-22 12:08:53 -0300663 if (priv->ctrl.mts)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300664 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300665
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300666retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300667 new_fw.type = type;
668 new_fw.id = std;
669 new_fw.std_req = std;
670 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
671 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300672 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300673
674 tuner_dbg("checking firmware, user requested type=");
675 if (debug) {
676 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300677 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300678 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300679 if (!int_freq) {
680 printk("scode_tbl ");
681 dump_firm_type(priv->ctrl.scode_table);
682 printk("(%x), ", priv->ctrl.scode_table);
683 } else
684 printk("int_freq %d, ", new_fw.int_freq);
685 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300686 }
687
688 /* No need to reload base firmware if it matches */
689 if (((BASE | new_fw.type) & BASE_TYPES) ==
690 (priv->cur_fw.type & BASE_TYPES)) {
691 tuner_dbg("BASE firmware not changed.\n");
692 goto skip_base;
693 }
694
695 /* Updating BASE - forget about all currently loaded firmware */
696 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
697
698 /* Reset is needed before loading firmware */
699 rc = priv->tuner_callback(priv->video_dev,
700 XC2028_TUNER_RESET, 0);
701 if (rc < 0)
702 goto fail;
703
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300704 /* BASE firmwares are all std0 */
705 std0 = 0;
706 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300707 if (rc < 0) {
708 tuner_err("Error %d while loading base firmware\n",
709 rc);
710 goto fail;
711 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300712
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300713 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300714 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300715
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300716 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300717 if (rc == -ENOENT)
718 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
719 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300720 if (rc < 0 && rc != -ENOENT) {
721 tuner_err("Error %d while loading init1 firmware\n",
722 rc);
723 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300724 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300725
Chris Pascoee0f0b372007-11-19 11:22:03 -0300726skip_base:
727 /*
728 * No need to reload standard specific firmware if base firmware
729 * was not reloaded and requested video standards have not changed.
730 */
731 if (priv->cur_fw.type == (BASE | new_fw.type) &&
732 priv->cur_fw.std_req == std) {
733 tuner_dbg("Std-specific firmware already loaded.\n");
734 goto skip_std_specific;
735 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300736
Chris Pascoee0f0b372007-11-19 11:22:03 -0300737 /* Reloading std-specific firmware forces a SCODE update */
738 priv->cur_fw.scode_table = 0;
739
Chris Pascoee0f0b372007-11-19 11:22:03 -0300740 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300741 if (rc == -ENOENT)
742 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
743
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300744 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300745 goto fail;
746
747skip_std_specific:
748 if (priv->cur_fw.scode_table == new_fw.scode_table &&
749 priv->cur_fw.scode_nr == new_fw.scode_nr) {
750 tuner_dbg("SCODE firmware already loaded.\n");
751 goto check_device;
752 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300753
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300754 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300755 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300756
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300757 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
758 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300759
Chris Pascoee0f0b372007-11-19 11:22:03 -0300760check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300761 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
762 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
763 tuner_err("Unable to read tuner registers.\n");
764 goto fail;
765 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300766
767 tuner_info("Device is Xceive %d version %d.%d, "
768 "firmware version %d.%d\n",
769 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
770 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300771
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300772 /* Check firmware version against what we downloaded. */
773 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
774 tuner_err("Incorrect readback of firmware version.\n");
775 goto fail;
776 }
777
778 /* Check that the tuner hardware model remains consistent over time. */
779 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
780 priv->hwmodel = hwmodel;
781 priv->hwvers = version & 0xff00;
782 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
783 priv->hwvers != (version & 0xff00)) {
784 tuner_err("Read invalid device hardware information - tuner "
785 "hung?\n");
786 goto fail;
787 }
788
Chris Pascoee0f0b372007-11-19 11:22:03 -0300789 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
790
791 /*
792 * By setting BASE in cur_fw.type only after successfully loading all
793 * firmwares, we can:
794 * 1. Identify that BASE firmware with type=0 has been loaded;
795 * 2. Tell whether BASE firmware was just changed the next time through.
796 */
797 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300798
799 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300800
801fail:
802 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300803 if (!is_retry) {
804 msleep(50);
805 is_retry = 1;
806 tuner_dbg("Retrying firmware load\n");
807 goto retry;
808 }
809
Chris Pascoee0f0b372007-11-19 11:22:03 -0300810 if (rc == -ENOENT)
811 rc = -EINVAL;
812 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300813}
814
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300815static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300816{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300817 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300818 u16 frq_lock, signal = 0;
819 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300820
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300821 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300822
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300823 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300824
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300825 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300826 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
827 if (rc < 0 || frq_lock == 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300828 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300829
830 /* Frequency is locked. Return signal quality */
831
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300832 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300833 rc = xc2028_get_reg(priv, 0x0040, &signal);
834 if (rc < 0)
835 signal = -frq_lock;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300836
837ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300838 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300839
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300840 *strength = signal;
841
Chris Pascoe7d58d112007-11-19 04:31:58 -0300842 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300843}
844
845#define DIV 15625
846
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300847static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300848 enum tuner_mode new_mode,
849 unsigned int type,
850 v4l2_std_id std,
851 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300852{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300853 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300854 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300855 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300856 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300857
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300858 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300859
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300860 mutex_lock(&priv->lock);
861
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300862 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300863
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300864 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300865 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300866
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300867 /* On some cases xc2028 can disable video output, if
868 * very weak signals are received. By sending a soft
869 * reset, this is re-enabled. So, it is better to always
870 * send a soft reset before changing channels, to be sure
871 * that xc2028 will be in a safe state.
872 * Maybe this might also be needed for DTV.
873 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300874 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300875 rc = send_seq(priv, {0x00, 0x00});
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300876 } else {
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300877 offset = 2750000;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300878 if (priv->cur_fw.type & DTV7)
Chris Pascoea44f1c42007-11-19 06:35:26 -0300879 offset -= 500000;
880 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300881
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300882 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300883
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300884 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300885 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300886 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
887 else
888 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
889 if (rc < 0)
890 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300891
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300892 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300893 if (rc < 0)
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300894 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300895
896 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300897
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300898 buf[0] = 0xff & (div >> 24);
899 buf[1] = 0xff & (div >> 16);
900 buf[2] = 0xff & (div >> 8);
901 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300902
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300903 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300904 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300905 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300906 msleep(100);
907
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300908 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300909
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300910 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
911 buf[0], buf[1], buf[2], buf[3],
912 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300913
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300914 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300915
916ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300917 mutex_unlock(&priv->lock);
918
919 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300920}
921
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300922static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300923 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300924{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300925 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300926 unsigned int type=0;
927
928 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -0300929
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300930 if (p->mode == V4L2_TUNER_RADIO) {
931 type |= FM;
932 if (priv->ctrl.input1)
933 type |= INPUT1;
934 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300935 T_ANALOG_TV, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300936 }
937
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -0300938 /* if std is not defined, choose one */
939 if (!p->std)
940 p->std = V4L2_STD_MN;
941
942 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300943 if (!(p->std & V4L2_STD_MN))
944 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -0300945
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300946 /* Add audio hack to std mask */
947 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300948
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300949 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300950 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -0300951}
952
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300953static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -0300954 struct dvb_frontend_parameters *p)
955{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300956 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300957 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300958 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -0300959 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -0300960
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300961 tuner_dbg("%s called\n", __FUNCTION__);
Michel Ludwig701672e2007-07-18 10:29:10 -0300962
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300963 if (priv->ctrl.d2633)
964 type |= D2633;
965 else
966 type |= D2620;
967
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300968 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300969 case FE_OFDM:
970 bw = p->u.ofdm.bandwidth;
971 break;
972 case FE_QAM:
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -0300973 tuner_info("WARN: There are some reports that "
974 "QAM 6 MHz doesn't work.\n"
975 "If this works for you, please report by "
976 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300977 bw = BANDWIDTH_6_MHZ;
978 type |= QAM;
979 break;
980 case FE_ATSC:
981 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -0300982 /* The only ATSC firmware (at least on v2.7) is D2633,
983 so overrides ctrl->d2633 */
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300984 type |= ATSC| D2633;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -0300985 type &= ~D2620;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300986 break;
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -0300987 /* DVB-S is not supported */
988 default:
989 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300990 }
991
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300992 /* FIXME:
993 There are two Scodes that will never be selected:
994 DTV78 ZARLINK456, DTV78 DIBCOM52
995 When it should opt for DTV78 instead of DTV7 or DTV8?
996 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300997 switch (bw) {
998 case BANDWIDTH_8_MHZ:
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300999 type |= DTV8 | F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001000 break;
1001 case BANDWIDTH_7_MHZ:
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001002 type |= DTV7 | F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001003 break;
1004 case BANDWIDTH_6_MHZ:
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001005 type |= DTV6 ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001006 break;
1007 default:
1008 tuner_err("error: bandwidth not supported.\n");
1009 };
1010
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001011 /* All S-code tables need a 200kHz shift */
1012 if (priv->ctrl.demod)
Chris Pascoead35ce92007-12-02 06:36:42 -03001013 demod = priv->ctrl.demod + 200;
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001014
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001015 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001016 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001017}
1018
Chris Pascoe45819c32007-11-19 11:41:20 -03001019static int xc2028_sleep(struct dvb_frontend *fe)
1020{
1021 struct xc2028_data *priv = fe->tuner_priv;
1022 int rc = 0;
1023
1024 tuner_dbg("%s called\n", __FUNCTION__);
1025
1026 mutex_lock(&priv->lock);
1027
1028 if (priv->firm_version < 0x0202)
1029 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1030 else
1031 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1032
1033 priv->cur_fw.type = 0; /* need firmware reload */
1034
1035 mutex_unlock(&priv->lock);
1036
1037 return rc;
1038}
1039
1040
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001041static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001042{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001043 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001044
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001045 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001046
Chris Pascoeaa501be2007-11-19 04:45:38 -03001047 mutex_lock(&xc2028_list_mutex);
1048
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001049 priv->count--;
1050
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001051 if (!priv->count) {
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001052 list_del(&priv->xc2028_list);
1053
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001054 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001055
1056 free_firmware(priv);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001057 kfree(priv);
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001058 fe->tuner_priv = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001059 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001060
Chris Pascoeaa501be2007-11-19 04:45:38 -03001061 mutex_unlock(&xc2028_list_mutex);
1062
Michel Ludwig701672e2007-07-18 10:29:10 -03001063 return 0;
1064}
1065
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001066static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001067{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001068 struct xc2028_data *priv = fe->tuner_priv;
1069
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001070 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001071
1072 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001073
1074 return 0;
1075}
1076
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001077static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001078{
1079 struct xc2028_data *priv = fe->tuner_priv;
1080 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001081 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001082
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001083 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001084
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001085 mutex_lock(&priv->lock);
1086
Chris Pascoe0a196b62007-11-19 09:29:59 -03001087 kfree(priv->ctrl.fname);
1088 free_firmware(priv);
1089
1090 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1091 priv->ctrl.fname = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001092
1093 if (p->fname) {
Chris Pascoe0a196b62007-11-19 09:29:59 -03001094 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1095 if (priv->ctrl.fname == NULL)
1096 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001097 }
1098
Chris Pascoe0a196b62007-11-19 09:29:59 -03001099 if (priv->ctrl.max_len < 9)
1100 priv->ctrl.max_len = 13;
Mauro Carvalho Chehab352fae12007-11-01 16:56:26 -03001101
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001102 mutex_unlock(&priv->lock);
1103
Chris Pascoe0a196b62007-11-19 09:29:59 -03001104 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001105}
1106
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001107static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001108 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001109 .name = "Xceive XC3028",
1110 .frequency_min = 42000000,
1111 .frequency_max = 864000000,
1112 .frequency_step = 50000,
1113 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001114
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001115 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001116 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001117 .release = xc2028_dvb_release,
1118 .get_frequency = xc2028_get_frequency,
1119 .get_rf_strength = xc2028_signal,
1120 .set_params = xc2028_set_params,
Chris Pascoe45819c32007-11-19 11:41:20 -03001121 .sleep = xc2028_sleep,
Michel Ludwig701672e2007-07-18 10:29:10 -03001122
Michel Ludwig701672e2007-07-18 10:29:10 -03001123};
1124
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001125void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001126{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001127 struct xc2028_data *priv;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001128 void *video_dev;
Michel Ludwig701672e2007-07-18 10:29:10 -03001129
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001130 if (debug)
Chris Pascoe3157ece2007-11-19 04:34:29 -03001131 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001132
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001133 if (NULL == cfg || NULL == cfg->video_dev)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001134 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001135
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001136 if (!fe) {
Chris Pascoe3157ece2007-11-19 04:34:29 -03001137 printk(KERN_ERR PREFIX ": No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001138 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001139 }
1140
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001141 video_dev = cfg->video_dev;
1142
Chris Pascoeaa501be2007-11-19 04:45:38 -03001143 mutex_lock(&xc2028_list_mutex);
1144
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001145 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001146 if (priv->video_dev == cfg->video_dev) {
1147 video_dev = NULL;
1148 break;
1149 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001150 }
1151
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001152 if (video_dev) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001153 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Chris Pascoeaa501be2007-11-19 04:45:38 -03001154 if (priv == NULL) {
1155 mutex_unlock(&xc2028_list_mutex);
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001156 return NULL;
Chris Pascoeaa501be2007-11-19 04:45:38 -03001157 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001158
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001159 priv->i2c_props.addr = cfg->i2c_addr;
1160 priv->i2c_props.adap = cfg->i2c_adap;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001161 priv->video_dev = video_dev;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001162 priv->tuner_callback = cfg->callback;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001163 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001164
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001165 mutex_init(&priv->lock);
1166
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001167 list_add_tail(&priv->xc2028_list, &xc2028_list);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001168 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001169
1170 fe->tuner_priv = priv;
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001171 priv->count++;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001172
1173 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001174 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001175
1176 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001177
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001178 if (cfg->ctrl)
1179 xc2028_set_config(fe, cfg->ctrl);
1180
Chris Pascoeaa501be2007-11-19 04:45:38 -03001181 mutex_unlock(&xc2028_list_mutex);
1182
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001183 return fe;
Michel Ludwig701672e2007-07-18 10:29:10 -03001184}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001185
Michel Ludwig701672e2007-07-18 10:29:10 -03001186EXPORT_SYMBOL(xc2028_attach);
1187
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001188MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001189MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001190MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1191MODULE_LICENSE("GPL");