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