[XFRM]: skb_cow_data() does not set proper owner for new skbs.
[linux-2.6/verdex.git] / drivers / media / video / tuner-core.c
blob6212388edb75b0035b19761eb00bfec503b0a4ce
1 /*
2 * $Id: tuner-core.c,v 1.5 2005/02/15 15:59:35 kraxel Exp $
4 * i2c tv tuner chip device driver
5 * core core, i.e. kernel interfaces, registering and so on
6 */
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/timer.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/poll.h>
18 #include <linux/i2c.h>
19 #include <linux/types.h>
20 #include <linux/videodev.h>
21 #include <linux/init.h>
23 #include <media/tuner.h>
24 #include <media/audiochip.h>
26 #define UNSET (-1U)
28 /* standard i2c insmod options */
29 static unsigned short normal_i2c[] = {
30 0x4b, /* tda8290 */
31 I2C_CLIENT_END
33 static unsigned short normal_i2c_range[] = {
34 0x60, 0x6f,
35 I2C_CLIENT_END
37 I2C_CLIENT_INSMOD;
39 /* insmod options used at init time => read/only */
40 static unsigned int addr = 0;
41 module_param(addr, int, 0444);
43 /* insmod options used at runtime => read/write */
44 unsigned int tuner_debug = 0;
45 module_param(tuner_debug, int, 0644);
47 static unsigned int tv_range[2] = { 44, 958 };
48 static unsigned int radio_range[2] = { 65, 108 };
50 module_param_array(tv_range, int, NULL, 0644);
51 module_param_array(radio_range, int, NULL, 0644);
53 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
54 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
55 MODULE_LICENSE("GPL");
57 static int this_adap;
59 static struct i2c_driver driver;
60 static struct i2c_client client_template;
62 /* ---------------------------------------------------------------------- */
64 // Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz
65 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
67 struct tuner *t = i2c_get_clientdata(c);
69 if (t->type == UNSET) {
70 tuner_info("tuner type not set\n");
71 return;
73 if (NULL == t->tv_freq) {
74 tuner_info("Huh? tv_set is NULL?\n");
75 return;
77 if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
78 /* FIXME: better do that chip-specific, but
79 right now we don't have that in the config
80 struct and this way is still better than no
81 check at all */
82 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
83 freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
84 return;
86 t->tv_freq(c,freq);
89 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
91 struct tuner *t = i2c_get_clientdata(c);
93 if (t->type == UNSET) {
94 tuner_info("tuner type not set\n");
95 return;
97 if (NULL == t->radio_freq) {
98 tuner_info("no radio tuning for this one, sorry.\n");
99 return;
101 if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
102 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
103 freq/16,freq%16*100/16,
104 radio_range[0],radio_range[1]);
105 return;
107 t->radio_freq(c,freq);
110 static void set_freq(struct i2c_client *c, unsigned long freq)
112 struct tuner *t = i2c_get_clientdata(c);
114 switch (t->mode) {
115 case V4L2_TUNER_RADIO:
116 tuner_dbg("radio freq set to %lu.%02lu\n",
117 freq/16,freq%16*100/16);
118 set_radio_freq(c,freq);
119 break;
120 case V4L2_TUNER_ANALOG_TV:
121 case V4L2_TUNER_DIGITAL_TV:
122 tuner_dbg("tv freq set to %lu.%02lu\n",
123 freq/16,freq%16*100/16);
124 set_tv_freq(c, freq);
125 break;
127 t->freq = freq;
130 static void set_type(struct i2c_client *c, unsigned int type)
132 struct tuner *t = i2c_get_clientdata(c);
134 /* sanity check */
135 if (type == UNSET || type == TUNER_ABSENT)
136 return;
137 if (type >= tuner_count)
138 return;
140 if (NULL == t->i2c.dev.driver) {
141 /* not registered yet */
142 t->type = type;
143 return;
145 if (t->initialized)
146 /* run only once */
147 return;
149 t->initialized = 1;
150 t->type = type;
151 switch (t->type) {
152 case TUNER_MT2032:
153 microtune_init(c);
154 break;
155 case TUNER_PHILIPS_TDA8290:
156 tda8290_init(c);
157 break;
158 default:
159 default_tuner_init(c);
160 break;
164 static char pal[] = "-";
165 module_param_string(pal, pal, sizeof(pal), 0644);
167 static int tuner_fixup_std(struct tuner *t)
169 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
170 /* get more precise norm info from insmod option */
171 switch (pal[0]) {
172 case 'b':
173 case 'B':
174 case 'g':
175 case 'G':
176 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
177 t->std = V4L2_STD_PAL_BG;
178 break;
179 case 'i':
180 case 'I':
181 tuner_dbg("insmod fixup: PAL => PAL-I\n");
182 t->std = V4L2_STD_PAL_I;
183 break;
184 case 'd':
185 case 'D':
186 case 'k':
187 case 'K':
188 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
189 t->std = V4L2_STD_PAL_DK;
190 break;
193 return 0;
196 /* ---------------------------------------------------------------------- */
198 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
200 struct tuner *t;
202 if (this_adap > 0)
203 return -1;
204 this_adap++;
206 client_template.adapter = adap;
207 client_template.addr = addr;
209 t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
210 if (NULL == t)
211 return -ENOMEM;
212 memset(t,0,sizeof(struct tuner));
213 memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
214 i2c_set_clientdata(&t->i2c, t);
215 t->type = UNSET;
216 t->radio_if2 = 10700*1000; // 10.7MHz - FM radio
218 i2c_attach_client(&t->i2c);
219 tuner_info("chip found @ 0x%x (%s)\n",
220 addr << 1, adap->name);
221 set_type(&t->i2c, t->type);
222 return 0;
225 static int tuner_probe(struct i2c_adapter *adap)
227 if (0 != addr) {
228 normal_i2c[0] = addr;
229 normal_i2c_range[0] = addr;
230 normal_i2c_range[1] = addr;
232 this_adap = 0;
234 if (adap->class & I2C_CLASS_TV_ANALOG)
235 return i2c_probe(adap, &addr_data, tuner_attach);
236 return 0;
239 static int tuner_detach(struct i2c_client *client)
241 struct tuner *t = i2c_get_clientdata(client);
243 i2c_detach_client(&t->i2c);
244 kfree(t);
245 return 0;
248 #define SWITCH_V4L2 if (!t->using_v4l2 && tuner_debug) \
249 tuner_info("switching to v4l2\n"); \
250 t->using_v4l2 = 1;
251 #define CHECK_V4L2 if (t->using_v4l2) { if (tuner_debug) \
252 tuner_info("ignore v4l1 call\n"); \
253 return 0; }
255 static int
256 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
258 struct tuner *t = i2c_get_clientdata(client);
259 unsigned int *iarg = (int*)arg;
261 switch (cmd) {
263 /* --- configuration --- */
264 case TUNER_SET_TYPE:
265 set_type(client,*iarg);
266 break;
267 case AUDC_SET_RADIO:
268 if (V4L2_TUNER_RADIO != t->mode) {
269 set_tv_freq(client,400 * 16);
270 t->mode = V4L2_TUNER_RADIO;
272 break;
273 case AUDC_CONFIG_PINNACLE:
274 switch (*iarg) {
275 case 2:
276 tuner_dbg("pinnacle pal\n");
277 t->radio_if2 = 33300 * 1000;
278 break;
279 case 3:
280 tuner_dbg("pinnacle ntsc\n");
281 t->radio_if2 = 41300 * 1000;
282 break;
284 break;
286 /* --- v4l ioctls --- */
287 /* take care: bttv does userspace copying, we'll get a
288 kernel pointer here... */
289 case VIDIOCSCHAN:
291 static const v4l2_std_id map[] = {
292 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
293 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
294 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
295 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
296 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
297 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
299 struct video_channel *vc = arg;
301 CHECK_V4L2;
302 t->mode = V4L2_TUNER_ANALOG_TV;
303 if (vc->norm < ARRAY_SIZE(map))
304 t->std = map[vc->norm];
305 tuner_fixup_std(t);
306 if (t->freq)
307 set_tv_freq(client,t->freq);
308 return 0;
310 case VIDIOCSFREQ:
312 unsigned long *v = arg;
314 CHECK_V4L2;
315 set_freq(client,*v);
316 return 0;
318 case VIDIOCGTUNER:
320 struct video_tuner *vt = arg;
322 CHECK_V4L2;
323 if (V4L2_TUNER_RADIO == t->mode && t->has_signal)
324 vt->signal = t->has_signal(client);
325 return 0;
327 case VIDIOCGAUDIO:
329 struct video_audio *va = arg;
331 CHECK_V4L2;
332 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
333 va->mode = t->is_stereo(client)
334 ? VIDEO_SOUND_STEREO
335 : VIDEO_SOUND_MONO;
336 return 0;
339 case VIDIOC_S_STD:
341 v4l2_std_id *id = arg;
343 SWITCH_V4L2;
344 t->mode = V4L2_TUNER_ANALOG_TV;
345 t->std = *id;
346 tuner_fixup_std(t);
347 if (t->freq)
348 set_freq(client,t->freq);
349 break;
351 case VIDIOC_S_FREQUENCY:
353 struct v4l2_frequency *f = arg;
355 SWITCH_V4L2;
356 if (V4L2_TUNER_RADIO == f->type &&
357 V4L2_TUNER_RADIO != t->mode)
358 set_tv_freq(client,400*16);
359 t->mode = f->type;
360 set_freq(client,f->frequency);
361 break;
363 case VIDIOC_G_FREQUENCY:
365 struct v4l2_frequency *f = arg;
367 SWITCH_V4L2;
368 f->type = t->mode;
369 f->frequency = t->freq;
370 break;
372 case VIDIOC_G_TUNER:
374 struct v4l2_tuner *tuner = arg;
376 SWITCH_V4L2;
377 if (V4L2_TUNER_RADIO == t->mode && t->has_signal)
378 tuner->signal = t->has_signal(client);
379 tuner->rangelow = tv_range[0] * 16;
380 tuner->rangehigh = tv_range[1] * 16;
381 break;
383 default:
384 /* nothing */
385 break;
388 return 0;
391 static int tuner_suspend(struct device * dev, pm_message_t state, u32 level)
393 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
394 struct tuner *t = i2c_get_clientdata(c);
396 tuner_dbg("suspend\n");
397 /* FIXME: power down ??? */
398 return 0;
401 static int tuner_resume(struct device * dev, u32 level)
403 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
404 struct tuner *t = i2c_get_clientdata(c);
406 tuner_dbg("resume\n");
407 if (t->freq)
408 set_freq(c,t->freq);
409 return 0;
412 /* ----------------------------------------------------------------------- */
414 static struct i2c_driver driver = {
415 .owner = THIS_MODULE,
416 .name = "tuner",
417 .id = I2C_DRIVERID_TUNER,
418 .flags = I2C_DF_NOTIFY,
419 .attach_adapter = tuner_probe,
420 .detach_client = tuner_detach,
421 .command = tuner_command,
422 .driver = {
423 .suspend = tuner_suspend,
424 .resume = tuner_resume,
427 static struct i2c_client client_template =
429 I2C_DEVNAME("(tuner unset)"),
430 .flags = I2C_CLIENT_ALLOW_USE,
431 .driver = &driver,
434 static int __init tuner_init_module(void)
436 return i2c_add_driver(&driver);
439 static void __exit tuner_cleanup_module(void)
441 i2c_del_driver(&driver);
444 module_init(tuner_init_module);
445 module_exit(tuner_cleanup_module);
448 * Overrides for Emacs so that we follow Linus's tabbing style.
449 * ---------------------------------------------------------------------------
450 * Local variables:
451 * c-basic-offset: 8
452 * End: