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