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