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