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