Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.git] / drivers / media / video / saa5249.c
1 /*
2  * Modified in order to keep it compatible both with new and old videotext IOCTLs by
3  * Michael Geng <linux@MichaelGeng.de>
4  *
5  *      Cleaned up to use existing videodev interface and allow the idea
6  *      of multiple teletext decoders on the video4linux iface. Changed i2c
7  *      to cover addressing clashes on device busses. It's also rebuilt so
8  *      you can add arbitary multiple teletext devices to Linux video4linux
9  *      now (well 32 anyway).
10  *
11  *      Alan Cox <Alan.Cox@linux.org>
12  *
13  *      The original driver was heavily modified to match the i2c interface
14  *      It was truncated to use the WinTV boards, too.
15  *
16  *      Copyright (c) 1998 Richard Guenther <richard.guenther@student.uni-tuebingen.de>
17  *
18  * $Id: saa5249.c,v 1.1 1998/03/30 22:23:23 alan Exp $
19  *
20  *      Derived From
21  *
22  * vtx.c:
23  * This is a loadable character-device-driver for videotext-interfaces
24  * (aka teletext). Please check the Makefile/README for a list of supported
25  * interfaces.
26  *
27  * Copyright (c) 1994-97 Martin Buck  <martin-2.buck@student.uni-ulm.de>
28  *
29  *
30  * This program is free software; you can redistribute it and/or modify
31  * it under the terms of the GNU General Public License as published by
32  * the Free Software Foundation; either version 2 of the License, or
33  * (at your option) any later version.
34  *
35  * This program is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  * GNU General Public License for more details.
39  *
40  * You should have received a copy of the GNU General Public License
41  * along with this program; if not, write to the Free Software
42  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
43  * USA.
44  */
45
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
49 #include <linux/mm.h>
50 #include <linux/errno.h>
51 #include <linux/delay.h>
52 #include <linux/ioport.h>
53 #include <linux/slab.h>
54 #include <linux/init.h>
55 #include <stdarg.h>
56 #include <linux/i2c.h>
57 #include <linux/videotext.h>
58 #include <linux/videodev.h>
59 #include <media/v4l2-common.h>
60 #include <linux/mutex.h>
61
62
63 #include <asm/io.h>
64 #include <asm/uaccess.h>
65
66 #define VTX_VER_MAJ 1
67 #define VTX_VER_MIN 8
68
69
70
71 #define NUM_DAUS 4
72 #define NUM_BUFS 8
73 #define IF_NAME "SAA5249"
74
75 static const int disp_modes[8][3] =
76 {
77         { 0x46, 0x03, 0x03 },   /* DISPOFF */
78         { 0x46, 0xcc, 0xcc },   /* DISPNORM */
79         { 0x44, 0x0f, 0x0f },   /* DISPTRANS */
80         { 0x46, 0xcc, 0x46 },   /* DISPINS */
81         { 0x44, 0x03, 0x03 },   /* DISPOFF, interlaced */
82         { 0x44, 0xcc, 0xcc },   /* DISPNORM, interlaced */
83         { 0x44, 0x0f, 0x0f },   /* DISPTRANS, interlaced */
84         { 0x44, 0xcc, 0x46 }    /* DISPINS, interlaced */
85 };
86
87
88
89 #define PAGE_WAIT (300*HZ/1000)                 /* Time between requesting page and */
90                                                 /* checking status bits */
91 #define PGBUF_EXPIRE (15*HZ)                    /* Time to wait before retransmitting */
92                                                 /* page regardless of infobits */
93 typedef struct {
94         u8 pgbuf[VTX_VIRTUALSIZE];              /* Page-buffer */
95         u8 laststat[10];                        /* Last value of infobits for DAU */
96         u8 sregs[7];                            /* Page-request registers */
97         unsigned long expire;                   /* Time when page will be expired */
98         unsigned clrfound : 1;                  /* VTXIOCCLRFOUND has been called */
99         unsigned stopped : 1;                   /* VTXIOCSTOPDAU has been called */
100 } vdau_t;
101
102 struct saa5249_device
103 {
104         vdau_t vdau[NUM_DAUS];                  /* Data for virtual DAUs (the 5249 only has one */
105                                                 /* real DAU, so we have to simulate some more) */
106         int vtx_use_count;
107         int is_searching[NUM_DAUS];
108         int disp_mode;
109         int virtual_mode;
110         struct i2c_client *client;
111         struct mutex lock;
112 };
113
114
115 #define CCTWR 34                /* I²C write/read-address of vtx-chip */
116 #define CCTRD 35
117 #define NOACK_REPEAT 10         /* Retry access this many times on failure */
118 #define CLEAR_DELAY (HZ/20)     /* Time required to clear a page */
119 #define READY_TIMEOUT (30*HZ/1000)      /* Time to wait for ready signal of I²C-bus interface */
120 #define INIT_DELAY 500          /* Time in usec to wait at initialization of CEA interface */
121 #define START_DELAY 10          /* Time in usec to wait before starting write-cycle (CEA) */
122
123 #define VTX_DEV_MINOR 0
124
125 /* General defines and debugging support */
126
127 #ifndef FALSE
128 #define FALSE 0
129 #define TRUE 1
130 #endif
131
132 #define RESCHED do { cond_resched(); } while(0)
133
134 static struct video_device saa_template;        /* Declared near bottom */
135
136 /* Addresses to scan */
137 static unsigned short normal_i2c[] = {34>>1,I2C_CLIENT_END};
138 I2C_CLIENT_INSMOD;
139
140 static struct i2c_client client_template;
141
142 static int saa5249_attach(struct i2c_adapter *adap, int addr, int kind)
143 {
144         int pgbuf;
145         int err;
146         struct i2c_client *client;
147         struct video_device *vd;
148         struct saa5249_device *t;
149
150         printk(KERN_INFO "saa5249: teletext chip found.\n");
151         client=kmalloc(sizeof(*client), GFP_KERNEL);
152         if(client==NULL)
153                 return -ENOMEM;
154         client_template.adapter = adap;
155         client_template.addr = addr;
156         memcpy(client, &client_template, sizeof(*client));
157         t = kzalloc(sizeof(*t), GFP_KERNEL);
158         if(t==NULL)
159         {
160                 kfree(client);
161                 return -ENOMEM;
162         }
163         strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
164         mutex_init(&t->lock);
165
166         /*
167          *      Now create a video4linux device
168          */
169
170         vd = kmalloc(sizeof(struct video_device), GFP_KERNEL);
171         if(vd==NULL)
172         {
173                 kfree(t);
174                 kfree(client);
175                 return -ENOMEM;
176         }
177         i2c_set_clientdata(client, vd);
178         memcpy(vd, &saa_template, sizeof(*vd));
179
180         for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
181         {
182                 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
183                 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
184                 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
185                 t->vdau[pgbuf].expire = 0;
186                 t->vdau[pgbuf].clrfound = TRUE;
187                 t->vdau[pgbuf].stopped = TRUE;
188                 t->is_searching[pgbuf] = FALSE;
189         }
190         vd->priv=t;
191
192
193         /*
194          *      Register it
195          */
196
197         if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
198         {
199                 kfree(t);
200                 kfree(vd);
201                 kfree(client);
202                 return err;
203         }
204         t->client = client;
205         i2c_attach_client(client);
206         return 0;
207 }
208
209 /*
210  *      We do most of the hard work when we become a device on the i2c.
211  */
212
213 static int saa5249_probe(struct i2c_adapter *adap)
214 {
215         if (adap->class & I2C_CLASS_TV_ANALOG)
216                 return i2c_probe(adap, &addr_data, saa5249_attach);
217         return 0;
218 }
219
220 static int saa5249_detach(struct i2c_client *client)
221 {
222         struct video_device *vd = i2c_get_clientdata(client);
223         i2c_detach_client(client);
224         video_unregister_device(vd);
225         kfree(vd->priv);
226         kfree(vd);
227         kfree(client);
228         return 0;
229 }
230
231 /* new I2C driver support */
232
233 static struct i2c_driver i2c_driver_videotext =
234 {
235         .driver = {
236                 .name   = IF_NAME,              /* name */
237         },
238         .id             = I2C_DRIVERID_SAA5249, /* in i2c.h */
239         .attach_adapter = saa5249_probe,
240         .detach_client  = saa5249_detach,
241 };
242
243 static struct i2c_client client_template = {
244         .driver         = &i2c_driver_videotext,
245         .name           = "(unset)",
246 };
247
248 /*
249  *      Wait the given number of jiffies (10ms). This calls the scheduler, so the actual
250  *      delay may be longer.
251  */
252
253 static void jdelay(unsigned long delay)
254 {
255         sigset_t oldblocked = current->blocked;
256
257         spin_lock_irq(&current->sighand->siglock);
258         sigfillset(&current->blocked);
259         recalc_sigpending();
260         spin_unlock_irq(&current->sighand->siglock);
261         msleep_interruptible(jiffies_to_msecs(delay));
262
263         spin_lock_irq(&current->sighand->siglock);
264         current->blocked = oldblocked;
265         recalc_sigpending();
266         spin_unlock_irq(&current->sighand->siglock);
267 }
268
269
270 /*
271  *      I2C interfaces
272  */
273
274 static int i2c_sendbuf(struct saa5249_device *t, int reg, int count, u8 *data)
275 {
276         char buf[64];
277
278         buf[0] = reg;
279         memcpy(buf+1, data, count);
280
281         if(i2c_master_send(t->client, buf, count+1)==count+1)
282                 return 0;
283         return -1;
284 }
285
286 static int i2c_senddata(struct saa5249_device *t, ...)
287 {
288         unsigned char buf[64];
289         int v;
290         int ct=0;
291         va_list argp;
292         va_start(argp,t);
293
294         while((v=va_arg(argp,int))!=-1)
295                 buf[ct++]=v;
296         return i2c_sendbuf(t, buf[0], ct-1, buf+1);
297 }
298
299 /* Get count number of bytes from I²C-device at address adr, store them in buf. Start & stop
300  * handshaking is done by this routine, ack will be sent after the last byte to inhibit further
301  * sending of data. If uaccess is TRUE, data is written to user-space with put_user.
302  * Returns -1 if I²C-device didn't send acknowledge, 0 otherwise
303  */
304
305 static int i2c_getdata(struct saa5249_device *t, int count, u8 *buf)
306 {
307         if(i2c_master_recv(t->client, buf, count)!=count)
308                 return -1;
309         return 0;
310 }
311
312
313 /*
314  *      Standard character-device-driver functions
315  */
316
317 static int do_saa5249_ioctl(struct inode *inode, struct file *file,
318                             unsigned int cmd, void *arg)
319 {
320         static int virtual_mode = FALSE;
321         struct video_device *vd = video_devdata(file);
322         struct saa5249_device *t=vd->priv;
323
324         switch(cmd)
325         {
326                 case VTXIOCGETINFO:
327                 {
328                         vtx_info_t *info = arg;
329                         info->version_major = VTX_VER_MAJ;
330                         info->version_minor = VTX_VER_MIN;
331                         info->numpages = NUM_DAUS;
332                         /*info->cct_type = CCT_TYPE;*/
333                         return 0;
334                 }
335
336                 case VTXIOCCLRPAGE:
337                 {
338                         vtx_pagereq_t *req = arg;
339
340                         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
341                                 return -EINVAL;
342                         memset(t->vdau[req->pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
343                         t->vdau[req->pgbuf].clrfound = TRUE;
344                         return 0;
345                 }
346
347                 case VTXIOCCLRFOUND:
348                 {
349                         vtx_pagereq_t *req = arg;
350
351                         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
352                                 return -EINVAL;
353                         t->vdau[req->pgbuf].clrfound = TRUE;
354                         return 0;
355                 }
356
357                 case VTXIOCPAGEREQ:
358                 {
359                         vtx_pagereq_t *req = arg;
360                         if (!(req->pagemask & PGMASK_PAGE))
361                                 req->page = 0;
362                         if (!(req->pagemask & PGMASK_HOUR))
363                                 req->hour = 0;
364                         if (!(req->pagemask & PGMASK_MINUTE))
365                                 req->minute = 0;
366                         if (req->page < 0 || req->page > 0x8ff) /* 7FF ?? */
367                                 return -EINVAL;
368                         req->page &= 0x7ff;
369                         if (req->hour < 0 || req->hour > 0x3f || req->minute < 0 || req->minute > 0x7f ||
370                                 req->pagemask < 0 || req->pagemask >= PGMASK_MAX || req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
371                                 return -EINVAL;
372                         t->vdau[req->pgbuf].sregs[0] = (req->pagemask & PG_HUND ? 0x10 : 0) | (req->page / 0x100);
373                         t->vdau[req->pgbuf].sregs[1] = (req->pagemask & PG_TEN ? 0x10 : 0) | ((req->page / 0x10) & 0xf);
374                         t->vdau[req->pgbuf].sregs[2] = (req->pagemask & PG_UNIT ? 0x10 : 0) | (req->page & 0xf);
375                         t->vdau[req->pgbuf].sregs[3] = (req->pagemask & HR_TEN ? 0x10 : 0) | (req->hour / 0x10);
376                         t->vdau[req->pgbuf].sregs[4] = (req->pagemask & HR_UNIT ? 0x10 : 0) | (req->hour & 0xf);
377                         t->vdau[req->pgbuf].sregs[5] = (req->pagemask & MIN_TEN ? 0x10 : 0) | (req->minute / 0x10);
378                         t->vdau[req->pgbuf].sregs[6] = (req->pagemask & MIN_UNIT ? 0x10 : 0) | (req->minute & 0xf);
379                         t->vdau[req->pgbuf].stopped = FALSE;
380                         t->vdau[req->pgbuf].clrfound = TRUE;
381                         t->is_searching[req->pgbuf] = TRUE;
382                         return 0;
383                 }
384
385                 case VTXIOCGETSTAT:
386                 {
387                         vtx_pagereq_t *req = arg;
388                         u8 infobits[10];
389                         vtx_pageinfo_t info;
390                         int a;
391
392                         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
393                                 return -EINVAL;
394                         if (!t->vdau[req->pgbuf].stopped)
395                         {
396                                 if (i2c_senddata(t, 2, 0, -1) ||
397                                         i2c_sendbuf(t, 3, sizeof(t->vdau[0].sregs), t->vdau[req->pgbuf].sregs) ||
398                                         i2c_senddata(t, 8, 0, 25, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', -1) ||
399                                         i2c_senddata(t, 2, 0, t->vdau[req->pgbuf].sregs[0] | 8, -1) ||
400                                         i2c_senddata(t, 8, 0, 25, 0, -1))
401                                         return -EIO;
402                                 jdelay(PAGE_WAIT);
403                                 if (i2c_getdata(t, 10, infobits))
404                                         return -EIO;
405
406                                 if (!(infobits[8] & 0x10) && !(infobits[7] & 0xf0) &&   /* check FOUND-bit */
407                                         (memcmp(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)) ||
408                                         time_after_eq(jiffies, t->vdau[req->pgbuf].expire)))
409                                 {               /* check if new page arrived */
410                                         if (i2c_senddata(t, 8, 0, 0, 0, -1) ||
411                                                 i2c_getdata(t, VTX_PAGESIZE, t->vdau[req->pgbuf].pgbuf))
412                                                 return -EIO;
413                                         t->vdau[req->pgbuf].expire = jiffies + PGBUF_EXPIRE;
414                                         memset(t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE, ' ', VTX_VIRTUALSIZE - VTX_PAGESIZE);
415                                         if (t->virtual_mode)
416                                         {
417                                                 /* Packet X/24 */
418                                                 if (i2c_senddata(t, 8, 0, 0x20, 0, -1) ||
419                                                         i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 20 * 40))
420                                                         return -EIO;
421                                                 /* Packet X/27/0 */
422                                                 if (i2c_senddata(t, 8, 0, 0x21, 0, -1) ||
423                                                         i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 16 * 40))
424                                                         return -EIO;
425                                                 /* Packet 8/30/0...8/30/15
426                                                  * FIXME: AFAIK, the 5249 does hamming-decoding for some bytes in packet 8/30,
427                                                  *        so we should undo this here.
428                                                  */
429                                                 if (i2c_senddata(t, 8, 0, 0x22, 0, -1) ||
430                                                         i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 23 * 40))
431                                                         return -EIO;
432                                         }
433                                         t->vdau[req->pgbuf].clrfound = FALSE;
434                                         memcpy(t->vdau[req->pgbuf].laststat, infobits, sizeof(infobits));
435                                 }
436                                 else
437                                 {
438                                         memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
439                                 }
440                         }
441                         else
442                         {
443                                 memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
444                         }
445
446                         info.pagenum = ((infobits[8] << 8) & 0x700) | ((infobits[1] << 4) & 0xf0) | (infobits[0] & 0x0f);
447                         if (info.pagenum < 0x100)
448                                 info.pagenum += 0x800;
449                         info.hour = ((infobits[5] << 4) & 0x30) | (infobits[4] & 0x0f);
450                         info.minute = ((infobits[3] << 4) & 0x70) | (infobits[2] & 0x0f);
451                         info.charset = ((infobits[7] >> 1) & 7);
452                         info.delete = !!(infobits[3] & 8);
453                         info.headline = !!(infobits[5] & 4);
454                         info.subtitle = !!(infobits[5] & 8);
455                         info.supp_header = !!(infobits[6] & 1);
456                         info.update = !!(infobits[6] & 2);
457                         info.inter_seq = !!(infobits[6] & 4);
458                         info.dis_disp = !!(infobits[6] & 8);
459                         info.serial = !!(infobits[7] & 1);
460                         info.notfound = !!(infobits[8] & 0x10);
461                         info.pblf = !!(infobits[9] & 0x20);
462                         info.hamming = 0;
463                         for (a = 0; a <= 7; a++)
464                         {
465                                 if (infobits[a] & 0xf0)
466                                 {
467                                         info.hamming = 1;
468                                         break;
469                                 }
470                         }
471                         if (t->vdau[req->pgbuf].clrfound)
472                                 info.notfound = 1;
473                         if(copy_to_user(req->buffer, &info, sizeof(vtx_pageinfo_t)))
474                                 return -EFAULT;
475                         if (!info.hamming && !info.notfound)
476                         {
477                                 t->is_searching[req->pgbuf] = FALSE;
478                         }
479                         return 0;
480                 }
481
482                 case VTXIOCGETPAGE:
483                 {
484                         vtx_pagereq_t *req = arg;
485                         int start, end;
486
487                         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS || req->start < 0 ||
488                                 req->start > req->end || req->end >= (virtual_mode ? VTX_VIRTUALSIZE : VTX_PAGESIZE))
489                                 return -EINVAL;
490                         if(copy_to_user(req->buffer, &t->vdau[req->pgbuf].pgbuf[req->start], req->end - req->start + 1))
491                                 return -EFAULT;
492
493                          /*
494                           *     Always read the time directly from SAA5249
495                           */
496
497                         if (req->start <= 39 && req->end >= 32)
498                         {
499                                 int len;
500                                 char buf[16];
501                                 start = max(req->start, 32);
502                                 end = min(req->end, 39);
503                                 len=end-start+1;
504                                 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
505                                         i2c_getdata(t, len, buf))
506                                         return -EIO;
507                                 if(copy_to_user(req->buffer+start-req->start, buf, len))
508                                         return -EFAULT;
509                         }
510                         /* Insert the current header if DAU is still searching for a page */
511                         if (req->start <= 31 && req->end >= 7 && t->is_searching[req->pgbuf])
512                         {
513                                 char buf[32];
514                                 int len;
515                                 start = max(req->start, 7);
516                                 end = min(req->end, 31);
517                                 len=end-start+1;
518                                 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
519                                         i2c_getdata(t, len, buf))
520                                         return -EIO;
521                                 if(copy_to_user(req->buffer+start-req->start, buf, len))
522                                         return -EFAULT;
523                         }
524                         return 0;
525                 }
526
527                 case VTXIOCSTOPDAU:
528                 {
529                         vtx_pagereq_t *req = arg;
530
531                         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
532                                 return -EINVAL;
533                         t->vdau[req->pgbuf].stopped = TRUE;
534                         t->is_searching[req->pgbuf] = FALSE;
535                         return 0;
536                 }
537
538                 case VTXIOCPUTPAGE:
539                 case VTXIOCSETDISP:
540                 case VTXIOCPUTSTAT:
541                         return 0;
542
543                 case VTXIOCCLRCACHE:
544                 {
545                         if (i2c_senddata(t, 0, NUM_DAUS, 0, 8, -1) || i2c_senddata(t, 11,
546                                 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
547                                 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', -1))
548                                 return -EIO;
549                         if (i2c_senddata(t, 3, 0x20, -1))
550                                 return -EIO;
551                         jdelay(10 * CLEAR_DELAY);                       /* I have no idea how long we have to wait here */
552                         return 0;
553                 }
554
555                 case VTXIOCSETVIRT:
556                 {
557                         /* The SAA5249 has virtual-row reception turned on always */
558                         t->virtual_mode = (int)(long)arg;
559                         return 0;
560                 }
561         }
562         return -EINVAL;
563 }
564
565 /*
566  * Translates old vtx IOCTLs to new ones
567  *
568  * This keeps new kernel versions compatible with old userspace programs.
569  */
570 static inline unsigned int vtx_fix_command(unsigned int cmd)
571 {
572         switch (cmd) {
573         case VTXIOCGETINFO_OLD:
574                 cmd = VTXIOCGETINFO;
575                 break;
576         case VTXIOCCLRPAGE_OLD:
577                 cmd = VTXIOCCLRPAGE;
578                 break;
579         case VTXIOCCLRFOUND_OLD:
580                 cmd = VTXIOCCLRFOUND;
581                 break;
582         case VTXIOCPAGEREQ_OLD:
583                 cmd = VTXIOCPAGEREQ;
584                 break;
585         case VTXIOCGETSTAT_OLD:
586                 cmd = VTXIOCGETSTAT;
587                 break;
588         case VTXIOCGETPAGE_OLD:
589                 cmd = VTXIOCGETPAGE;
590                 break;
591         case VTXIOCSTOPDAU_OLD:
592                 cmd = VTXIOCSTOPDAU;
593                 break;
594         case VTXIOCPUTPAGE_OLD:
595                 cmd = VTXIOCPUTPAGE;
596                 break;
597         case VTXIOCSETDISP_OLD:
598                 cmd = VTXIOCSETDISP;
599                 break;
600         case VTXIOCPUTSTAT_OLD:
601                 cmd = VTXIOCPUTSTAT;
602                 break;
603         case VTXIOCCLRCACHE_OLD:
604                 cmd = VTXIOCCLRCACHE;
605                 break;
606         case VTXIOCSETVIRT_OLD:
607                 cmd = VTXIOCSETVIRT;
608                 break;
609         }
610         return cmd;
611 }
612
613 /*
614  *      Handle the locking
615  */
616
617 static int saa5249_ioctl(struct inode *inode, struct file *file,
618                          unsigned int cmd, unsigned long arg)
619 {
620         struct video_device *vd = video_devdata(file);
621         struct saa5249_device *t=vd->priv;
622         int err;
623
624         cmd = vtx_fix_command(cmd);
625         mutex_lock(&t->lock);
626         err = video_usercopy(inode,file,cmd,arg,do_saa5249_ioctl);
627         mutex_unlock(&t->lock);
628         return err;
629 }
630
631 static int saa5249_open(struct inode *inode, struct file *file)
632 {
633         struct video_device *vd = video_devdata(file);
634         struct saa5249_device *t=vd->priv;
635         int err,pgbuf;
636
637         err = video_exclusive_open(inode,file);
638         if (err < 0)
639                 return err;
640
641         if (t->client==NULL) {
642                 err = -ENODEV;
643                 goto fail;
644         }
645
646         if (i2c_senddata(t, 0, 0, -1) ||                /* Select R11 */
647                                                 /* Turn off parity checks (we do this ourselves) */
648                 i2c_senddata(t, 1, disp_modes[t->disp_mode][0], 0, -1) ||
649                                                 /* Display TV-picture, no virtual rows */
650                 i2c_senddata(t, 4, NUM_DAUS, disp_modes[t->disp_mode][1], disp_modes[t->disp_mode][2], 7, -1)) /* Set display to page 4 */
651
652         {
653                 err = -EIO;
654                 goto fail;
655         }
656
657         for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
658         {
659                 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
660                 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
661                 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
662                 t->vdau[pgbuf].expire = 0;
663                 t->vdau[pgbuf].clrfound = TRUE;
664                 t->vdau[pgbuf].stopped = TRUE;
665                 t->is_searching[pgbuf] = FALSE;
666         }
667         t->virtual_mode=FALSE;
668         return 0;
669
670  fail:
671         video_exclusive_release(inode,file);
672         return err;
673 }
674
675
676
677 static int saa5249_release(struct inode *inode, struct file *file)
678 {
679         struct video_device *vd = video_devdata(file);
680         struct saa5249_device *t=vd->priv;
681         i2c_senddata(t, 1, 0x20, -1);           /* Turn off CCT */
682         i2c_senddata(t, 5, 3, 3, -1);           /* Turn off TV-display */
683         video_exclusive_release(inode,file);
684         return 0;
685 }
686
687 static int __init init_saa_5249 (void)
688 {
689         printk(KERN_INFO "SAA5249 driver (" IF_NAME " interface) for VideoText version %d.%d\n",
690                         VTX_VER_MAJ, VTX_VER_MIN);
691         return i2c_add_driver(&i2c_driver_videotext);
692 }
693
694 static void __exit cleanup_saa_5249 (void)
695 {
696         i2c_del_driver(&i2c_driver_videotext);
697 }
698
699 module_init(init_saa_5249);
700 module_exit(cleanup_saa_5249);
701
702 static struct file_operations saa_fops = {
703         .owner          = THIS_MODULE,
704         .open           = saa5249_open,
705         .release        = saa5249_release,
706         .ioctl          = saa5249_ioctl,
707         .compat_ioctl   = v4l_compat_ioctl32,
708         .llseek         = no_llseek,
709 };
710
711 static struct video_device saa_template =
712 {
713         .owner          = THIS_MODULE,
714         .name           = IF_NAME,
715         .type           = VID_TYPE_TELETEXT,    /*| VID_TYPE_TUNER ?? */
716         .hardware       = VID_HARDWARE_SAA5249,
717         .fops           = &saa_fops,
718 };
719
720 MODULE_LICENSE("GPL");