V4L/DVB (3269): ioctls cleanups.
[linux-2.6] / drivers / media / video / tuner-core.c
1 /*
2  *
3  * i2c tv tuner chip device driver
4  * core core, i.e. kernel interfaces, registering and so on
5  */
6
7 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/string.h>
12 #include <linux/timer.h>
13 #include <linux/delay.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/poll.h>
17 #include <linux/i2c.h>
18 #include <linux/types.h>
19 #include <linux/videodev.h>
20 #include <linux/init.h>
21
22 #include <media/tuner.h>
23 #include <media/v4l2-common.h>
24 #include <media/audiochip.h>
25
26 #include "msp3400.h"
27
28 #define UNSET (-1U)
29
30 /* standard i2c insmod options */
31 static unsigned short normal_i2c[] = {
32         0x42, 0x43, 0x4a, 0x4b,                 /* tda8290 */
33         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
34         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
35         I2C_CLIENT_END
36 };
37
38 I2C_CLIENT_INSMOD;
39
40 /* insmod options used at init time => read/only */
41 static unsigned int addr = 0;
42 static unsigned int no_autodetect = 0;
43 static unsigned int show_i2c = 0;
44
45 /* insmod options used at runtime => read/write */
46 unsigned int tuner_debug = 0;
47
48 static unsigned int tv_range[2] = { 44, 958 };
49 static unsigned int radio_range[2] = { 65, 108 };
50
51 static char pal[] = "--";
52 static char secam[] = "--";
53 static char ntsc[] = "-";
54
55 module_param(addr, int, 0444);
56 module_param(no_autodetect, int, 0444);
57 module_param(show_i2c, int, 0444);
58 module_param(tuner_debug, int, 0644);
59
60 module_param_string(pal, pal, sizeof(pal), 0644);
61 module_param_string(secam, secam, sizeof(secam), 0644);
62 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
63 module_param_array(tv_range, int, NULL, 0644);
64 module_param_array(radio_range, int, NULL, 0644);
65
66 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
67 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
68 MODULE_LICENSE("GPL");
69
70 static struct i2c_driver driver;
71 static struct i2c_client client_template;
72
73 /* ---------------------------------------------------------------------- */
74
75 /* Set tuner frequency,  freq in Units of 62.5kHz = 1/16MHz */
76 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
77 {
78         struct tuner *t = i2c_get_clientdata(c);
79
80         if (t->type == UNSET) {
81                 tuner_warn ("tuner type not set\n");
82                 return;
83         }
84         if (NULL == t->tv_freq) {
85                 tuner_warn ("Tuner has no way to set tv freq\n");
86                 return;
87         }
88         if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
89                 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
90                            freq / 16, freq % 16 * 100 / 16, tv_range[0],
91                            tv_range[1]);
92         }
93         t->tv_freq(c, freq);
94 }
95
96 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
97 {
98         struct tuner *t = i2c_get_clientdata(c);
99
100         if (t->type == UNSET) {
101                 tuner_warn ("tuner type not set\n");
102                 return;
103         }
104         if (NULL == t->radio_freq) {
105                 tuner_warn ("tuner has no way to set radio frequency\n");
106                 return;
107         }
108         if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
109                 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
110                            freq / 16000, freq % 16000 * 100 / 16000,
111                            radio_range[0], radio_range[1]);
112         }
113
114         t->radio_freq(c, freq);
115         return;
116 }
117
118 static void set_freq(struct i2c_client *c, unsigned long freq)
119 {
120         struct tuner *t = i2c_get_clientdata(c);
121
122         switch (t->mode) {
123         case V4L2_TUNER_RADIO:
124                 tuner_dbg("radio freq set to %lu.%02lu\n",
125                           freq / 16000, freq % 16000 * 100 / 16000);
126                 set_radio_freq(c, freq);
127                 break;
128         case V4L2_TUNER_ANALOG_TV:
129         case V4L2_TUNER_DIGITAL_TV:
130                 tuner_dbg("tv freq set to %lu.%02lu\n",
131                           freq / 16, freq % 16 * 100 / 16);
132                 set_tv_freq(c, freq);
133                 break;
134         }
135         t->freq = freq;
136 }
137
138 static void set_type(struct i2c_client *c, unsigned int type,
139                      unsigned int new_mode_mask)
140 {
141         struct tuner *t = i2c_get_clientdata(c);
142         unsigned char buffer[4];
143
144         if (type == UNSET || type == TUNER_ABSENT) {
145                 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
146                 return;
147         }
148
149         if (type >= tuner_count) {
150                 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
151                 return;
152         }
153
154         /* This code detects calls by card attach_inform */
155         if (NULL == t->i2c.dev.driver) {
156                 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
157
158                 t->type=type;
159                 return;
160         }
161
162         t->type = type;
163
164         switch (t->type) {
165         case TUNER_MT2032:
166                 microtune_init(c);
167                 break;
168         case TUNER_PHILIPS_TDA8290:
169                 tda8290_init(c);
170                 break;
171         case TUNER_TEA5767:
172                 if (tea5767_tuner_init(c) == EINVAL) {
173                         t->type = TUNER_ABSENT;
174                         t->mode_mask = T_UNINITIALIZED;
175                         return;
176                 }
177                 t->mode_mask = T_RADIO;
178                 break;
179         case TUNER_PHILIPS_FMD1216ME_MK3:
180                 buffer[0] = 0x0b;
181                 buffer[1] = 0xdc;
182                 buffer[2] = 0x9c;
183                 buffer[3] = 0x60;
184                 i2c_master_send(c, buffer, 4);
185                 mdelay(1);
186                 buffer[2] = 0x86;
187                 buffer[3] = 0x54;
188                 i2c_master_send(c, buffer, 4);
189                 default_tuner_init(c);
190                 break;
191         case TUNER_LG_TDVS_H062F:
192                 /* Set the Auxiliary Byte. */
193                 buffer[2] &= ~0x20;
194                 buffer[2] |= 0x18;
195                 buffer[3] = 0x20;
196                 i2c_master_send(c, buffer, 4);
197                 default_tuner_init(c);
198                 break;
199         case TUNER_PHILIPS_TD1316:
200                 buffer[0] = 0x0b;
201                 buffer[1] = 0xdc;
202                 buffer[2] = 0x86;
203                 buffer[3] = 0xa4;
204                 i2c_master_send(c,buffer,4);
205                 default_tuner_init(c);
206         default:
207                 default_tuner_init(c);
208                 break;
209         }
210
211         if (t->mode_mask == T_UNINITIALIZED)
212                 t->mode_mask = new_mode_mask;
213
214         set_freq(c, t->freq);
215         tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
216                   c->adapter->name, c->driver->driver.name, c->addr << 1, type,
217                   t->mode_mask);
218 }
219
220 /*
221  * This function apply tuner config to tuner specified
222  * by tun_setup structure. I addr is unset, then admin status
223  * and tun addr status is more precise then current status,
224  * it's applied. Otherwise status and type are applied only to
225  * tuner with exactly the same addr.
226 */
227
228 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
229 {
230         struct tuner *t = i2c_get_clientdata(c);
231
232         if ( t->type == UNSET && ((tun_setup->addr == ADDR_UNSET &&
233                 (t->mode_mask & tun_setup->mode_mask)) ||
234                 tun_setup->addr == c->addr)) {
235                         set_type(c, tun_setup->type, tun_setup->mode_mask);
236         }
237 }
238
239 static inline int check_mode(struct tuner *t, char *cmd)
240 {
241         if ((1 << t->mode & t->mode_mask) == 0) {
242                 return EINVAL;
243         }
244
245         switch (t->mode) {
246         case V4L2_TUNER_RADIO:
247                 tuner_dbg("Cmd %s accepted for radio\n", cmd);
248                 break;
249         case V4L2_TUNER_ANALOG_TV:
250                 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
251                 break;
252         case V4L2_TUNER_DIGITAL_TV:
253                 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
254                 break;
255         }
256         return 0;
257 }
258
259 /* get more precise norm info from insmod option */
260 static int tuner_fixup_std(struct tuner *t)
261 {
262         if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
263                 switch (pal[0]) {
264                 case 'b':
265                 case 'B':
266                 case 'g':
267                 case 'G':
268                         tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
269                         t->std = V4L2_STD_PAL_BG;
270                         break;
271                 case 'i':
272                 case 'I':
273                         tuner_dbg ("insmod fixup: PAL => PAL-I\n");
274                         t->std = V4L2_STD_PAL_I;
275                         break;
276                 case 'd':
277                 case 'D':
278                 case 'k':
279                 case 'K':
280                         tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
281                         t->std = V4L2_STD_PAL_DK;
282                         break;
283                 case 'M':
284                 case 'm':
285                         tuner_dbg ("insmod fixup: PAL => PAL-M\n");
286                         t->std = V4L2_STD_PAL_M;
287                         break;
288                 case 'N':
289                 case 'n':
290                         if (pal[1] == 'c' || pal[1] == 'C') {
291                                 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
292                                 t->std = V4L2_STD_PAL_Nc;
293                         } else {
294                                 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
295                                 t->std = V4L2_STD_PAL_N;
296                         }
297                         break;
298                 case '-':
299                         /* default parameter, do nothing */
300                         break;
301                 default:
302                         tuner_warn ("pal= argument not recognised\n");
303                         break;
304                 }
305         }
306         if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
307                 switch (secam[0]) {
308                 case 'b':
309                 case 'B':
310                 case 'g':
311                 case 'G':
312                 case 'h':
313                 case 'H':
314                         tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
315                         t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
316                         break;
317                 case 'd':
318                 case 'D':
319                 case 'k':
320                 case 'K':
321                         tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
322                         t->std = V4L2_STD_SECAM_DK;
323                         break;
324                 case 'l':
325                 case 'L':
326                         if ((secam[1]=='C')||(secam[1]=='c')) {
327                                 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
328                                 t->std = V4L2_STD_SECAM_LC;
329                         } else {
330                                 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
331                                 t->std = V4L2_STD_SECAM_L;
332                         }
333                         break;
334                 case '-':
335                         /* default parameter, do nothing */
336                         break;
337                 default:
338                         tuner_warn ("secam= argument not recognised\n");
339                         break;
340                 }
341         }
342
343         if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
344                 switch (ntsc[0]) {
345                 case 'm':
346                 case 'M':
347                         tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
348                         t->std = V4L2_STD_NTSC_M;
349                         break;
350                 case 'j':
351                 case 'J':
352                         tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
353                         t->std = V4L2_STD_NTSC_M_JP;
354                         break;
355                 case '-':
356                         /* default parameter, do nothing */
357                         break;
358                 default:
359                         tuner_info("ntsc= argument not recognised\n");
360                         break;
361                 }
362         }
363         return 0;
364 }
365
366 static void tuner_status(struct i2c_client *client)
367 {
368         struct tuner *t = i2c_get_clientdata(client);
369         unsigned long freq, freq_fraction;
370         const char *p;
371
372         switch (t->mode) {
373                 case V4L2_TUNER_RADIO:      p = "radio"; break;
374                 case V4L2_TUNER_ANALOG_TV:  p = "analog TV"; break;
375                 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
376                 default: p = "undefined"; break;
377         }
378         if (t->mode == V4L2_TUNER_RADIO) {
379                 freq = t->freq / 16000;
380                 freq_fraction = (t->freq % 16000) * 100 / 16000;
381         } else {
382                 freq = t->freq / 16;
383                 freq_fraction = (t->freq % 16) * 100 / 16;
384         }
385         tuner_info("Tuner mode:      %s\n", p);
386         tuner_info("Frequency:       %lu.%02lu MHz\n", freq, freq_fraction);
387         tuner_info("Standard:        0x%08llx\n", t->std);
388         if (t->mode == V4L2_TUNER_RADIO) {
389                 if (t->has_signal) {
390                         tuner_info("Signal strength: %d\n", t->has_signal(client));
391                 }
392                 if (t->is_stereo) {
393                         tuner_info("Stereo:          %s\n", t->is_stereo(client) ? "yes" : "no");
394                 }
395         }
396 }
397 /* ---------------------------------------------------------------------- */
398
399 /* static var Used only in tuner_attach and tuner_probe */
400 static unsigned default_mode_mask;
401
402 /* During client attach, set_type is called by adapter's attach_inform callback.
403    set_type must then be completed by tuner_attach.
404  */
405 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
406 {
407         struct tuner *t;
408
409         client_template.adapter = adap;
410         client_template.addr = addr;
411
412         t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
413         if (NULL == t)
414                 return -ENOMEM;
415         memset(t, 0, sizeof(struct tuner));
416         memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
417         i2c_set_clientdata(&t->i2c, t);
418         t->type = UNSET;
419         t->radio_if2 = 10700 * 1000;    /* 10.7MHz - FM radio */
420         t->audmode = V4L2_TUNER_MODE_STEREO;
421         t->mode_mask = T_UNINITIALIZED;
422
423         if (show_i2c) {
424                 unsigned char buffer[16];
425                 int i,rc;
426
427                 memset(buffer, 0, sizeof(buffer));
428                 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
429                 tuner_info("I2C RECV = ");
430                 for (i=0;i<rc;i++)
431                         printk("%02x ",buffer[i]);
432                 printk("\n");
433         }
434         /* TEA5767 autodetection code - only for addr = 0xc0 */
435         if (!no_autodetect) {
436                 switch (addr) {
437                 case 0x42:
438                 case 0x43:
439                 case 0x4a:
440                 case 0x4b:
441                         /* If chip is not tda8290, don't register.
442                            since it can be tda9887*/
443                         if (tda8290_probe(&t->i2c) != 0) {
444                                 tuner_dbg("chip at addr %x is not a tda8290\n", addr);
445                                 kfree(t);
446                                 return 0;
447                         }
448                         break;
449                 case 0x60:
450                         if (tea5767_autodetection(&t->i2c) != EINVAL) {
451                                 t->type = TUNER_TEA5767;
452                                 t->mode_mask = T_RADIO;
453                                 t->mode = T_STANDBY;
454                                 t->freq = 87.5 * 16; /* Sets freq to FM range */
455                                 default_mode_mask &= ~T_RADIO;
456
457                                 goto register_client;
458                         }
459                         break;
460                 }
461         }
462
463         /* Initializes only the first adapter found */
464         if (default_mode_mask != T_UNINITIALIZED) {
465                 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
466                 t->mode_mask = default_mode_mask;
467                 t->freq = 400 * 16; /* Sets freq to VHF High */
468                 default_mode_mask = T_UNINITIALIZED;
469         }
470
471         /* Should be just before return */
472 register_client:
473         tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
474         i2c_attach_client (&t->i2c);
475         set_type (&t->i2c,t->type, t->mode_mask);
476         return 0;
477 }
478
479 static int tuner_probe(struct i2c_adapter *adap)
480 {
481         if (0 != addr) {
482                 normal_i2c[0] = addr;
483                 normal_i2c[1] = I2C_CLIENT_END;
484         }
485
486         default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
487
488         if (adap->class & I2C_CLASS_TV_ANALOG)
489                 return i2c_probe(adap, &addr_data, tuner_attach);
490         return 0;
491 }
492
493 static int tuner_detach(struct i2c_client *client)
494 {
495         struct tuner *t = i2c_get_clientdata(client);
496         int err;
497
498         err = i2c_detach_client(&t->i2c);
499         if (err) {
500                 tuner_warn
501                     ("Client deregistration failed, client not detached.\n");
502                 return err;
503         }
504
505         kfree(t);
506         return 0;
507 }
508
509 /*
510  * Switch tuner to other mode. If tuner support both tv and radio,
511  * set another frequency to some value (This is needed for some pal
512  * tuners to avoid locking). Otherwise, just put second tuner in
513  * standby mode.
514  */
515
516 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
517 {
518         if (mode == t->mode)
519                 return 0;
520
521         t->mode = mode;
522
523         if (check_mode(t, cmd) == EINVAL) {
524                 t->mode = T_STANDBY;
525                 if (t->standby)
526                         t->standby (client);
527                 return EINVAL;
528         }
529         return 0;
530 }
531
532 #define switch_v4l2()   if (!t->using_v4l2) \
533                             tuner_dbg("switching to v4l2\n"); \
534                         t->using_v4l2 = 1;
535
536 static inline int check_v4l2(struct tuner *t)
537 {
538         if (t->using_v4l2) {
539                 tuner_dbg ("ignore v4l1 call\n");
540                 return EINVAL;
541         }
542         return 0;
543 }
544
545 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
546 {
547         struct tuner *t = i2c_get_clientdata(client);
548
549         if (tuner_debug>1)
550                 v4l_i2c_print_ioctl(&(t->i2c),cmd);
551
552         switch (cmd) {
553         /* --- configuration --- */
554         case TUNER_SET_TYPE_ADDR:
555                 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
556                                 ((struct tuner_setup *)arg)->type,
557                                 ((struct tuner_setup *)arg)->addr,
558                                 ((struct tuner_setup *)arg)->mode_mask);
559
560                 set_addr(client, (struct tuner_setup *)arg);
561                 break;
562         case AUDC_SET_RADIO:
563                 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
564                 break;
565         case TUNER_SET_STANDBY:
566                 {
567                         if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
568                                 return 0;
569                         if (t->standby)
570                                 t->standby (client);
571                         break;
572                 }
573         case VIDIOCSAUDIO:
574                 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
575                         return 0;
576                 if (check_v4l2(t) == EINVAL)
577                         return 0;
578
579                 /* Should be implemented, since bttv calls it */
580                 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
581
582                 break;
583         /* --- v4l ioctls --- */
584         /* take care: bttv does userspace copying, we'll get a
585            kernel pointer here... */
586         case VIDIOCSCHAN:
587                 {
588                         static const v4l2_std_id map[] = {
589                                 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
590                                 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
591                                 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
592                                 [4 /* bttv */ ] = V4L2_STD_PAL_M,
593                                 [5 /* bttv */ ] = V4L2_STD_PAL_N,
594                                 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
595                         };
596                         struct video_channel *vc = arg;
597
598                         if (check_v4l2(t) == EINVAL)
599                                 return 0;
600
601                         if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
602                                 return 0;
603
604                         if (vc->norm < ARRAY_SIZE(map))
605                                 t->std = map[vc->norm];
606                         tuner_fixup_std(t);
607                         if (t->freq)
608                                 set_tv_freq(client, t->freq);
609                         return 0;
610                 }
611         case VIDIOCSFREQ:
612                 {
613                         unsigned long *v = arg;
614
615                         if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
616                                 return 0;
617                         if (check_v4l2(t) == EINVAL)
618                                 return 0;
619
620                         set_freq(client, *v);
621                         return 0;
622                 }
623         case VIDIOCGTUNER:
624                 {
625                         struct video_tuner *vt = arg;
626
627                         if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
628                                 return 0;
629                         if (check_v4l2(t) == EINVAL)
630                                 return 0;
631
632                         if (V4L2_TUNER_RADIO == t->mode) {
633                                 if (t->has_signal)
634                                         vt->signal = t->has_signal(client);
635                                 if (t->is_stereo) {
636                                         if (t->is_stereo(client))
637                                                 vt->flags |=
638                                                     VIDEO_TUNER_STEREO_ON;
639                                         else
640                                                 vt->flags &=
641                                                     ~VIDEO_TUNER_STEREO_ON;
642                                 }
643                                 vt->flags |= VIDEO_TUNER_LOW;   /* Allow freqs at 62.5 Hz */
644
645                                 vt->rangelow = radio_range[0] * 16000;
646                                 vt->rangehigh = radio_range[1] * 16000;
647
648                         } else {
649                                 vt->rangelow = tv_range[0] * 16;
650                                 vt->rangehigh = tv_range[1] * 16;
651                         }
652
653                         return 0;
654                 }
655         case VIDIOCGAUDIO:
656                 {
657                         struct video_audio *va = arg;
658
659                         if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
660                                 return 0;
661                         if (check_v4l2(t) == EINVAL)
662                                 return 0;
663
664                         if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
665                                 va->mode = t->is_stereo(client)
666                                     ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
667                         return 0;
668                 }
669
670         case VIDIOC_S_STD:
671                 {
672                         v4l2_std_id *id = arg;
673
674                         if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
675                                         == EINVAL)
676                                 return 0;
677
678                         switch_v4l2();
679
680                         t->std = *id;
681                         tuner_fixup_std(t);
682                         if (t->freq)
683                                 set_freq(client, t->freq);
684                         break;
685                 }
686         case VIDIOC_S_FREQUENCY:
687                 {
688                         struct v4l2_frequency *f = arg;
689
690                         t->freq = f->frequency;
691                         switch_v4l2();
692                         if (V4L2_TUNER_RADIO == f->type &&
693                             V4L2_TUNER_RADIO != t->mode) {
694                                 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
695                                             == EINVAL)
696                                         return 0;
697                         }
698                         set_freq(client,t->freq);
699
700                         break;
701                 }
702         case VIDIOC_G_FREQUENCY:
703                 {
704                         struct v4l2_frequency *f = arg;
705
706                         if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
707                                 return 0;
708                         switch_v4l2();
709                         f->type = t->mode;
710                         f->frequency = t->freq;
711                         break;
712                 }
713         case VIDIOC_G_TUNER:
714                 {
715                         struct v4l2_tuner *tuner = arg;
716
717                         if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
718                                 return 0;
719                         switch_v4l2();
720
721                         if (V4L2_TUNER_RADIO == t->mode) {
722
723                                 if (t->has_signal)
724                                         tuner->signal = t->has_signal(client);
725
726                                 if (t->is_stereo) {
727                                         if (t->is_stereo(client)) {
728                                                 tuner->rxsubchans =
729                                                     V4L2_TUNER_SUB_STEREO |
730                                                     V4L2_TUNER_SUB_MONO;
731                                         } else {
732                                                 tuner->rxsubchans =
733                                                     V4L2_TUNER_SUB_MONO;
734                                         }
735                                 }
736
737                                 tuner->capability |=
738                                     V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
739
740                                 tuner->audmode = t->audmode;
741
742                                 tuner->rangelow = radio_range[0] * 16000;
743                                 tuner->rangehigh = radio_range[1] * 16000;
744                         } else {
745                                 tuner->rangelow = tv_range[0] * 16;
746                                 tuner->rangehigh = tv_range[1] * 16;
747                         }
748                         break;
749                 }
750         case VIDIOC_S_TUNER:
751                 {
752                         struct v4l2_tuner *tuner = arg;
753
754                         if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
755                                 return 0;
756
757                         switch_v4l2();
758
759                         if (V4L2_TUNER_RADIO == t->mode) {
760                                 t->audmode = tuner->audmode;
761                                 set_radio_freq(client, t->freq);
762                         }
763                         break;
764                 }
765         case VIDIOC_LOG_STATUS:
766                 tuner_status(client);
767                 break;
768         }
769
770         return 0;
771 }
772
773 static int tuner_suspend(struct device *dev, pm_message_t state)
774 {
775         struct i2c_client *c = container_of (dev, struct i2c_client, dev);
776         struct tuner *t = i2c_get_clientdata (c);
777
778         tuner_dbg ("suspend\n");
779         /* FIXME: power down ??? */
780         return 0;
781 }
782
783 static int tuner_resume(struct device *dev)
784 {
785         struct i2c_client *c = container_of (dev, struct i2c_client, dev);
786         struct tuner *t = i2c_get_clientdata (c);
787
788         tuner_dbg ("resume\n");
789         if (t->freq)
790                 set_freq(c, t->freq);
791         return 0;
792 }
793
794 /* ----------------------------------------------------------------------- */
795
796 static struct i2c_driver driver = {
797         .id = I2C_DRIVERID_TUNER,
798         .attach_adapter = tuner_probe,
799         .detach_client = tuner_detach,
800         .command = tuner_command,
801         .driver = {
802                    .name = "tuner",
803                    .suspend = tuner_suspend,
804                    .resume = tuner_resume,
805                    },
806 };
807 static struct i2c_client client_template = {
808         .name = "(tuner unset)",
809         .driver = &driver,
810 };
811
812 static int __init tuner_init_module(void)
813 {
814         return i2c_add_driver(&driver);
815 }
816
817 static void __exit tuner_cleanup_module(void)
818 {
819         i2c_del_driver(&driver);
820 }
821
822 module_init(tuner_init_module);
823 module_exit(tuner_cleanup_module);
824
825 /*
826  * Overrides for Emacs so that we follow Linus's tabbing style.
827  * ---------------------------------------------------------------------------
828  * Local variables:
829  * c-basic-offset: 8
830  * End:
831  */