V4L/DVB (13508): pms: source code cleanup, use struct v4l2_device.
[safe/jmp/linux-2.6] / drivers / media / video / pms.c
1 /*
2  *      Media Vision Pro Movie Studio
3  *                      or
4  *      "all you need is an I2C bus some RAM and a prayer"
5  *
6  *      This draws heavily on code
7  *
8  *      (c) Wolfgang Koehler,  wolf@first.gmd.de, Dec. 1994
9  *      Kiefernring 15
10  *      14478 Potsdam, Germany
11  *
12  *      Most of this code is directly derived from his userspace driver.
13  *      His driver works so send any reports to alan@lxorguk.ukuu.org.uk
14  *      unless the userspace driver also doesn't work for you...
15  *
16  *      Changes:
17  *      08/07/2003        Daniele Bellucci <bellucda@tiscali.it>
18  *                        - pms_capture: report back -EFAULT
19  */
20
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/errno.h>
24 #include <linux/fs.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/mm.h>
28 #include <linux/ioport.h>
29 #include <linux/init.h>
30 #include <asm/io.h>
31 #include <linux/videodev.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-device.h>
35 #include <linux/mutex.h>
36
37 #include <asm/uaccess.h>
38
39 MODULE_LICENSE("GPL");
40
41
42 #define MOTOROLA        1
43 #define PHILIPS2        2
44 #define PHILIPS1        3
45 #define MVVMEMORYWIDTH  0x40            /* 512 bytes */
46
47 struct i2c_info {
48         u8 slave;
49         u8 sub;
50         u8 data;
51         u8 hits;
52 };
53
54 struct pms {
55         struct v4l2_device v4l2_dev;
56         struct video_device vdev;
57         struct video_picture picture;
58         int height;
59         int width;
60         unsigned long in_use;
61         struct mutex lock;
62         int i2c_count;
63         struct i2c_info i2cinfo[64];
64
65         int decoder;
66         int standard;   /* 0 - auto 1 - ntsc 2 - pal 3 - secam */
67         int io;
68         int data;
69         void __iomem *mem;
70 };
71
72 static struct pms pms_card;
73
74 /*
75  *      I/O ports and Shared Memory
76  */
77
78 static int io_port = 0x250;
79 module_param(io_port, int, 0);
80
81 static int mem_base = 0xc8000;
82 module_param(mem_base, int, 0);
83
84 static int video_nr = -1;
85 module_param(video_nr, int, 0);
86
87
88 static inline void mvv_write(struct pms *dev, u8 index, u8 value)
89 {
90         outw(index | (value << 8), dev->io);
91 }
92
93 static inline u8 mvv_read(struct pms *dev, u8 index)
94 {
95         outb(index, dev->io);
96         return inb(dev->data);
97 }
98
99 static int pms_i2c_stat(struct pms *dev, u8 slave)
100 {
101         int counter = 0;
102         int i;
103
104         outb(0x28, dev->io);
105
106         while ((inb(dev->data) & 0x01) == 0)
107                 if (counter++ == 256)
108                         break;
109
110         while ((inb(dev->data) & 0x01) != 0)
111                 if (counter++ == 256)
112                         break;
113
114         outb(slave, dev->io);
115
116         counter = 0;
117         while ((inb(dev->data) & 0x01) == 0)
118                 if (counter++ == 256)
119                         break;
120
121         while ((inb(dev->data) & 0x01) != 0)
122                 if (counter++ == 256)
123                         break;
124
125         for (i = 0; i < 12; i++) {
126                 char st = inb(dev->data);
127
128                 if ((st & 2) != 0)
129                         return -1;
130                 if ((st & 1) == 0)
131                         break;
132         }
133         outb(0x29, dev->io);
134         return inb(dev->data);
135 }
136
137 static int pms_i2c_write(struct pms *dev, u16 slave, u16 sub, u16 data)
138 {
139         int skip = 0;
140         int count;
141         int i;
142
143         for (i = 0; i < dev->i2c_count; i++) {
144                 if ((dev->i2cinfo[i].slave == slave) &&
145                     (dev->i2cinfo[i].sub == sub)) {
146                         if (dev->i2cinfo[i].data == data)
147                                 skip = 1;
148                         dev->i2cinfo[i].data = data;
149                         i = dev->i2c_count + 1;
150                 }
151         }
152
153         if (i == dev->i2c_count && dev->i2c_count < 64) {
154                 dev->i2cinfo[dev->i2c_count].slave = slave;
155                 dev->i2cinfo[dev->i2c_count].sub = sub;
156                 dev->i2cinfo[dev->i2c_count].data = data;
157                 dev->i2c_count++;
158         }
159
160         if (skip)
161                 return 0;
162
163         mvv_write(dev, 0x29, sub);
164         mvv_write(dev, 0x2A, data);
165         mvv_write(dev, 0x28, slave);
166
167         outb(0x28, dev->io);
168
169         count = 0;
170         while ((inb(dev->data) & 1) == 0)
171                 if (count > 255)
172                         break;
173         while ((inb(dev->data) & 1) != 0)
174                 if (count > 255)
175                         break;
176
177         count = inb(dev->data);
178
179         if (count & 2)
180                 return -1;
181         return count;
182 }
183
184 static int pms_i2c_read(struct pms *dev, int slave, int sub)
185 {
186         int i;
187
188         for (i = 0; i < dev->i2c_count; i++) {
189                 if (dev->i2cinfo[i].slave == slave && dev->i2cinfo[i].sub == sub)
190                         return dev->i2cinfo[i].data;
191         }
192         return 0;
193 }
194
195
196 static void pms_i2c_andor(struct pms *dev, int slave, int sub, int and, int or)
197 {
198         u8 tmp;
199
200         tmp = pms_i2c_read(dev, slave, sub);
201         tmp = (tmp & and) | or;
202         pms_i2c_write(dev, slave, sub, tmp);
203 }
204
205 /*
206  *      Control functions
207  */
208
209
210 static void pms_videosource(struct pms *dev, short source)
211 {
212         mvv_write(dev, 0x2E, source ? 0x31 : 0x30);
213 }
214
215 static void pms_hue(struct pms *dev, short hue)
216 {
217         switch (dev->decoder) {
218         case MOTOROLA:
219                 pms_i2c_write(dev, 0x8a, 0x00, hue);
220                 break;
221         case PHILIPS2:
222                 pms_i2c_write(dev, 0x8a, 0x07, hue);
223                 break;
224         case PHILIPS1:
225                 pms_i2c_write(dev, 0x42, 0x07, hue);
226                 break;
227         }
228 }
229
230 static void pms_colour(struct pms *dev, short colour)
231 {
232         switch (dev->decoder) {
233         case MOTOROLA:
234                 pms_i2c_write(dev, 0x8a, 0x00, colour);
235                 break;
236         case PHILIPS1:
237                 pms_i2c_write(dev, 0x42, 0x12, colour);
238                 break;
239         }
240 }
241
242
243 static void pms_contrast(struct pms *dev, short contrast)
244 {
245         switch (dev->decoder) {
246         case MOTOROLA:
247                 pms_i2c_write(dev, 0x8a, 0x00, contrast);
248                 break;
249         case PHILIPS1:
250                 pms_i2c_write(dev, 0x42, 0x13, contrast);
251                 break;
252         }
253 }
254
255 static void pms_brightness(struct pms *dev, short brightness)
256 {
257         switch (dev->decoder) {
258         case MOTOROLA:
259                 pms_i2c_write(dev, 0x8a, 0x00, brightness);
260                 pms_i2c_write(dev, 0x8a, 0x00, brightness);
261                 pms_i2c_write(dev, 0x8a, 0x00, brightness);
262                 break;
263         case PHILIPS1:
264                 pms_i2c_write(dev, 0x42, 0x19, brightness);
265                 break;
266         }
267 }
268
269
270 static void pms_format(struct pms *dev, short format)
271 {
272         int target;
273
274         dev->standard = format;
275
276         if (dev->decoder == PHILIPS1)
277                 target = 0x42;
278         else if (dev->decoder == PHILIPS2)
279                 target = 0x8a;
280         else
281                 return;
282
283         switch (format) {
284         case 0: /* Auto */
285                 pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x00);
286                 pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x80);
287                 break;
288         case 1: /* NTSC */
289                 pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x00);
290                 pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x40);
291                 break;
292         case 2: /* PAL */
293                 pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x00);
294                 pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x00);
295                 break;
296         case 3: /* SECAM */
297                 pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x01);
298                 pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x00);
299                 break;
300         }
301 }
302
303 #ifdef FOR_FUTURE_EXPANSION
304
305 /*
306  *      These features of the PMS card are not currently exposes. They
307  *      could become a private v4l ioctl for PMSCONFIG or somesuch if
308  *      people need it. We also don't yet use the PMS interrupt.
309  */
310
311 static void pms_hstart(struct pms *dev, short start)
312 {
313         switch (dev->decoder) {
314         case PHILIPS1:
315                 pms_i2c_write(dev, 0x8a, 0x05, start);
316                 pms_i2c_write(dev, 0x8a, 0x18, start);
317                 break;
318         case PHILIPS2:
319                 pms_i2c_write(dev, 0x42, 0x05, start);
320                 pms_i2c_write(dev, 0x42, 0x18, start);
321                 break;
322         }
323 }
324
325 /*
326  *      Bandpass filters
327  */
328
329 static void pms_bandpass(struct pms *dev, short pass)
330 {
331         if (dev->decoder == PHILIPS2)
332                 pms_i2c_andor(dev, 0x8a, 0x06, 0xcf, (pass & 0x03) << 4);
333         else if (dev->decoder == PHILIPS1)
334                 pms_i2c_andor(dev, 0x42, 0x06, 0xcf, (pass & 0x03) << 4);
335 }
336
337 static void pms_antisnow(struct pms *dev, short snow)
338 {
339         if (dev->decoder == PHILIPS2)
340                 pms_i2c_andor(dev, 0x8a, 0x06, 0xf3, (snow & 0x03) << 2);
341         else if (dev->decoder == PHILIPS1)
342                 pms_i2c_andor(dev, 0x42, 0x06, 0xf3, (snow & 0x03) << 2);
343 }
344
345 static void pms_sharpness(struct pms *dev, short sharp)
346 {
347         if (dev->decoder == PHILIPS2)
348                 pms_i2c_andor(dev, 0x8a, 0x06, 0xfc, sharp & 0x03);
349         else if (dev->decoder == PHILIPS1)
350                 pms_i2c_andor(dev, 0x42, 0x06, 0xfc, sharp & 0x03);
351 }
352
353 static void pms_chromaagc(struct pms *dev, short agc)
354 {
355         if (dev->decoder == PHILIPS2)
356                 pms_i2c_andor(dev, 0x8a, 0x0c, 0x9f, (agc & 0x03) << 5);
357         else if (dev->decoder == PHILIPS1)
358                 pms_i2c_andor(dev, 0x42, 0x0c, 0x9f, (agc & 0x03) << 5);
359 }
360
361 static void pms_vertnoise(struct pms *dev, short noise)
362 {
363         if (dev->decoder == PHILIPS2)
364                 pms_i2c_andor(dev, 0x8a, 0x10, 0xfc, noise & 3);
365         else if (dev->decoder == PHILIPS1)
366                 pms_i2c_andor(dev, 0x42, 0x10, 0xfc, noise & 3);
367 }
368
369 static void pms_forcecolour(struct pms *dev, short colour)
370 {
371         if (dev->decoder == PHILIPS2)
372                 pms_i2c_andor(dev, 0x8a, 0x0c, 0x7f, (colour & 1) << 7);
373         else if (dev->decoder == PHILIPS1)
374                 pms_i2c_andor(dev, 0x42, 0x0c, 0x7, (colour & 1) << 7);
375 }
376
377 static void pms_antigamma(struct pms *dev, short gamma)
378 {
379         if (dev->decoder == PHILIPS2)
380                 pms_i2c_andor(dev, 0xb8, 0x00, 0x7f, (gamma & 1) << 7);
381         else if (dev->decoder == PHILIPS1)
382                 pms_i2c_andor(dev, 0x42, 0x20, 0x7, (gamma & 1) << 7);
383 }
384
385 static void pms_prefilter(struct pms *dev, short filter)
386 {
387         if (dev->decoder == PHILIPS2)
388                 pms_i2c_andor(dev, 0x8a, 0x06, 0xbf, (filter & 1) << 6);
389         else if (dev->decoder == PHILIPS1)
390                 pms_i2c_andor(dev, 0x42, 0x06, 0xbf, (filter & 1) << 6);
391 }
392
393 static void pms_hfilter(struct pms *dev, short filter)
394 {
395         if (dev->decoder == PHILIPS2)
396                 pms_i2c_andor(dev, 0xb8, 0x04, 0x1f, (filter & 7) << 5);
397         else if (dev->decoder == PHILIPS1)
398                 pms_i2c_andor(dev, 0x42, 0x24, 0x1f, (filter & 7) << 5);
399 }
400
401 static void pms_vfilter(struct pms *dev, short filter)
402 {
403         if (dev->decoder == PHILIPS2)
404                 pms_i2c_andor(dev, 0xb8, 0x08, 0x9f, (filter & 3) << 5);
405         else if (dev->decoder == PHILIPS1)
406                 pms_i2c_andor(dev, 0x42, 0x28, 0x9f, (filter & 3) << 5);
407 }
408
409 static void pms_killcolour(struct pms *dev, short colour)
410 {
411         if (dev->decoder == PHILIPS2) {
412                 pms_i2c_andor(dev, 0x8a, 0x08, 0x07, (colour & 0x1f) << 3);
413                 pms_i2c_andor(dev, 0x8a, 0x09, 0x07, (colour & 0x1f) << 3);
414         } else if (dev->decoder == PHILIPS1) {
415                 pms_i2c_andor(dev, 0x42, 0x08, 0x07, (colour & 0x1f) << 3);
416                 pms_i2c_andor(dev, 0x42, 0x09, 0x07, (colour & 0x1f) << 3);
417         }
418 }
419
420 static void pms_chromagain(struct pms *dev, short chroma)
421 {
422         if (dev->decoder == PHILIPS2)
423                 pms_i2c_write(dev, 0x8a, 0x11, chroma);
424         else if (dev->decoder == PHILIPS1)
425                 pms_i2c_write(dev, 0x42, 0x11, chroma);
426 }
427
428
429 static void pms_spacialcompl(struct pms *dev, short data)
430 {
431         mvv_write(dev, 0x3b, data);
432 }
433
434 static void pms_spacialcomph(struct pms *dev, short data)
435 {
436         mvv_write(dev, 0x3a, data);
437 }
438
439 static void pms_vstart(struct pms *dev, short start)
440 {
441         mvv_write(dev, 0x16, start);
442         mvv_write(dev, 0x17, (start >> 8) & 0x01);
443 }
444
445 #endif
446
447 static void pms_secamcross(struct pms *dev, short cross)
448 {
449         if (dev->decoder == PHILIPS2)
450                 pms_i2c_andor(dev, 0x8a, 0x0f, 0xdf, (cross & 1) << 5);
451         else if (dev->decoder == PHILIPS1)
452                 pms_i2c_andor(dev, 0x42, 0x0f, 0xdf, (cross & 1) << 5);
453 }
454
455
456 static void pms_swsense(struct pms *dev, short sense)
457 {
458         if (dev->decoder == PHILIPS2) {
459                 pms_i2c_write(dev, 0x8a, 0x0a, sense);
460                 pms_i2c_write(dev, 0x8a, 0x0b, sense);
461         } else if (dev->decoder == PHILIPS1) {
462                 pms_i2c_write(dev, 0x42, 0x0a, sense);
463                 pms_i2c_write(dev, 0x42, 0x0b, sense);
464         }
465 }
466
467
468 static void pms_framerate(struct pms *dev, short frr)
469 {
470         int fps = (dev->standard == 1) ? 30 : 25;
471
472         if (frr == 0)
473                 return;
474         fps = fps/frr;
475         mvv_write(dev, 0x14, 0x80 | fps);
476         mvv_write(dev, 0x15, 1);
477 }
478
479 static void pms_vert(struct pms *dev, u8 deciden, u8 decinum)
480 {
481         mvv_write(dev, 0x1c, deciden);  /* Denominator */
482         mvv_write(dev, 0x1d, decinum);  /* Numerator */
483 }
484
485 /*
486  *      Turn 16bit ratios into best small ratio the chipset can grok
487  */
488
489 static void pms_vertdeci(struct pms *dev, unsigned short decinum, unsigned short deciden)
490 {
491         /* Knock it down by / 5 once */
492         if (decinum % 5 == 0) {
493                 deciden /= 5;
494                 decinum /= 5;
495         }
496         /*
497          *      3's
498          */
499         while (decinum % 3 == 0 && deciden % 3 == 0) {
500                 deciden /= 3;
501                 decinum /= 3;
502         }
503         /*
504          *      2's
505          */
506         while (decinum % 2 == 0 && deciden % 2 == 0) {
507                 decinum /= 2;
508                 deciden /= 2;
509         }
510         /*
511          *      Fudgyify
512          */
513         while (deciden > 32) {
514                 deciden /= 2;
515                 decinum = (decinum + 1) / 2;
516         }
517         if (deciden == 32)
518                 deciden--;
519         pms_vert(dev, deciden, decinum);
520 }
521
522 static void pms_horzdeci(struct pms *dev, short decinum, short deciden)
523 {
524         if (decinum <= 512) {
525                 if (decinum % 5 == 0) {
526                         decinum /= 5;
527                         deciden /= 5;
528                 }
529         } else {
530                 decinum = 512;
531                 deciden = 640;  /* 768 would be ideal */
532         }
533
534         while (((decinum | deciden) & 1) == 0) {
535                 decinum >>= 1;
536                 deciden >>= 1;
537         }
538         while (deciden > 32) {
539                 deciden >>= 1;
540                 decinum = (decinum + 1) >> 1;
541         }
542         if (deciden == 32)
543                 deciden--;
544
545         mvv_write(dev, 0x24, 0x80 | deciden);
546         mvv_write(dev, 0x25, decinum);
547 }
548
549 static void pms_resolution(struct pms *dev, short width, short height)
550 {
551         int fg_height;
552
553         fg_height = height;
554         if (fg_height > 280)
555                 fg_height = 280;
556
557         mvv_write(dev, 0x18, fg_height);
558         mvv_write(dev, 0x19, fg_height >> 8);
559
560         if (dev->standard == 1) {
561                 mvv_write(dev, 0x1a, 0xfc);
562                 mvv_write(dev, 0x1b, 0x00);
563                 if (height > fg_height)
564                         pms_vertdeci(dev, 240, 240);
565                 else
566                         pms_vertdeci(dev, fg_height, 240);
567         } else {
568                 mvv_write(dev, 0x1a, 0x1a);
569                 mvv_write(dev, 0x1b, 0x01);
570                 if (fg_height > 256)
571                         pms_vertdeci(dev, 270, 270);
572                 else
573                         pms_vertdeci(dev, fg_height, 270);
574         }
575         mvv_write(dev, 0x12, 0);
576         mvv_write(dev, 0x13, MVVMEMORYWIDTH);
577         mvv_write(dev, 0x42, 0x00);
578         mvv_write(dev, 0x43, 0x00);
579         mvv_write(dev, 0x44, MVVMEMORYWIDTH);
580
581         mvv_write(dev, 0x22, width + 8);
582         mvv_write(dev, 0x23, (width + 8) >> 8);
583
584         if (dev->standard == 1)
585                 pms_horzdeci(dev, width, 640);
586         else
587                 pms_horzdeci(dev, width + 8, 768);
588
589         mvv_write(dev, 0x30, mvv_read(dev, 0x30) & 0xfe);
590         mvv_write(dev, 0x08, mvv_read(dev, 0x08) | 0x01);
591         mvv_write(dev, 0x01, mvv_read(dev, 0x01) & 0xfd);
592         mvv_write(dev, 0x32, 0x00);
593         mvv_write(dev, 0x33, MVVMEMORYWIDTH);
594 }
595
596
597 /*
598  *      Set Input
599  */
600
601 static void pms_vcrinput(struct pms *dev, short input)
602 {
603         if (dev->decoder == PHILIPS2)
604                 pms_i2c_andor(dev, 0x8a, 0x0d, 0x7f, (input & 1) << 7);
605         else if (dev->decoder == PHILIPS1)
606                 pms_i2c_andor(dev, 0x42, 0x0d, 0x7f, (input & 1) << 7);
607 }
608
609
610 static int pms_capture(struct pms *dev, char __user *buf, int rgb555, int count)
611 {
612         int y;
613         int dw = 2 * dev->width;
614         char tmp[dw + 32]; /* using a temp buffer is faster than direct  */
615         int cnt = 0;
616         int len = 0;
617         unsigned char r8 = 0x5;  /* value for reg8  */
618
619         if (rgb555)
620                 r8 |= 0x20; /* else use untranslated rgb = 565 */
621         mvv_write(dev, 0x08, r8); /* capture rgb555/565, init DRAM, PC enable */
622
623 /*      printf("%d %d %d %d %d %x %x\n",width,height,voff,nom,den,mvv_buf); */
624
625         for (y = 0; y < dev->height; y++) {
626                 writeb(0, dev->mem);  /* synchronisiert neue Zeile */
627
628                 /*
629                  *      This is in truth a fifo, be very careful as if you
630                  *      forgot this odd things will occur 8)
631                  */
632
633                 memcpy_fromio(tmp, dev->mem, dw + 32); /* discard 16 word   */
634                 cnt -= dev->height;
635                 while (cnt <= 0) {
636                         /*
637                          *      Don't copy too far
638                          */
639                         int dt = dw;
640                         if (dt + len > count)
641                                 dt = count - len;
642                         cnt += dev->height;
643                         if (copy_to_user(buf, tmp + 32, dt))
644                                 return len ? len : -EFAULT;
645                         buf += dt;
646                         len += dt;
647                 }
648         }
649         return len;
650 }
651
652
653 /*
654  *      Video4linux interfacing
655  */
656
657 static long pms_do_ioctl(struct file *file, unsigned int cmd, void *arg)
658 {
659         struct pms *dev = video_drvdata(file);
660
661         switch (cmd) {
662         case VIDIOCGCAP: {
663                 struct video_capability *b = arg;
664
665                 strcpy(b->name, "Mediavision PMS");
666                 b->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
667                 b->channels = 4;
668                 b->audios = 0;
669                 b->maxwidth = 640;
670                 b->maxheight = 480;
671                 b->minwidth = 16;
672                 b->minheight = 16;
673                 return 0;
674         }
675         case VIDIOCGCHAN: {
676                 struct video_channel *v = arg;
677
678                 if (v->channel < 0 || v->channel > 3)
679                         return -EINVAL;
680                 v->flags = 0;
681                 v->tuners = 1;
682                 /* Good question.. its composite or SVHS so.. */
683                 v->type = VIDEO_TYPE_CAMERA;
684                 switch (v->channel) {
685                 case 0:
686                         strcpy(v->name, "Composite");
687                         break;
688                 case 1:
689                         strcpy(v->name, "SVideo");
690                         break;
691                 case 2:
692                         strcpy(v->name, "Composite(VCR)");
693                         break;
694                 case 3:
695                         strcpy(v->name, "SVideo(VCR)");
696                         break;
697                 }
698                 return 0;
699         }
700         case VIDIOCSCHAN: {
701                 struct video_channel *v = arg;
702
703                 if (v->channel < 0 || v->channel > 3)
704                         return -EINVAL;
705                 mutex_lock(&dev->lock);
706                 pms_videosource(dev, v->channel & 1);
707                 pms_vcrinput(dev, v->channel >> 1);
708                 mutex_unlock(&dev->lock);
709                 return 0;
710         }
711         case VIDIOCGTUNER: {
712                 struct video_tuner *v = arg;
713
714                 if (v->tuner)
715                         return -EINVAL;
716                 strcpy(v->name, "Format");
717                 v->rangelow = 0;
718                 v->rangehigh = 0;
719                 v->flags = VIDEO_TUNER_PAL | VIDEO_TUNER_NTSC | VIDEO_TUNER_SECAM;
720                 switch (dev->standard) {
721                 case 0:
722                         v->mode = VIDEO_MODE_AUTO;
723                         break;
724                 case 1:
725                         v->mode = VIDEO_MODE_NTSC;
726                         break;
727                 case 2:
728                         v->mode = VIDEO_MODE_PAL;
729                         break;
730                 case 3:
731                         v->mode = VIDEO_MODE_SECAM;
732                         break;
733                 }
734                 return 0;
735         }
736         case VIDIOCSTUNER: {
737                 struct video_tuner *v = arg;
738
739                 if (v->tuner)
740                         return -EINVAL;
741                 mutex_lock(&dev->lock);
742                 switch (v->mode) {
743                 case VIDEO_MODE_AUTO:
744                         pms_framerate(dev, 25);
745                         pms_secamcross(dev, 0);
746                         pms_format(dev, 0);
747                         break;
748                 case VIDEO_MODE_NTSC:
749                         pms_framerate(dev, 30);
750                         pms_secamcross(dev, 0);
751                         pms_format(dev, 1);
752                         break;
753                 case VIDEO_MODE_PAL:
754                         pms_framerate(dev, 25);
755                         pms_secamcross(dev, 0);
756                         pms_format(dev, 2);
757                         break;
758                 case VIDEO_MODE_SECAM:
759                         pms_framerate(dev, 25);
760                         pms_secamcross(dev, 1);
761                         pms_format(dev, 2);
762                         break;
763                 default:
764                         mutex_unlock(&dev->lock);
765                         return -EINVAL;
766                 }
767                 mutex_unlock(&dev->lock);
768                 return 0;
769         }
770         case VIDIOCGPICT: {
771                 struct video_picture *p = arg;
772
773                 *p = dev->picture;
774                 return 0;
775         }
776         case VIDIOCSPICT: {
777                 struct video_picture *p = arg;
778
779                 if (!((p->palette == VIDEO_PALETTE_RGB565 && p->depth == 16) ||
780                       (p->palette == VIDEO_PALETTE_RGB555 && p->depth == 15)))
781                         return -EINVAL;
782                 dev->picture = *p;
783
784                 /*
785                  *      Now load the card.
786                  */
787
788                 mutex_lock(&dev->lock);
789                 pms_brightness(dev, p->brightness >> 8);
790                 pms_hue(dev, p->hue >> 8);
791                 pms_colour(dev, p->colour >> 8);
792                 pms_contrast(dev, p->contrast >> 8);
793                 mutex_unlock(&dev->lock);
794                 return 0;
795         }
796         case VIDIOCSWIN: {
797                 struct video_window *vw = arg;
798
799                 if (vw->flags)
800                         return -EINVAL;
801                 if (vw->clipcount)
802                         return -EINVAL;
803                 if (vw->height < 16 || vw->height > 480)
804                         return -EINVAL;
805                 if (vw->width < 16 || vw->width > 640)
806                         return -EINVAL;
807                 dev->width = vw->width;
808                 dev->height = vw->height;
809                 mutex_lock(&dev->lock);
810                 pms_resolution(dev, dev->width, dev->height);
811                 /* Ok we figured out what to use from our wide choice */
812                 mutex_unlock(&dev->lock);
813                 return 0;
814         }
815         case VIDIOCGWIN: {
816                 struct video_window *vw = arg;
817
818                 memset(vw, 0, sizeof(*vw));
819                 vw->width = dev->width;
820                 vw->height = dev->height;
821                 return 0;
822         }
823         case VIDIOCKEY:
824                 return 0;
825         case VIDIOCCAPTURE:
826         case VIDIOCGFBUF:
827         case VIDIOCSFBUF:
828         case VIDIOCGFREQ:
829         case VIDIOCSFREQ:
830         case VIDIOCGAUDIO:
831         case VIDIOCSAUDIO:
832                 return -EINVAL;
833         default:
834                 return -ENOIOCTLCMD;
835         }
836         return 0;
837 }
838
839 static long pms_ioctl(struct file *file,
840                      unsigned int cmd, unsigned long arg)
841 {
842         return video_usercopy(file, cmd, arg, pms_do_ioctl);
843 }
844
845 static ssize_t pms_read(struct file *file, char __user *buf,
846                     size_t count, loff_t *ppos)
847 {
848         struct pms *dev = video_drvdata(file);
849         int len;
850
851         mutex_lock(&dev->lock);
852         len = pms_capture(dev, buf, (dev->picture.depth == 16) ? 0 : 1, count);
853         mutex_unlock(&dev->lock);
854         return len;
855 }
856
857 static int pms_exclusive_open(struct file *file)
858 {
859         struct pms *dev = video_drvdata(file);
860
861         return test_and_set_bit(0, &dev->in_use) ? -EBUSY : 0;
862 }
863
864 static int pms_exclusive_release(struct file *file)
865 {
866         struct pms *dev = video_drvdata(file);
867
868         clear_bit(0, &dev->in_use);
869         return 0;
870 }
871
872 static const struct v4l2_file_operations pms_fops = {
873         .owner          = THIS_MODULE,
874         .open           = pms_exclusive_open,
875         .release        = pms_exclusive_release,
876         .ioctl          = pms_ioctl,
877         .read           = pms_read,
878 };
879
880
881 /*
882  *      Probe for and initialise the Mediavision PMS
883  */
884
885 static int init_mediavision(struct pms *dev)
886 {
887         int id;
888         int idec, decst;
889         int i;
890         static const unsigned char i2c_defs[] = {
891                 0x4c, 0x30, 0x00, 0xe8,
892                 0xb6, 0xe2, 0x00, 0x00,
893                 0xff, 0xff, 0x00, 0x00,
894                 0x00, 0x00, 0x78, 0x98,
895                 0x00, 0x00, 0x00, 0x00,
896                 0x34, 0x0a, 0xf4, 0xce,
897                 0xe4
898         };
899
900         dev->mem = ioremap(mem_base, 0x800);
901         if (!dev->mem)
902                 return -ENOMEM;
903
904         if (!request_region(0x9a01, 1, "Mediavision PMS config")) {
905                 printk(KERN_WARNING "mediavision: unable to detect: 0x9a01 in use.\n");
906                 iounmap(dev->mem);
907                 return -EBUSY;
908         }
909         if (!request_region(dev->io, 3, "Mediavision PMS")) {
910                 printk(KERN_WARNING "mediavision: I/O port %d in use.\n", dev->io);
911                 release_region(0x9a01, 1);
912                 iounmap(dev->mem);
913                 return -EBUSY;
914         }
915         outb(0xb8, 0x9a01);             /* Unlock */
916         outb(dev->io >> 4, 0x9a01);     /* Set IO port */
917
918
919         id = mvv_read(dev, 3);
920         decst = pms_i2c_stat(dev, 0x43);
921
922         if (decst != -1)
923                 idec = 2;
924         else if (pms_i2c_stat(dev, 0xb9) != -1)
925                 idec = 3;
926         else if (pms_i2c_stat(dev, 0x8b) != -1)
927                 idec = 1;
928         else
929                 idec = 0;
930
931         printk(KERN_INFO "PMS type is %d\n", idec);
932         if (idec == 0) {
933                 release_region(dev->io, 3);
934                 release_region(0x9a01, 1);
935                 iounmap(dev->mem);
936                 return -ENODEV;
937         }
938
939         /*
940          *      Ok we have a PMS of some sort
941          */
942
943         mvv_write(dev, 0x04, mem_base >> 12);   /* Set the memory area */
944
945         /* Ok now load the defaults */
946
947         for (i = 0; i < 0x19; i++) {
948                 if (i2c_defs[i] == 0xff)
949                         pms_i2c_andor(dev, 0x8a, i, 0x07, 0x00);
950                 else
951                         pms_i2c_write(dev, 0x8a, i, i2c_defs[i]);
952         }
953
954         pms_i2c_write(dev, 0xb8, 0x00, 0x12);
955         pms_i2c_write(dev, 0xb8, 0x04, 0x00);
956         pms_i2c_write(dev, 0xb8, 0x07, 0x00);
957         pms_i2c_write(dev, 0xb8, 0x08, 0x00);
958         pms_i2c_write(dev, 0xb8, 0x09, 0xff);
959         pms_i2c_write(dev, 0xb8, 0x0a, 0x00);
960         pms_i2c_write(dev, 0xb8, 0x0b, 0x10);
961         pms_i2c_write(dev, 0xb8, 0x10, 0x03);
962
963         mvv_write(dev, 0x01, 0x00);
964         mvv_write(dev, 0x05, 0xa0);
965         mvv_write(dev, 0x08, 0x25);
966         mvv_write(dev, 0x09, 0x00);
967         mvv_write(dev, 0x0a, 0x20 | MVVMEMORYWIDTH);
968
969         mvv_write(dev, 0x10, 0x02);
970         mvv_write(dev, 0x1e, 0x0c);
971         mvv_write(dev, 0x1f, 0x03);
972         mvv_write(dev, 0x26, 0x06);
973
974         mvv_write(dev, 0x2b, 0x00);
975         mvv_write(dev, 0x2c, 0x20);
976         mvv_write(dev, 0x2d, 0x00);
977         mvv_write(dev, 0x2f, 0x70);
978         mvv_write(dev, 0x32, 0x00);
979         mvv_write(dev, 0x33, MVVMEMORYWIDTH);
980         mvv_write(dev, 0x34, 0x00);
981         mvv_write(dev, 0x35, 0x00);
982         mvv_write(dev, 0x3a, 0x80);
983         mvv_write(dev, 0x3b, 0x10);
984         mvv_write(dev, 0x20, 0x00);
985         mvv_write(dev, 0x21, 0x00);
986         mvv_write(dev, 0x30, 0x22);
987         return 0;
988 }
989
990 /*
991  *      Initialization and module stuff
992  */
993
994 #ifndef MODULE
995 static int enable;
996 module_param(enable, int, 0);
997 #endif
998
999 static int __init pms_init(void)
1000 {
1001         struct pms *dev = &pms_card;
1002         struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
1003         int res;
1004
1005         strlcpy(v4l2_dev->name, "pms", sizeof(v4l2_dev->name));
1006
1007         v4l2_info(v4l2_dev, "Mediavision Pro Movie Studio driver 0.03\n");
1008
1009 #ifndef MODULE
1010         if (!enable) {
1011                 v4l2_err(v4l2_dev,
1012                         "PMS: not enabled, use pms.enable=1 to probe\n");
1013                 return -ENODEV;
1014         }
1015 #endif
1016
1017         dev->decoder = PHILIPS2;
1018         dev->io = io_port;
1019         dev->data = io_port + 1;
1020
1021         if (init_mediavision(dev)) {
1022                 v4l2_err(v4l2_dev, "Board not found.\n");
1023                 return -ENODEV;
1024         }
1025
1026         res = v4l2_device_register(NULL, v4l2_dev);
1027         if (res < 0) {
1028                 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
1029                 return res;
1030         }
1031
1032         strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
1033         dev->vdev.v4l2_dev = v4l2_dev;
1034         dev->vdev.fops = &pms_fops;
1035         dev->vdev.release = video_device_release_empty;
1036         video_set_drvdata(&dev->vdev, dev);
1037         mutex_init(&dev->lock);
1038         dev->height = 240;
1039         dev->width = 320;
1040         pms_swsense(dev, 75);
1041         pms_resolution(dev, 320, 240);
1042         pms_videosource(dev, 0);
1043         pms_vcrinput(dev, 0);
1044         if (video_register_device(&dev->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1045                 v4l2_device_unregister(&dev->v4l2_dev);
1046                 release_region(dev->io, 3);
1047                 release_region(0x9a01, 1);
1048                 iounmap(dev->mem);
1049                 return -EINVAL;
1050         }
1051         return 0;
1052 }
1053
1054 static void __exit pms_exit(void)
1055 {
1056         struct pms *dev = &pms_card;
1057
1058         video_unregister_device(&dev->vdev);
1059         release_region(dev->io, 3);
1060         release_region(0x9a01, 1);
1061         iounmap(dev->mem);
1062 }
1063
1064 module_init(pms_init);
1065 module_exit(pms_exit);