]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/saa7111.c
V4L/DVB (3568b): saa7111: Prevent array overrun
[linux-2.6-omap-h63xx.git] / drivers / media / video / saa7111.c
1 /* 
2  * saa7111 - Philips SAA7111A video decoder driver version 0.0.3
3  *
4  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
5  *
6  * Slight changes for video timing and attachment output by
7  * Wolfgang Scherr <scherr@net4you.net>
8  *
9  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10  *    - moved over to linux>=2.4.x i2c protocol (1/1/2003)
11  *
12  * Changes by Michael Hunold <michael@mihu.de>
13  *    - implemented DECODER_SET_GPIO, DECODER_INIT, DECODER_SET_VBI_BYPASS
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/fs.h>
35 #include <linux/kernel.h>
36 #include <linux/major.h>
37 #include <linux/slab.h>
38 #include <linux/mm.h>
39 #include <linux/pci.h>
40 #include <linux/signal.h>
41 #include <asm/io.h>
42 #include <asm/pgtable.h>
43 #include <asm/page.h>
44 #include <linux/sched.h>
45 #include <linux/types.h>
46
47 #include <linux/videodev.h>
48 #include <asm/uaccess.h>
49
50 MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
51 MODULE_AUTHOR("Dave Perks");
52 MODULE_LICENSE("GPL");
53
54 #include <linux/i2c.h>
55 #include <linux/i2c-dev.h>
56
57 #define I2C_NAME(s) (s)->name
58
59 #include <linux/video_decoder.h>
60
61 static int debug = 0;
62 module_param(debug, int, 0644);
63 MODULE_PARM_DESC(debug, "Debug level (0-1)");
64
65 #define dprintk(num, format, args...) \
66         do { \
67                 if (debug >= num) \
68                         printk(format, ##args); \
69         } while (0)
70
71 /* ----------------------------------------------------------------------- */
72
73 #define SAA7111_NR_REG          0x18
74
75 struct saa7111 {
76         unsigned char reg[SAA7111_NR_REG];
77
78         int norm;
79         int input;
80         int enable;
81         int bright;
82         int contrast;
83         int hue;
84         int sat;
85 };
86
87 #define   I2C_SAA7111        0x48
88
89 /* ----------------------------------------------------------------------- */
90
91 static inline int
92 saa7111_write (struct i2c_client *client,
93                u8                 reg,
94                u8                 value)
95 {
96         struct saa7111 *decoder = i2c_get_clientdata(client);
97
98         decoder->reg[reg] = value;
99         return i2c_smbus_write_byte_data(client, reg, value);
100 }
101
102 static int
103 saa7111_write_block (struct i2c_client *client,
104                      const u8          *data,
105                      unsigned int       len)
106 {
107         int ret = -1;
108         u8 reg;
109
110         /* the saa7111 has an autoincrement function, use it if
111          * the adapter understands raw I2C */
112         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
113                 /* do raw I2C, not smbus compatible */
114                 struct saa7111 *decoder = i2c_get_clientdata(client);
115                 struct i2c_msg msg;
116                 u8 block_data[32];
117
118                 msg.addr = client->addr;
119                 msg.flags = 0;
120                 while (len >= 2) {
121                         msg.buf = (char *) block_data;
122                         msg.len = 0;
123                         block_data[msg.len++] = reg = data[0];
124                         do {
125                                 block_data[msg.len++] =
126                                     decoder->reg[reg++] = data[1];
127                                 len -= 2;
128                                 data += 2;
129                         } while (len >= 2 && data[0] == reg &&
130                                  msg.len < 32);
131                         if ((ret = i2c_transfer(client->adapter,
132                                                 &msg, 1)) < 0)
133                                 break;
134                 }
135         } else {
136                 /* do some slow I2C emulation kind of thing */
137                 while (len >= 2) {
138                         reg = *data++;
139                         if ((ret = saa7111_write(client, reg,
140                                                  *data++)) < 0)
141                                 break;
142                         len -= 2;
143                 }
144         }
145
146         return ret;
147 }
148
149 static int
150 saa7111_init_decoder (struct i2c_client *client,
151               struct video_decoder_init *init)
152 {
153         return saa7111_write_block(client, init->data, init->len);
154 }
155
156 static inline int
157 saa7111_read (struct i2c_client *client,
158               u8                 reg)
159 {
160         return i2c_smbus_read_byte_data(client, reg);
161 }
162
163 /* ----------------------------------------------------------------------- */
164
165 static const unsigned char saa7111_i2c_init[] = {
166         0x00, 0x00,             /* 00 - ID byte */
167         0x01, 0x00,             /* 01 - reserved */
168
169         /*front end */
170         0x02, 0xd0,             /* 02 - FUSE=3, GUDL=2, MODE=0 */
171         0x03, 0x23,             /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
172                                  * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
173         0x04, 0x00,             /* 04 - GAI1=256 */
174         0x05, 0x00,             /* 05 - GAI2=256 */
175
176         /* decoder */
177         0x06, 0xf3,             /* 06 - HSB at  13(50Hz) /  17(60Hz)
178                                  * pixels after end of last line */
179         /*0x07, 0x13,     * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
180                                  * after end of last line */
181         0x07, 0xe8,             /* 07 - HSS seems to be needed to
182                                  * work with NTSC, too */
183         0x08, 0xc8,             /* 08 - AUFD=1, FSEL=1, EXFIL=0,
184                                  * VTRC=1, HPLL=0, VNOI=0 */
185         0x09, 0x01,             /* 09 - BYPS=0, PREF=0, BPSS=0,
186                                  * VBLB=0, UPTCV=0, APER=1 */
187         0x0a, 0x80,             /* 0a - BRIG=128 */
188         0x0b, 0x47,             /* 0b - CONT=1.109 */
189         0x0c, 0x40,             /* 0c - SATN=1.0 */
190         0x0d, 0x00,             /* 0d - HUE=0 */
191         0x0e, 0x01,             /* 0e - CDTO=0, CSTD=0, DCCF=0,
192                                  * FCTC=0, CHBW=1 */
193         0x0f, 0x00,             /* 0f - reserved */
194         0x10, 0x48,             /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
195         0x11, 0x1c,             /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
196                                  * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
197         0x12, 0x00,             /* 12 - output control 2 */
198         0x13, 0x00,             /* 13 - output control 3 */
199         0x14, 0x00,             /* 14 - reserved */
200         0x15, 0x00,             /* 15 - VBI */
201         0x16, 0x00,             /* 16 - VBI */
202         0x17, 0x00,             /* 17 - VBI */
203 };
204
205 static int
206 saa7111_command (struct i2c_client *client,
207                  unsigned int       cmd,
208                  void              *arg)
209 {
210         struct saa7111 *decoder = i2c_get_clientdata(client);
211
212         switch (cmd) {
213
214         case 0:
215         case DECODER_INIT:
216         {
217                 struct video_decoder_init *init = arg;
218                 if (NULL != init)
219                         return saa7111_init_decoder(client, init);
220                 else {
221                         struct video_decoder_init vdi;
222                         vdi.data = saa7111_i2c_init;
223                         vdi.len = sizeof(saa7111_i2c_init);
224                         return saa7111_init_decoder(client, &vdi);
225                 }
226         }
227
228         case DECODER_DUMP:
229         {
230                 int i;
231
232                 for (i = 0; i < SAA7111_NR_REG; i += 16) {
233                         int j;
234
235                         printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
236                         for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
237                                 printk(" %02x",
238                                        saa7111_read(client, i + j));
239                         }
240                         printk("\n");
241                 }
242         }
243                 break;
244
245         case DECODER_GET_CAPABILITIES:
246         {
247                 struct video_decoder_capability *cap = arg;
248
249                 cap->flags = VIDEO_DECODER_PAL |
250                              VIDEO_DECODER_NTSC |
251                              VIDEO_DECODER_SECAM |
252                              VIDEO_DECODER_AUTO |
253                              VIDEO_DECODER_CCIR;
254                 cap->inputs = 8;
255                 cap->outputs = 1;
256         }
257                 break;
258
259         case DECODER_GET_STATUS:
260         {
261                 int *iarg = arg;
262                 int status;
263                 int res;
264
265                 status = saa7111_read(client, 0x1f);
266                 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
267                         status);
268                 res = 0;
269                 if ((status & (1 << 6)) == 0) {
270                         res |= DECODER_STATUS_GOOD;
271                 }
272                 switch (decoder->norm) {
273                 case VIDEO_MODE_NTSC:
274                         res |= DECODER_STATUS_NTSC;
275                         break;
276                 case VIDEO_MODE_PAL:
277                         res |= DECODER_STATUS_PAL;
278                         break;
279                 case VIDEO_MODE_SECAM:
280                         res |= DECODER_STATUS_SECAM;
281                         break;
282                 default:
283                 case VIDEO_MODE_AUTO:
284                         if ((status & (1 << 5)) != 0) {
285                                 res |= DECODER_STATUS_NTSC;
286                         } else {
287                                 res |= DECODER_STATUS_PAL;
288                         }
289                         break;
290                 }
291                 if ((status & (1 << 0)) != 0) {
292                         res |= DECODER_STATUS_COLOR;
293                 }
294                 *iarg = res;
295         }
296                 break;
297
298         case DECODER_SET_GPIO:
299         {
300                 int *iarg = arg;
301                 if (0 != *iarg) {
302                         saa7111_write(client, 0x11,
303                                 (decoder->reg[0x11] | 0x80));
304                 } else {
305                         saa7111_write(client, 0x11,
306                                 (decoder->reg[0x11] & 0x7f));
307                 }
308                 break;
309         }
310
311         case DECODER_SET_VBI_BYPASS:
312         {
313                 int *iarg = arg;
314                 if (0 != *iarg) {
315                         saa7111_write(client, 0x13,
316                                 (decoder->reg[0x13] & 0xf0) | 0x0a);
317                 } else {
318                         saa7111_write(client, 0x13,
319                                 (decoder->reg[0x13] & 0xf0));
320                 }
321                 break;
322         }
323
324         case DECODER_SET_NORM:
325         {
326                 int *iarg = arg;
327
328                 switch (*iarg) {
329
330                 case VIDEO_MODE_NTSC:
331                         saa7111_write(client, 0x08,
332                                       (decoder->reg[0x08] & 0x3f) | 0x40);
333                         saa7111_write(client, 0x0e,
334                                       (decoder->reg[0x0e] & 0x8f));
335                         break;
336
337                 case VIDEO_MODE_PAL:
338                         saa7111_write(client, 0x08,
339                                       (decoder->reg[0x08] & 0x3f) | 0x00);
340                         saa7111_write(client, 0x0e,
341                                       (decoder->reg[0x0e] & 0x8f));
342                         break;
343
344                 case VIDEO_MODE_SECAM:
345                         saa7111_write(client, 0x08,
346                                       (decoder->reg[0x08] & 0x3f) | 0x00);
347                         saa7111_write(client, 0x0e,
348                                       (decoder->reg[0x0e] & 0x8f) | 0x50);
349                         break;
350
351                 case VIDEO_MODE_AUTO:
352                         saa7111_write(client, 0x08,
353                                       (decoder->reg[0x08] & 0x3f) | 0x80);
354                         saa7111_write(client, 0x0e,
355                                       (decoder->reg[0x0e] & 0x8f));
356                         break;
357
358                 default:
359                         return -EINVAL;
360
361                 }
362                 decoder->norm = *iarg;
363         }
364                 break;
365
366         case DECODER_SET_INPUT:
367         {
368                 int *iarg = arg;
369
370                 if (*iarg < 0 || *iarg > 7) {
371                         return -EINVAL;
372                 }
373
374                 if (decoder->input != *iarg) {
375                         decoder->input = *iarg;
376                         /* select mode */
377                         saa7111_write(client, 0x02,
378                                       (decoder->
379                                        reg[0x02] & 0xf8) | decoder->input);
380                         /* bypass chrominance trap for modes 4..7 */
381                         saa7111_write(client, 0x09,
382                                       (decoder->
383                                        reg[0x09] & 0x7f) | ((decoder->
384                                                              input >
385                                                              3) ? 0x80 :
386                                                             0));
387                 }
388         }
389                 break;
390
391         case DECODER_SET_OUTPUT:
392         {
393                 int *iarg = arg;
394
395                 /* not much choice of outputs */
396                 if (*iarg != 0) {
397                         return -EINVAL;
398                 }
399         }
400                 break;
401
402         case DECODER_ENABLE_OUTPUT:
403         {
404                 int *iarg = arg;
405                 int enable = (*iarg != 0);
406
407                 if (decoder->enable != enable) {
408                         decoder->enable = enable;
409
410                         /* RJ: If output should be disabled (for
411                          * playing videos), we also need a open PLL.
412                          * The input is set to 0 (where no input
413                          * source is connected), although this
414                          * is not necessary.
415                          *
416                          * If output should be enabled, we have to
417                          * reverse the above.
418                          */
419
420                         if (decoder->enable) {
421                                 saa7111_write(client, 0x02,
422                                               (decoder->
423                                                reg[0x02] & 0xf8) |
424                                               decoder->input);
425                                 saa7111_write(client, 0x08,
426                                               (decoder->reg[0x08] & 0xfb));
427                                 saa7111_write(client, 0x11,
428                                               (decoder->
429                                                reg[0x11] & 0xf3) | 0x0c);
430                         } else {
431                                 saa7111_write(client, 0x02,
432                                               (decoder->reg[0x02] & 0xf8));
433                                 saa7111_write(client, 0x08,
434                                               (decoder->
435                                                reg[0x08] & 0xfb) | 0x04);
436                                 saa7111_write(client, 0x11,
437                                               (decoder->reg[0x11] & 0xf3));
438                         }
439                 }
440         }
441                 break;
442
443         case DECODER_SET_PICTURE:
444         {
445                 struct video_picture *pic = arg;
446
447                 if (decoder->bright != pic->brightness) {
448                         /* We want 0 to 255 we get 0-65535 */
449                         decoder->bright = pic->brightness;
450                         saa7111_write(client, 0x0a, decoder->bright >> 8);
451                 }
452                 if (decoder->contrast != pic->contrast) {
453                         /* We want 0 to 127 we get 0-65535 */
454                         decoder->contrast = pic->contrast;
455                         saa7111_write(client, 0x0b,
456                                       decoder->contrast >> 9);
457                 }
458                 if (decoder->sat != pic->colour) {
459                         /* We want 0 to 127 we get 0-65535 */
460                         decoder->sat = pic->colour;
461                         saa7111_write(client, 0x0c, decoder->sat >> 9);
462                 }
463                 if (decoder->hue != pic->hue) {
464                         /* We want -128 to 127 we get 0-65535 */
465                         decoder->hue = pic->hue;
466                         saa7111_write(client, 0x0d,
467                                       (decoder->hue - 32768) >> 8);
468                 }
469         }
470                 break;
471
472         default:
473                 return -EINVAL;
474         }
475
476         return 0;
477 }
478
479 /* ----------------------------------------------------------------------- */
480
481 /*
482  * Generic i2c probe
483  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
484  */
485 static unsigned short normal_i2c[] = { I2C_SAA7111 >> 1, I2C_CLIENT_END };
486
487 static unsigned short ignore = I2C_CLIENT_END;
488                                                                                 
489 static struct i2c_client_address_data addr_data = {
490         .normal_i2c             = normal_i2c,
491         .probe                  = &ignore,
492         .ignore                 = &ignore,
493 };
494
495 static struct i2c_driver i2c_driver_saa7111;
496
497 static int
498 saa7111_detect_client (struct i2c_adapter *adapter,
499                        int                 address,
500                        int                 kind)
501 {
502         int i;
503         struct i2c_client *client;
504         struct saa7111 *decoder;
505         struct video_decoder_init vdi;
506
507         dprintk(1,
508                 KERN_INFO
509                 "saa7111.c: detecting saa7111 client on address 0x%x\n",
510                 address << 1);
511
512         /* Check if the adapter supports the needed features */
513         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
514                 return 0;
515
516         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
517         if (client == 0)
518                 return -ENOMEM;
519         client->addr = address;
520         client->adapter = adapter;
521         client->driver = &i2c_driver_saa7111;
522         strlcpy(I2C_NAME(client), "saa7111", sizeof(I2C_NAME(client)));
523
524         decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
525         if (decoder == NULL) {
526                 kfree(client);
527                 return -ENOMEM;
528         }
529         decoder->norm = VIDEO_MODE_NTSC;
530         decoder->input = 0;
531         decoder->enable = 1;
532         decoder->bright = 32768;
533         decoder->contrast = 32768;
534         decoder->hue = 32768;
535         decoder->sat = 32768;
536         i2c_set_clientdata(client, decoder);
537
538         i = i2c_attach_client(client);
539         if (i) {
540                 kfree(client);
541                 kfree(decoder);
542                 return i;
543         }
544
545         vdi.data = saa7111_i2c_init;
546         vdi.len = sizeof(saa7111_i2c_init);
547         i = saa7111_init_decoder(client, &vdi);
548         if (i < 0) {
549                 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
550                         I2C_NAME(client), i);
551         } else {
552                 dprintk(1,
553                         KERN_INFO
554                         "%s_attach: chip version %x at address 0x%x\n",
555                         I2C_NAME(client), saa7111_read(client, 0x00) >> 4,
556                         client->addr << 1);
557         }
558
559         return 0;
560 }
561
562 static int
563 saa7111_attach_adapter (struct i2c_adapter *adapter)
564 {
565         dprintk(1,
566                 KERN_INFO
567                 "saa7111.c: starting probe for adapter %s (0x%x)\n",
568                 I2C_NAME(adapter), adapter->id);
569         return i2c_probe(adapter, &addr_data, &saa7111_detect_client);
570 }
571
572 static int
573 saa7111_detach_client (struct i2c_client *client)
574 {
575         struct saa7111 *decoder = i2c_get_clientdata(client);
576         int err;
577
578         err = i2c_detach_client(client);
579         if (err) {
580                 return err;
581         }
582
583         kfree(decoder);
584         kfree(client);
585
586         return 0;
587 }
588
589 /* ----------------------------------------------------------------------- */
590
591 static struct i2c_driver i2c_driver_saa7111 = {
592         .driver = {
593                 .name = "saa7111",
594         },
595
596         .id = I2C_DRIVERID_SAA7111A,
597
598         .attach_adapter = saa7111_attach_adapter,
599         .detach_client = saa7111_detach_client,
600         .command = saa7111_command,
601 };
602
603 static int __init
604 saa7111_init (void)
605 {
606         return i2c_add_driver(&i2c_driver_saa7111);
607 }
608
609 static void __exit
610 saa7111_exit (void)
611 {
612         i2c_del_driver(&i2c_driver_saa7111);
613 }
614
615 module_init(saa7111_init);
616 module_exit(saa7111_exit);